Hey guys, been at it all day, but I just cant seem to figure out this assignment I have. It's not due for a while but I am almost done. I just need help with quicksort.
Basically the main program generates random numbers, puts them in an array, copies the array, sends them to 3 separate functions in three separate .cpp files. The objective is to test 3 different sorts, bubble, shell, and quicksort and then have each of the 3 programs sort the array(all arrays have same 20 values) and assign and then return them to the screen. Bubble and shell are easy. But I cannot seem to figure out when the quicksort function(in its' own file) is done and tell it to print the sorted array. Here is my code for the quicksort file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
void quickSort(int arr[], int left, int right) {
int i = left, j = right;
bool end = false;
int tmp;
int pivot = arr[(left + right) / 2];
/* 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--;
}
}
/* recursion */
if (left < j){
quickSort(arr, left, j);
}
if (i < right){
quickSort(arr, i, right);
}
if (i >= right && left >= j){
}
|
So what I am asking is if this is the proper format to quicksort something and then have it returned? Is there a better way to set this up to where when the sort is finally finished, to figure out where or when that exactly is.
Is there a way to just pass the array generated in the main program to a function, have it sorted in the function, and then returned? I know I am asking a lot; it's been a long day. Any tips are appreciated.