Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions merger_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <stdio.h>

#define SIZE 8

int temp[SIZE] = {0};
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure a global array is the best idea, especially since this is going to be constant size. What you probably want is a local temporary array/arrays within merge function.


void merge (int array[], int start_1, int end_1, int start_2, int end_2)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I'd opt for two arrays, instead of start_1, start_2, end_1, end_2. Something along the lines of
void merge (int left[], int leftSz, int right[] int rightSz)

{
int i = 0;

// while there are elements in both subarrays
while ((start_1 <= end_1) && (start_2 <= end_2))
{
// compare numbers at the start of the subarrays
if (array[start_1] > array[start_2])
{
// append smaller to array
temp[i] = array[start_2];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

temp is, in theory, a pointer to a global array with one int element. Make the size of the original array in main larger and something will break.

You're probably looking for something along the line of
int temp[end_2 - start_1 + 1];

start_2++;
i++;
}
else
{
temp[i] = array[start_1];
start_1++;
i++;
}
}

// while elements remain in subbarray 2
while ((start_1 > end_1) && (start_2 <= end_2))
{
// append elements to array
temp[i] = array[start_2];
i++;
start_2++;
}

// while elements remain in subbarray 1
while ((start_2 > end_2) && (start_1 <= end_1))
{
// append elements to array
temp[i] = array[start_1];
i++;
start_1++;
}

//copy sorted values from the temp back to original array
for (i = 0; i < SIZE; i++)
{
array[i] = temp[i];
}

}

void sort (int array[], int start, int end)
{
if (end > start)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (end > start + 1) should be fine, one element is sorted by definition (though in reality this just means n extra steps, not too big a problem).

{
int middle = (start + end) / 2;

// sort left half
sort(array, start, middle);

// sort right half
sort(array, middle + 1, end);

// merge the two halves
merge(array, start, middle, middle + 1, end);
}
}

int main(void)
{
int numbers[SIZE] = { 4, 15, 16, 50, 8, 23, 42, 108};

for (int i = 0; i < SIZE; i++)
{
printf("%i ", numbers[i]);
}
printf("\n");

sort(numbers, 0, SIZE - 1);

/*
//copy sorted values from the temp array back to the original array
for (int i = 0; i < SIZE; i++)
{
numbers[i] = temp[i];
}
*/

for (int i = 0; i < SIZE; i++)
{
printf("%i ", numbers[i]);
}
printf("\n");
}