/** * Sort an (ascending) array of integers * * Arguments: * arr The array to search * length Number of elements in the array * * Uses "quicksort" to sort "arr". Use the first element of the array * as the pivot. * * Your solution MUST use recursive function (or functions) * * quicksort works in the following way: * * find an element in the array (this element is called the * pivot). * * rearrange the array's elements into two parts: the part smaller * than this pivot and the part greater than this pivot; make the pivot * the element between these two parts * * sort the first and the second parts separately by repeating the * procedure described above * * You will receive no point if you use any other sorting algorithm. * You cannot use selection sort, merge sort, or bubble sort. * * Some students are fascinated by the name of bubble sort. In * reality, bubble sort is rarely used because it is slow. It is a * mystery why some students (or even professors) like bubble sort. * Other than the funny name, bubble sort has nothing special and is * inefficient, in both asymptotic complexity and the amount of data * movement. There are many algorithms much better than bubble sort. * You would not lose anything if you do not know (or forget) bubble * sort. * */ void sort(int * arr, int length) { int pivot = arr[0];; int temp; int left = 1; int right = (length-1); void sorthelp(int*,int,int); while(left<right) //sorts the initial array { while ((arr[left]<pivot) & (left < length)) { left++; } while((arr[right]>pivot) & (right > -1)) { right--; } if (left>right || left==length-1) { temp = arr[left-1]; arr[left-1] = pivot; arr[0] = temp; sorthelp(arr,0,left-2); sorthelp(arr,right+1,length-1); } else { temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } } }
void sorthelp(int* arr, int i, int j){ if(i-j == 1){ return; } int piv = arr[i]; int right = j; int left = i; while(i != j){ while(piv > arr[i]){ i++; } while(piv < arr[j]){ j--; } swap(arr, i, j); } sorthelp(arr, left, i-1); sorthelp(arr, i+1, right); }
void sorthelp(int * arr, int left, int right) { int ind1 = left; int ind2 = right; int pivot = arr[left]; int temp; if (left != right) { left++; while(left<=right) //sorts the current array { while ((arr[left]<pivot) & (left <= ind2)) { left++; } while((arr[right]>pivot) & (right > ind1)) { right--; } if (left>right || left==ind2) { temp = arr[left-1]; arr[left-1] = arr[ind1]; arr[ind1] = temp; sorthelp(arr,ind1,left-1); sorthelp(arr,left,ind2); } else { temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } } } }
/** * Sort an (ascending) array of integers * * Arguments: * arr The array to search * length Number of elements in the array * * Uses "quicksort" to sort "arr". Use the first element of the array * as the pivot. * * Your solution MUST use recursive function (or functions) * * quicksort works in the following way: * * find an element in the array (this element is called the * pivot). * * rearrange the array's elements into two parts: the part smaller * than this pivot and the part greater than this pivot; make the pivot * the element between these two parts * * sort the first and the second parts separately by repeating the * procedure described above * * You will receive no point if you use any other sorting algorithm. * You cannot use selection sort, merge sort, or bubble sort. * * Some students are fascinated by the name of bubble sort. In * reality, bubble sort is rarely used because it is slow. It is a * mystery why some students (or even professors) like bubble sort. * Other than the funny name, bubble sort has nothing special and is * inefficient, in both asymptotic complexity and the amount of data * movement. There are many algorithms much better than bubble sort. * You would not lose anything if you do not know (or forget) bubble * sort. * */ void sort(int * arr, int length){ sorthelp(arr, 0, length-1); }