[英]I have tried to implement mergesort and am unable to find the exact problem of my code. The program shows no errors but I do not get a desired output
Tried implementing mergesort, below is code.尝试实现合并排序,下面是代码。 Let me know if anything is wrong.让我知道是否有任何问题。 Is there any any error in my merge function?我的合并 function 是否有任何错误? unable to point to the exact problem in my code.无法指出我的代码中的确切问题。 Any help regarding the same would be highly appreciated.任何有关相同的帮助将不胜感激。
#include <iostream>
using namespace std;
Merge function to merge the two sorted arrays合并 function 合并两个排序好的 arrays
void merge(int arr[], int start, int mid, int end) {
int n1 = mid - start + 1;
int n2 = end - mid;
int arr1[n1], arr2[n2];
for (int i = 0; i < n1; i++) {
arr1[i] = arr[start + i];
}
for (int j = 0; j < n2; j++) {
arr2[j] = arr[mid + 1 + j];
}
int i;
int j;
int p;
i = 0;
j = 0;
p = start;
while (i < n1 && j < n2) {
if (arr1[i] <= arr2[j]) {
arr[p] = arr1[i];
i++;
} else {
arr[p] = arr2[j];
j++;
}
p++;
}
while (i < n1) {
arr[p] = arr1[i];
i++;
p++;
}
while (j < n2) {
arr[p] = arr2[j];
i++;
p++;
}
}
Calling mergesort recursively递归调用归并排序
void merge_sort(int arr[], int start, int end) {
if (start >= end) {
return;
}
if (start < end) {
int mid = start + (end - 1) / 2;
merge_sort(arr, start, mid);
merge_sort(arr, mid + 1, end);
merge(arr, start, mid, end);
}
}
Print function to print the array打印 function 打印数组
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
Main function主function
int main() {
int arr[] = { 6, 5, 12, 10, 9, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
merge_sort(arr, 0, n - 1);
cout << "The sorted array is: " << endl;
printArray(arr, n);
}
There are 2 bugs is the code:代码有2个错误:
computing the mid point as int mid = start + (end - 1) / 2;
将中点计算为int mid = start + (end - 1) / 2;
is incorrect.是不正确的。 You should instead write:你应该写:
int mid = start + (end - start) / 2;
in the last loop in merge
you increment i
instead of j
.在merge
的最后一个循环中,您增加i
而不是j
。 Note that this loop is redundant as the remaining elements of arr2
are already at the end of arr
.请注意,此循环是多余的,因为arr2
的其余元素已经在arr
的末尾。
You follow the classic convention in mergesort implementations where end
is the index of the last element in the slice.您遵循合并排序实现中的经典约定,其中end
是切片中最后一个元素的索引。 This convention requires multiple +1
/ -1
adjustments, it easy to get it wrong.这个约定需要多次+1
/ -1
调整,很容易弄错。
In C and related languages where array indices start at 0
, it is more idiomatic to use a different convention: passing start
as the index of the first element of the slice and end
as the index just past the last element.在 C 和数组索引从0
开始的相关语言中,使用不同的约定更为惯用:将start
作为切片的第一个元素的索引,将end
作为最后一个元素的索引。 This allows to specify empty splices and produces cleaner code.这允许指定空拼接并产生更清晰的代码。
Here is a modified version:这是修改后的版本:
#include <iostream>
using namespace std;
// Merge function to merge the two sorted arrays
void merge(int arr[], int start, int mid, int end) {
int n1 = mid - start;
int n2 = end - mid;
int arr1[n1], arr2[n2];
for (int i = 0; i < n1; i++) {
arr1[i] = arr[start + i];
}
for (int j = 0; j < n2; j++) {
arr2[j] = arr[mid + j];
}
int i = 0;
int j = 0;
int p = start;
while (i < n1 && j < n2) {
if (arr1[i] <= arr2[j]) {
arr[p] = arr1[i];
i++;
} else {
arr[p] = arr2[j];
j++;
}
p++;
}
while (i < n1) {
arr[p] = arr1[i];
i++;
p++;
}
// the remaining elements from `arr2` are already in place in `arr`
}
// Calling mergesort recursively
void merge_sort(int arr[], int start, int end) {
if (end - start > 1) {
int mid = start + (end - start) / 2;
merge_sort(arr, start, mid);
merge_sort(arr, mid, end);
merge(arr, start, mid, end);
}
}
// Print function to print the array
void printArray(const int arr[], int size) {
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
// Main function
int main() {
int arr[] = { 6, 5, 12, 10, 9, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
merge_sort(arr, 0, n);
cout << "The sorted array is: " << endl;
printArray(arr, n);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.