I'm learning algorithms from Cormen and Co. and I have problem with implementation of merge sort from their pseudocode. I compiled it by:
$ gcc -Wall -g merge_sort.c
I have a problem because for numbers:
2 4 5 7 1 2 3 6
The result is:
1 2 2 3 3 4 5 5
I tried to read carefully the pseudo code but this does not help me. I want to know what I'm doing wrong. Below is my code:
#include <stdio.h>
#define SIZE 8
void merge(int *array_of_integers, int p, int q, int r) {
int n1 = q - p + 1;
int n2 = r - q;
int i, j, k;
int left_array[n1 + 1];
int right_array[n2 + 1];
for (i = 0; i < n1; i++)
left_array[i] = array_of_integers[p + i];
for (j = 0; j < n2; j++)
right_array[j] = array_of_integers[q + j];
i = 0;
j = 0;
for (k = p; k < r; k++){
if (left_array[i] <= right_array[j]) {
array_of_integers[k] = left_array[i];
i++;
} else {
array_of_integers[k] = right_array[j];
j++;
}
}
}
void merge_sort(int *array_of_integers, int p, int r) {
if (p < r) {
int q = (p + r) / 2;
merge_sort(array_of_integers, p, q);
merge_sort(array_of_integers, q + 1, r);
merge(array_of_integers, p, q, r);
}
}
void print_array(int *array_of_integers, int amout_of_integers) {
int i;
for(i = 0; i < amout_of_integers; i++)
printf("%d ", array_of_integers[i]);
puts("");
}
int main(void) {
int dataset[] = { 2, 4, 5, 7, 1, 2, 3, 6 };
print_array(dataset, SIZE);
merge_sort(dataset, 0, SIZE);
print_array(dataset, SIZE);
return 0;
}
Edit: (Correct solution)
void merge(int *array_of_integers, int p, int q, int r) {
int n1 = q - p + 1;
int n2 = r - q;
int i, j, k;
int left_array[n1 + 1];
int right_array[n2 + 1];
left_array[n1] = 123456798;
right_array[n2] = 123456798;
for (i = 0; i < n1; i++)
left_array[i] = array_of_integers[p + i];
for (j = 0; j < n2; j++)
right_array[j] = array_of_integers[q + j + 1];
i = 0;
j = 0;
for (k = p; k <= r; k++) {
if (left_array[i] <= right_array[j]) {
array_of_integers[k] = left_array[i];
i++;
} else {
array_of_integers[k] = right_array[j];
j++;
}
}
}
void merge_sort(int *array_of_integers, int p, int r) {
if(p < r) {
int q = (p + r) / 2;
merge_sort(array_of_integers, p, q);
merge_sort(array_of_integers, q + 1, r);
merge(array_of_integers, p, q, r);
}
}
123456798
? You should probably disregard this reference book. – Overstrain