Next time, please post your code here, in [code][/code] tags. Make sure you post as plain text (our forums will provide syntax highlighting), and that your code is properly formatted. It should look like
Code:
#include <stdio.h>
#include <stdlib.h>
/*
* bottoms up merge sort procedural algorithm : http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/7-Sort/merge-sort5.html
* */
void printArray(int *arr, int len) {
for (int i = 0; i < len; i++) {
fprintf(stdout, " %d", arr[i]);
}
}
int getBigger(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
void merge(int *a, int iLeft, int iMiddle, int iRight, int tmp[]) {
int i, j, k;
i = iLeft;
j = iMiddle;
k = iLeft;
while (i < iMiddle || j < iRight) {
if (i < iMiddle && j < iRight) { // Both array have elements
if (a[i] < a[j])
tmp[k++] = a[i++];
else
tmp[k++] = a[j++];
} else if (i == iMiddle)
tmp[k++] = a[j++]; // a is empty
else if (j == iRight)
tmp[k++] = a[i++]; // b is empty
}
}
void sort(int *a, int *tmp, int length) {
int width;
for (width = 1; width < length; width = 2 * width) {
// Combine sections of array a of width "width"
int i;
for (i = 0; i < length; i = i + 2 * width) {
int left, middle, right;
left = i;
middle = i + width;
right = i + 2 * width;
merge(a, left, middle, right, tmp);
}
/* ========================================================
Instead of doing multiply copy operations inside merge()
we perform ONE copy operation after ALL mereg operations
======================================================== */
for (i = 0; i < length; i++)
a[i] = tmp[i];
}
}
int main(void) {
int array[10] = { 4, 3, 6, 1, 14, 2, 12, 4, 10, 3 };
int temp[10];
int length = 10;
printArray(&array[0], 10);
sort(&array[0], &temp[0], length);
printArray(&array[0], 10);
return 0;
}
Now, are you sure you get no output? Here's what I get when I run your code:
Code:
$ make sort
gcc -Wall -ggdb3 -pedantic -std=c99 -O0 -o sort sort.c -lm -lpthread -lrt
$ ./sort
Segmentation fault (core dumped)
I get a seg fault. That means the program doesn't finish properly, so all output buffers aren't flushed. This is important because stdout is line buffered, meaning it generally doesn't actually send the output to the terminal (it buffers it) until it sees a newline. If you print a newline at the end of printArray, (e.g. putchar('\n')) you should see this:
Code:
$ ./sort
4 3 6 1 14 2 12 4 10 3
1 1 2 2 3 4 4 6 12 14
Segmentation fault (core dumped)
Use your debugger to figure out why you have repeated elements and why you got a seg fault (most likely array out of bounds).
EDIT: If you wish to add debugging output, use stderr, which is unbuffered, meaning the output shows up immediately, regardless of presence of a newline. But that should be separate from normal program output. Don't output regular messages to stderr, they belong on stdout.