[英]How to count and return swaps in algorithms - MergeSort and QuickSort?
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.对于给定的数组{8,4,2,1}
,我知道已经进行了 3 次交换。 BUT, I need to modify the code so that function RETURNS number of swaps.但是,我需要修改代码,以便 function 返回交换次数。 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.我需要对 mergeSort 做同样的事情,但我仍然没有找到不太复杂的工作代码。 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
和quicksort
函数的参数:
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:在quicksort
中:
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:然后在主 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?我的意思是,如果我将sumMerge++
放在代码中的某个位置,可以吗? 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.在快速排序中,我认为这很明显,无论哪里调用swap
,这就是我增加计数器的地方。 But what about merge?但是合并呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.