Largest and Smallest in the Array

Hello,I was trying to know the position of Largest and smallest element in the array using sorting and binary search but i feel something wrong with my binary search implementation.I will be obliged please help me finding the error.link

hey @chotabheemmc

Binary Search works only on sorted array and you are passing b[] which is unsorted array.

for 11

Running:

func bsearch:
    First Call(11)
    b = [33,44,11,22]
    key = 11
    mid = 2
    b[mid] = b[2] = 11
        return 2
        
    Next call(44)
    b = [33,44,11,22]
    key = 44
    mid = 2
    b[mid] = b[2] = 11
    l = 3
    mid = 3
    b[mid] =  b[3] = 22
    l = 4
    return -1

Oh i am so sorry i sorted a[] and passed b[] as an argument thanks for your attention mister.

1 Like

no problem… :slight_smile:

also you need to replace

int st=a[n];

with

int st=a[n-1];
1 Like