int lb = 0, ub = 8, mid; //ub=upper bound,lb=lower bound.
int i = 0, j = 0, k = 0;
int a[] = { 15, 5, 24, 8, 1, 3, 16, 10, 20 };
int b[10];
void mergeSort(int a[], int lb, int ub) {
if (lb < ub) {
mid = ((lb + ub) / 2);
mergeSort(a, lb, mid);
mergeSort(a, mid + 1, ub);
merge(a, lb, mid, ub);
}
}
void merge(int a[], int lb, int mid, int ub) {
i = lb;
j = mid + 1;
k = lb;
while (i <= mid && j <= ub) {
if (a[i] <= a[j]) {
b[k] = a[i];
i++;
} else {
b[k] = a[j];
j++;
}
k++;
}
if (i > mid) {
while (j <= ub) {
b[k] = a[j];
j++;
k++;
}
} else {
while (i <= mid) {
b[k] = a[i];
i++;
k++;
}
}
for (k = lb; k <= ub; k++) {
a[k] = b[k];
}
}
void printList(int A[]) {
for (k = 0; k <= 8; k++) {
printf("%d\n", A[k]);
}
printf("done\n");
}
int main() {
printList(a);
mergeSort(a, 0, 8);
printList(a);
}
I think the code of mergesort
and merge
is not the problem, the problem is how I have checked the code many times but I cannot find the mistakes, so I hope someone could explain to me where is the problems, thanks for everyone who try to help!!
the output when I run the code:
5 15 8 1 3 16 10 20 24 done
which is not a sorted list.
The problem is mid
is a global variable, so it gets modified by the recursive call mergeSort(a, lb, mid);
and a different value is used for the second recursive call mergeSort(a, mid + 1, ub);
and yet a different value is used for the final call merge(a, lb, mid, ub);
producing unexpected results, and potentially causing undefined behavior.
Do not use global variables. Define both arrays in main
, make i
, j
, k
and mid
local variables in merge
and mergeSort
and pass the auxiliary array b
to merge
and mergeSort
.
Here is a modified version:
#include <stdio.h>
void merge(int a[], int lb, int mid, int ub, int b[]) {
int i = lb;
int j = mid + 1;
int k = lb;
while (i <= mid && j <= ub) {
if (a[i] <= a[j]) {
b[k] = a[i];
i++;
} else {
b[k] = a[j];
j++;
}
k++;
}
if (i > mid) {
while (j <= ub) {
b[k] = a[j];
j++;
k++;
}
} else {
while (i <= mid) {
b[k] = a[i];
i++;
k++;
}
}
for (k = lb; k <= ub; k++) {
a[k] = b[k];
}
}
void mergeSort(int a[], int lb, int ub, int b[]) {
if (lb < ub) {
int mid = (lb + ub) / 2;
mergeSort(a, lb, mid, b);
mergeSort(a, mid + 1, ub, b);
merge(a, lb, mid, ub, b);
}
}
void printList(int A[], int len) {
for (int k = 0; k < len; k++) {
printf(" %d", A[k]);
}
printf("\n");
}
int main() {
int a[] = { 15, 5, 24, 8, 1, 3, 16, 10, 20 };
int len = sizeof(a) / sizeof(a[0]);
// make b a temporary array the same size as a
int b[len];
printf("before:");
printList(a, len);
mergeSort(a, 0, len - 1, b);
printf("after:");
printList(a, len);
return 0;
}
Output:
before: 15 5 24 8 1 3 16 10 20 after: 1 3 5 8 10 15 16 20 24
Note that is it less error prone to pass ub
as the index to the element after the last one of the slice, this way the slice length is ub - lb
and you do not need any confusing +1
/ -1
adjustments. Furthermore, there is no need to copy the remaining elements from the right half as they are already in the proper position in the destination array.
Here is a modified version with these simplifications:
#include <stdio.h>
void merge(int a[], int lb, int mid, int ub, int b[]) {
int i = lb;
int j = mid;
int k = lb;
while (i < mid && j < ub) {
if (a[i] <= a[j]) {
b[k++] = a[i++];
} else {
b[k++] = a[j++];
}
}
while (i < mid) {
b[k++] = a[i++];
}
for (i = lb; i < k; i++) {
a[i] = b[i];
}
}
void mergeSort(int a[], int lb, int ub, int b[]) {
if (ub - lb >= 2) {
int mid = lb + (ub - lb) / 2;
mergeSort(a, lb, mid, b);
mergeSort(a, mid, ub, b);
merge(a, lb, mid, ub, b);
}
}
void printList(int A[], int len) {
for (int k = 0; k < len; k++) {
printf(" %d", A[k]);
}
printf("\n");
}
int main() {
int a[] = { 15, 5, 24, 8, 1, 3, 16, 10, 20 };
int len = sizeof(a) / sizeof(a[0]);
// make b a temporary array the same size as a
int b[len];
printf("before:");
printList(a, len);
mergeSort(a, 0, len, b);
printf("after:");
printList(a, len);
return 0;
}
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.