//Program 5 #include #include void SelectionSort(int ar[],int size) { int i, j; int small, tmp;
}
//Selection sort function
for (i=0; i<size; i++) { small = i; for (j=i+1; j<size; j++) { if (ar[small]>ar[j]) { small = j; } } tmp = ar[i]; ar[i] = ar[small]; ar[small] = tmp; }
int binarysearch(int arr[], int size, int query) { int first, mid, last; first = 0; last = size-1; while(first <= last) { mid =(first+last)/2; if (query == arr[mid]) return mid; else if (query > arr[mid]) first = mid+1; else last = mid-1; } return -1; } void main() { clrscr(); int arr[50], item, n, index, query, res; cout<<"Enter the number of elements: "; cin>>n; cout<<"Enter the elements: "; for (int i=0; i>arr[i]; }
//Binary Search Function
SelectionSort(arr, n);
//Calling SelectionSort()
cout<<"\n\nThe sorted array is:\n"; for (i=0; i>query; res=binarysearch(arr, n, query);
}
//Calling binarysearch()
if (res == -1) cout<<"Element not found "; else cout<<"Element found at position "<
Related Documents
More Documents from ""