Program #include #include void bubblesort(int array[], int size) { int temp, ctr=0; for (int i=0; i<size; i++) { for (int j=0; j<(size-1)-i; j++) { if (array[j]>array[j+1]) { temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } }
//Bubble Sort Function
//Comparing the Elements //Swapping Elements
cout<<"\n\nThe array after iteration "<<++ctr<<" is:\n"; for (int k=0; k<size; k++) //Printing each iteration { cout<<array[k]<<"\t"; } }
}
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
bubblesort(arr, n);
//Calling bubblesort()
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 ""