Question: EXPLAIN FUNCTION EVERY LINE OF THIS CODING. (ASAP) #include using namespace std ; void swap(int *ap, int *bp) { int temp = *ap; *ap =
EXPLAIN FUNCTION EVERY LINE OF THIS CODING. (ASAP)
#include
void swap(int *ap, int *bp)
{
int temp = *ap;
*ap = *bp;
*bp = temp;
}
// A function to implement bubble sort on the list
void bubbleSort(int list[], int n)
{
int i, j;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++){
if (list[j] > list[j+1])
{
swap(&list[j], &list[j+1]);}}}
}
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
// If the element is present at the middle
// itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, r, x);
}
// We reach here when element is not
// present in array
return -1;
}
void execute(char c,int list1[],int list2[]){
if(c=='a'||c=='A'){
cout <<" Orginal Order :"< for(int i=0;i<5;i++){ cout< } cout< cout<<"Sorted array in Ascending Order"< bubbleSort(list1,5); for(int i=0;i<5;i++){ cout< } } else if(c=='s'||c=='S'){ cout<<"You want to find "; int x,v; cin>>x; cout< v=binarySearch(list2,0,4,x); if(v==-1){ cout<<"Not found"< } else{ cout< } } else if(c=='x'||c=='X'){ exit(1); } else { cout<<" Sorry . This is an invalid choice "< } } int main(){ int list1[]={90,20,80,60,10}; int list2[] ={90,200,820,1060,1110}; char c; do{ cout<<" what do you want to do (A for Sort,S for Search,X for exit the program:) "; cin>>c; cout< execute(c,list1,list2); }while(1); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
