Question
I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I
I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort
here is my code:
But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me to add this part?
#include
int quickSort(int arr[], int left, int right) { int i = left, j = right; int tmp; int pivot = arr[(left + right) / 2]; //int count =0; /* partition */ while (i <= j) { while (arr[i] < pivot) i++; while (arr[j] > pivot) j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; count++; } };
/* recursion */ if (left < j) quickSort(arr, left, j); if (i < right) quickSort(arr, i, right); return count; } int selectSort(int arr[], int n) {
int index_min_value; int temp; int count=0; for (int i=0; i } if (index_min_value != i) { temp = arr[i]; arr[i] = arr[index_min_value]; arr[index_min_value] = temp; count++; } } return count; } void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp; *yp = temp; } void printList(int arr[], int length){ for (int i = 0; i } cout< int main(){ //HINT: You should place a counter into each algorithm to see how many time each of them run. Then you can compare them easier. //You should use the given print statements for better organization. int count = 0; int myArray1[] = {12, 13, 5, 4, 7, 18, 9 }; int myArray2[] = {12, 13, 5, 4, 7, 18, 9 }; //selectSort int start = clock(); count = selectSort(myArray1,7); int stop = clock(); cout<<"When ordered with selectSort, after "< cout<<"When ordered with quickSort, after "< for(int i=0; i<1000000; i++){ myArray3[i] = (rand()%10000)+1; //cout << myArray5[i] << endl; } int start2 = clock(); count = selectSort(myArray3,1000000); int stop2 = clock(); cout<<"When ordered with selectSort, after "<
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