Program #include #include void insertionsort(int array[], int size) //Insertion Sort Function { int temp, i, j; for (i=0;i<size;i++) { temp = array[i]; //Pick the first element of unsorted array j = i-1; while(temp<array[j] && j>=0) //Scan for its proper place { array[j+1] = array[j]; j = j - 1; } array[j+1] = temp; //Insert the element at its proper place } } 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; }
//Binary Search Function
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]; } insertionsort(arr, n); cout<<"\n\nThe sorted array is:\n"; for (i=0; i
//Calling insertoinsort()
{ }
cout<<arr[i]<<"\t";
cout<<"\n\nEnter the element to be searched: "; cin>>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 ""