简体   繁体   中英

Merge Sort Algorithm in C not working properly

I am trying to implement the merge sort algorithm in C. I understand how the algorithm and logic is supposed to function however I have been coming across some difficulties with the direct implementation.

I am aware that there are many examples for Merge Sorting online and I have also looked at some StackOverflow posts for similar problems. However, I was hoping someone could help me understand why my code does not seem to run correctly.

My code is below:

#include <stdio.h>
#include <stdlib.h>

// Function to Merge Arrays L and R into A
// leftCount = number of elements in L
// rightCount = number of elements in R

void Merge(int *A,int *L,int leftCount, int *R, int rightCount)
{

// i, to mark the index of subarray (L)
// j, to mark the index of subarray (R)
// k, to mark the index of subarray (A)

int i = 0;
int j = 0;
int k = 0;

while(i<leftCount && j<rightCount)
{
    if(L[i] <= R[j])
    {
        A[k++] = L[i++];
    }
    else
    {
        A[k++] = R[j++];
    }
}
while(i<leftCount)
 {
    A[k++] = L[i++];
 }
while(j<rightCount)
 {
    A[k++] = R[j++];
 }
}

// Recursive function to sort an array of integers

void MergeSort(int *A, int n)  
{
int i;
int mid;
int *L;
int *R;
if (n<2) // Base condition
  {
    return;
  }

mid = n/2; // Find the mid index
L = (int*)malloc(mid*sizeof(int));
R = (int*)malloc((n-mid)*sizeof(int));

 for(i=0;i<mid;i++) // Creating left subarray
  {
    L[i] = A[i];
  }
 for(i=mid;i<n;i++) // Creating right subarray
  {
    R[i-mid] = A[i];
  }

MergeSort(L,mid);
MergeSort(R,n-mid);
Merge(A,L,R,mid,n-mid);
free(L);
free(R);
}

 int main() 
{

int A[] = {2,4,1,6,8,5,3,7};
int i;
int numberofelements;
numberofelements = sizeof(A)/sizeof(A[0]);
MergeSort(A,8);

 for(int i = 0; i<8; i++)
  {
    printf("%d ",A[i]);
    return 0;
  }
}

After I run this code I only seem to get an output of '1,' and not a sorted array. I was really hoping someone could help me out.

Your Merge() signature does not match how you invoke it:

Signature:

void Merge(int *A,int *L,int leftCount, int *R, int rightCount)

Invokation:

Merge(A,L,R,mid,n-mid);

This causes undefined behavior when you parse (and later use) a pointer ( R ) as an integer ( leftCount ), and an integer ( mid ) as a pointer ( R ).

Pretty sure your compiler would have given you a warning about it, make sure you turn warnings on, your compiler usually knows what he's saying :)

Get yourself used to compile with -Wall (if you're using gcc). If you did so, you would have seen that you invoke Merge() with the wrong arguments. It should be:

Merge(A,L,mid,R,n-mid);

Also, you shouldn't return from inside the loop that prints the array elements. This is why you only see a 1 . Look at the code carefully: the loop body returns unconditionally from main() , so it will only execute once. Move the return out of the loop:

for(i = 0; i<8; i++)
{
    printf("%d ",A[i]);
}

return 0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM