[英]merge_sort not working, output is just a bunch of 1 -1 and 0
So I got the task to write merge_sort
in a recursive way, and it just returns either an array of 0,-1, 1
of the same length as the original input.所以我得到了以递归方式编写merge_sort
的任务,它只返回与原始输入长度相同的0,-1, 1
数组。 Any ideas where I did something wrong?任何想法我做错了什么? input_merge_sort.h and input_merge_sort.c are given by the task and handle the input and output, so all I have to focus on is the algorithm itself. input_merge_sort.h和input_merge_sort.c是由任务给出的,处理输入和输出,所以我只需要关注算法本身。 Some details about the algorithm, to make sure I understood it correctly:关于算法的一些细节,以确保我正确理解它:
MergeSort sorts lists by splitting them into equally sized lists, splitting them until they're single elements, to then 2 single-element lists together, comparing them and putting the smaller one in front. MergeSort 通过将列表分成大小相等的列表来对列表进行排序,将它们拆分直到它们是单个元素,然后将 2 个单元素列表放在一起,比较它们并将较小的放在前面。 With the sub-lists you write into the original list by reading from 2 sublists, comparing the value and putting pointer 1 element further, to then compare it with the old element of the other sublist, which was bigger than the other.对于子列表,您通过从 2 个子列表中读取,比较值并将指针 1 元素进一步写入原始列表,然后将其与另一个子列表的旧元素进行比较,该元素大于另一个。
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "input_merge_sort.h"
/*
array: Pointer at the start of the array
first: Index of the first element
len : Index of the last element
*/
void merge(int a[], int i1, int j1, int j2) {
int temp[j2 - i1]; //array used for merging
int i, j, k;
i = i1; //beginning of the first list
int i2 = j1 + 1;
j = i2; //beginning of the second list
k = 0;
while (i <= j1 && j <= j2) { //while elements in both lists
if (a[i] < a[j])
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
while (i <= j1) //copy remaining elements of the first list
temp[k++] = a[i++];
while (j <= j2) //copy remaining elements of the second list
temp[k++] = a[j++];
//Transfer elements from temp[] back to a[]
for (i = i1, j = 0; i <= j2; i++, j++)
a[i] = temp[j];
}
void merge_sort(int *array, int first, int last) {
int middle;
if (first < last) {
middle = ((first + last) / 2);
merge_sort(array, first, middle);
merge_sort(array, middle + 1, last);
merge(array, first, middle, last);
}
}
/*
Reads integers from files and outputs them into the stdout after mergesorting them.
How to run: ./introprog_merge_sort_rekursiv <max_amount> <filepath>
*/
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("usage: %s <max_amount> <filepath>\n", argv[0]);
exit(2);
}
char *filename = argv[2];
// Initialize array
int *array = (int*)malloc(atoi(argv[1]) * sizeof(int)); //MINE
int len = read_array_from_file(array, atoi(argv[1]), filename);
printf("Input:\n");
print_array(array, len);
// Call of "merge_sort()"
merge_sort(array, array[0], array[len - 1]); //MINE
printf("Sorted:\n");
print_array(array, len);
free(array);
return 0;
}
The function merge_sort
takes an array and the indices of its first and last elements as arguments, but you pass the elements themselves.函数merge_sort
接受一个数组及其第一个和最后一个元素的索引作为参数,但您传递元素本身。 Change:改变:
merge_sort(array, array[0],array[len-1]);
to:到:
merge_sort(array, 0, len - 1);
In merge
you crate a temporary array on the stack, but it is one element short.在merge
您在堆栈上创建一个临时数组,但它是一个元素短。 It should be:它应该是:
int temp[j2 - i1 + 1];
I recommend that you change the functions so that they don't take the last element as upper bound but the first element outside the range, as is usual in C arrays and loops.我建议您更改函数,以便它们不将最后一个元素作为上限,而是将第一个元素作为范围之外的元素,这在 C 数组和循环中很常见。 In my opinion, that makes the code simpler.在我看来,这使代码更简单。 The two halves of the array are then [low, mid)
and [mid, high)
.然后数组的两半是[low, mid)
和[mid, high)
。 The length of the whole array is high - low
.整个数组的长度为high - low
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.