Skip to content

Merge sort.c#1

Open
AndrejPetelin wants to merge 2 commits intomasterfrom
merge_sort.c
Open

Merge sort.c#1
AndrejPetelin wants to merge 2 commits intomasterfrom
merge_sort.c

Conversation

@AndrejPetelin
Copy link

Reviewing.

Copy link
Author

@AndrejPetelin AndrejPetelin left a comment

Choose a reason for hiding this comment

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

Some hints


int temp[SIZE] = {0};

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)


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).


#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.

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];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants