I have this code for quicksort
int sum = 0;
int partition(int *L, int left, int right) {
int pivot = left;
int p_val = L[pivot];
while (left < right) {
while (L[left] <= p_val)
left++;
while (L[right] > p_val)
right--;
if (left < right) {
swap(&L[left], &L[right]);
sum++;
}
}
swap(&L[pivot], &L[right]);
sum++;
return right;
}
void quicksort(int *L, int start, int end) {
if (start >= end)
return;
int splitPoint = partition(L, start, end);
quicksort(L, start, splitPoint - 1);
quicksort(L, splitPoint + 1, end);
}
And it works fine, I think. For given array {8,4,2,1}
I get that 3 swaps have been made. BUT, I need to modify the code so that function RETURNS number of swaps. Is this possible, and if so, how? Please explain as simply as you can because I'm a novice in this. I need to do the same with mergeSort but I still haven't found working code that isn't too complicated. When I do, I'll update the question. Thank you.
You can use the int
pointer as the argument of partition
and quicksort
functions:
int partition(int *num, int *L, int left, int right) {
...
if (left < right) {
swap(&L[left], &L[right]);
(*num)++; // increment number of swap
sum++;
}
}
...
swap(&L[pivot], &L[right]);
(*num)++;
sum++;
return right;
}
And in the quicksort
function:
void quicksort(int *num, int *L, int start, int end) {
if (start >= end)
return;
int splitPoint = partition(num, L, start, end);
quicksort(num, L, start, splitPoint - 1);
quicksort(num, L, splitPoint + 1, end);
}
Then in main function:
int main()
{
int arr[] = {8, 4, 2, 4, 5, 7, 8, 9, 1};
int n = sizeof(arr)/sizeof(arr[0]);
int num = 0;
quicksort(&num, arr, 0, n-1);
printf("sum = %d num = %d\n", sum, num);
return 0;
}
Okay now I have this:
int sum = 0;
int partition(int *L, int left, int right) {
int pivot = left;
int p_val = L[pivot];
while (left < right) {
while (L[left] <= p_val)
left++;
while (L[right] > p_val)
right--;
if (left < right) {
swap(&L[left], &L[right]);
sum++;
}
}
swap(&L[pivot], &L[right]);
sum++;
return right;
}
int quicksort(int *L, int start, int end) {
if (start >= end)
return;
int splitPoint = partition(L, start, end);
quicksort(L, start, splitPoint - 1);
quicksort(L, splitPoint + 1, end);
return sum;
}
for QuickSort, and this:
int sumMerge = 0;
void merge(int arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
int mergeSort(int arr[], int l, int r){
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
return sumMerge;
}
for MergeSort. Is this correct? I mean, if I put sumMerge++
somewhere in the code, will this be ok? Also, what part of code actually swaps the values in merge? In quicksort I figured it was kind of obvious, wherever swap
gets called, that's where I increase the counter. But what about merge?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.