Question
I need to know what each line of code is doing. Please explain!! //Bubblesort algo function void bubbleSort(){ //double timediff; int count; struct car cars[SIZE];//created
I need to know what each line of code is doing. Please explain!!
//Bubblesort algo function void bubbleSort(){ //double timediff; int count; struct car cars[SIZE];//created array with limited space struct car cars2[SIZE]; readCars(&cars[0], &count);//reads aray and places the frist item n the first index of array //clock begin long start =getMicroTime();
//bubble sort for(int j=0; j bool swapped = true; memcpy(&cars2, &cars, sizeof(car)*SIZE); while(swapped){ swapped = false; for(int i=0; i int cmp =strcmp(cars2[i].make, cars2[i+1].make); if(cmp>0){ car temp= cars2[i]; cars2[i]= cars2[i+1]; cars2[i+1]=temp; swapped=true; } } } }
//insertion sort algo function
void insertionSort(){ //double timediff; int count; struct car cars[SIZE];//created array with limited space struct car cars2[SIZE]; readCars(&cars[0], &count);//reads aray and places the frist item n the first index of array //clock begin long start =getMicroTime(); for(int x=0; x memcpy(&cars2, &cars, sizeof(car)*SIZE); int i, j; car key; for(i=1; i key = cars2[i]; j= i-1; while(j>=0 && strcmp(cars[j].make, key.make)>0){ cars[j+1]=cars[j]; j-=1; } cars[j+1]=key; } } long end =getMicroTime(); long elapsed = end - start; cout << "" << endl; cout << "Time taken using insertion sort:" << ((double)elapsed)/LOOPS << "us"<< endl; cout << "" << endl; cout << "This list is sorted" << endl; for(int i=0; i cout <<"Make:"< } }
//QuickSort function
void quickSort(){ //double timediff; int count; struct car cars[SIZE];//created array with limited space struct car cars2[SIZE]; readCars(&cars[0], &count);//reads aray and places the frist item n the first index of array //clock begin long start =getMicroTime(); for(int j=0; j memcpy(&cars2, &cars, sizeof(car)*SIZE); int low = 0; int high = count-1; quickSortRecursive(cars2, low, high); } long end =getMicroTime(); long elapsed = end - start; cout << "" << endl; cout << "Time taken using quickSort sort:" << ((double)elapsed)/LOOPS << "us"<< endl; cout << "" << endl; cout << "This list is sorted" << endl; for(int i=0; i cout <<"Make:"< } }
void quickSortRecursive(car cars[], int low,int high){ if(low int pi = partition(cars, low, high); quickSortRecursive(cars, low, pi-1); quickSortRecursive(cars, pi+1, high); } }
int partition(car cars[], int low, int high){ car pivot = cars[high]; int i=(low-1); for(int j=low; j<=high-1; j++){ int cmp = strcmp(cars[j].make, pivot.make); if(cmp<=0){ i++; car temp= cars[i]; cars[i]= cars[j]; cars[j]=temp; } } car temp= cars[i+1]; cars[i+1]= cars[high]; cars[high]=temp; return (i+1); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started