Merge sort not showing correct output - c

I have implemented each line of merge sort algorithm-wise and can't find error.
al and ar are left and right sub arrays. Arrays are passed along with size.
#include<stdio.h>
#include<conio.h>
void mergesort(int a[] ,int);
void merge(int al[],int,int ar[],int,int a[]);
int main()
{
int i,n;
printf("Enter the no of elements to be sorted\n");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
mergesort(a,n);
printf("\nThe elements after sorting are:");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
getch();
return 0;
}
void mergesort(int a[],int size)
{
int i,n=size,mid;
if(n<2)
return;
mid=n/2;
int left[mid],right[n-mid];
for(i=0;i<mid;i++)
left[i]=a[i];
for(i=mid;i<n;i++)
right[i]=a[i];
mergesort(left,mid);
mergesort(right,n-mid);
merge(left,mid,right,n-mid,a);
}
void merge(int al[],int sl,int ar[],int sr,int a[])
{
int i=0,j=0,k=0;
while(i<sl && j<sr)
{
if(al[i]<=ar[j])
{
a[k]=al[i];
i++;
}
else
{
a[k]=ar[j];
j++;
}
k++;
}
while(i<sl)
{
a[k]=al[i];
i++;
k++;
}
while(j<sr)
{
a[k]=ar[j];
j++;
k++;
}
}
input:
no of elements:4
5 6 3 1
output:
5 16 16 16

Look at this part of your code:
int left[mid],right[n-mid];
for(i=0;i<mid;i++)
left[i]=a[i];
for(i=mid;i<n;i++)
right[i]=a[i];
You are accessing the right array with indexes beyond the array bounds. That should be something like this instead:
right[i - mid]=a[i];

I have correct code for merge sort written in C. Please tally with it.
#include<stdio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50];
int i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1 && j <=j2)
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}

Related

Why does this code of merge and merge sort algorithm does not work?

1.Can you help me in looking for the error in the code?
'This is the merge function.'
#include<stdio.h>
#include<stdlib.h>
void merge(int arr[],int left,int mid,int right)
{
int i,j,k;/*FOR USING IN THE FOR LOOP*/
int n1,n2;/*For the number of elements*/
n1=mid-left+1;
n2=right-mid;
int a1[n1],a2[n2];
for(i=0;i<n1;i++)
{
a1[i]=arr[left+i];
}
for(j=0;j<n2;j++)
{
a2[i]=arr[mid+1+j];
}
i=0;
j=0;
k=left;
while(i<n1 && j<n2)
{
if(a1[i]<=a2[j])
{
arr[k]=a1[i];
i++;
}
else
{
arr[k]=a2[j];
j++;
}
k++;
}
while(i<n1)
{
arr[k]=a1[i];
i++;
k++;
}
while(j<n2)
{
arr[k]=a2[j];
j++;
k++;
}
}
'This is the mergesort function'
void mergesort(int arr[],int left,int right)
{
if(left<right)
{
int mid=left+(right-left)/2;
mergesort(arr,left,mid);
mergesort(arr,mid+1,right);
merge(arr,left,mid,right);
}}
'The Main Function'
void main()
{
int n,i;
printf("Enter the number of elements in the array:");
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
printf("Enter the Element %d:",i+1);
scanf("%d",&arr[i]);
}
mergesort(arr,0,n-1);
printf("The Elements in the array sorted are:");
for(i=0;i<n;i++)
{
printf("%d->",arr[i]);
}
}
I could not find error in this code but the compiler could not compile the code and give a valid solution of the algorithm.
So pls help me to find the solution in this code.

MergeSort giving Segmentation fault

I used this merge sort program to create my own program:
https://gist.github.com/mycodeschool/9678029
my program gives me segmentation fault 11. I can't seem to figure out what's wrong. Please help me.
my program:
#include<stdio.h>
int a[20],n, nl, nr,left[20],right[20];
void merge(int *l, int *r, int *x);
void mergesort(int *x, int v);
void main()
{
int i;
printf("Enter the number of the elements \n");
scanf("%d", &n);
printf("Enter the array elements \n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("The array before sorting: \n");
for(i=0; i<n; i++)
printf("%d \n", a[i]);
mergesort(a,n);
printf("The array after sorting: \n");
for(i=0; i<n; i++)
printf("%d \n", a[i]);
}
void merge(int *l, int *r, int *x)
{
int i=0, j=0, k=0;
while(i<nl && j<nr)
{
if(l[i]<=r[j])
{
x[k]=l[i];
i++;
}
else
{
x[k]=r[j];
j++;
}
k++;
}
while(i<nl)
{
x[k]=l[i];
i++;
k++;
}
while(j<nr)
{
x[k]=r[j];
j++;
k++;
}
}
void mergesort(int *x, int v)
{
int mid,i;
if(n<2)
return;
mid=n/2;
nl=mid;
nr= n-mid;
for(i=0; i<mid; i++)
left[i]=x[i];
for(i=mid; i<n; i++)
right[i-mid]= x[i];
mergesort(left, mid);
mergesort(right,v-mid);
merge(left, right, a);
}
I would really appreciate if someone helped me. Thanks!
here is an example of a merge sort program, easily found via a google of the web.
Some comparison should show you where your code went wrong.
#include<stdio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid); //left recursion
mergesort(a,mid+1,j); //right recursion
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50]; //array used for merging
int i,j,k;
i=i1; //beginning of the first list
j=i2; //beginning of the second list
k=0;
while(i<=j1 && j<=j2) //while elements in both lists
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1) //copy remaining elements of the first list
temp[k++]=a[i++];
while(j<=j2) //copy remaining elements of the second list
temp[k++]=a[j++];
//Transfer elements from temp[] back to a[]
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}

C program for quicksort(recursive)

The following c program is a quicksort recursive program. Although I have written this code according to the Cormen explanation, yet it is incorrectly sorting the input.For example it is sorting the input 3,8,1 to 3,1,8. Thanks a lot in advance for finding the mistake
#include<stdio.h>
void printa(int a[],int size)
{
int i;
printf("\n");
for(i=0;i<size;i++)
{
printf("%d\n",a[i]);
}
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int partition(int a[],int p,int r)
{
int i,j,x;
i=p-1;
x=a[r];
for(j=p;j<r;j++)
{
if(a[j]<=x)
{
i=i+1;
swap(&a[i],&a[j]);
}
swap(&a[i+1],&a[r]);
}
return i+1;
}
void quicksort(int a[],int p,int r)
{
if(p<r)
{
int q;
q=partition(a,p,r);
quicksort(a,p,q);
quicksort(a,q+1,r);
}
}
main()
{
int a[50],i,size;
printf("enter the size of the array\n");
scanf("%d",&size);
printf("enter the elements of the array\n");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
quicksort(a,0,size-1);
printa(a,size);
}

Selection sort on array in C

I'm trying to create a simple(?) selection sort program in C that selects the largest integer of an integer array and places it in the location a[n-1], places the second largest number in a[n-2], etc until the smallest number is placed in a[0]. I've run through the below code on paper and it seems like it should work, but when I compile it I'm getting faulty results. Am I missing something obvious?
/* The program implements selection sort*/
#include <stdio.h>
#include "simpio.h"
#define n 5
void GetArray(int a[]);
void SelectionSort(int a[]);
int FindMax(int a[], int high);
void swap(int a[], int p1, int p2);
void PrintArray(int a[]);
main()
{
int a[n];
GetArray(a);
SelectionSort(a);
PrintArray(a);
getchar();
}
void GetArray(int a[])
{
int i;
for(i=0;i<n;i++)
{
printf("Enter integer# %d", i+1);
a[i]=GetInteger();
}
}
void SelectionSort(int a[])
{
int i, max;
for(i=0;i<n;i++)
{
max=FindMax(a,i);
swap(a,max,(n-1-i));
}
}
int FindMax(int a[], int high)
{
int i, index;
index=high;
for(i=high;i<n;i++)
{
if(a[i]>a[index])
index=i;
}
return index;
}
void swap(int a[], int p1, int p2)
{
int temp;
temp=a[p2];
a[p2]=a[p1];
a[p1]=temp;
}
void PrintArray(int a[])
{
int i;
for(i=0;i<n;i++)
printf("a[%d]=%d\n", i, a[i]);
}
Change these method to:
void SelectionSort(int a[])
{
int i, max;
for(i=0;i<n;i++)
{
max=FindMax(a,n-i-1);
swap(a,max,n-i-1);
}
}
int FindMax(int a[], int high)
{
int i, index;
index=high;
for(i=0;i<high;i++)
{
if(a[i]>a[index])
index=i;
}
return index;
}
I actually tested my answer and it works.
Selection sort is process of comparing minimum element from the list and placing from the least index.
Now consider below code snippet.
public void selectionSort(int[] elements) {
for(int i=0;i<elements.length;i++) {
int minPosition = i;
for(int j=i+1;j<elements.length;j++) {
if(elements[minPosition]>elements[j])
minPosition = j;
}
int temp = elements[i];
elements[i] = elements[minPosition];
elements[minPosition] = temp;
}
}
Thanks for reading, let me know feedback to improve from myside
Shouldn't:
max=FindMax(a,i);
swap(a,max,(n-1-i));
Be:
max=FindMax(a,i);
swap(a,max,i);
otherwise, next time through the loop, you'll find the same max value in the top position in the array.
A very basic implementation of selection sort
#include<stdio.h>
main()
{
int i,j,n=7,a[]={1,2,5,3,8,9,5},key;
for(j=1;j<n;j++)
{
key=a[j]; //a[j] is the new element to be added to the sorted
//sequence
i=j-1;
while(i>=0 && key<a[i]) //traverse through the sorted sequence
{a[i+1]=a[i];i--;} //until the place of key is found
a[i+1]=key;
}
for (j=0;j<n;j++)
printf("%d",a[j]);
}
#include<stdio.h>
#include<conio.h>
int removex(int arr[],int small,int n)
{
int i=0;
for(;i<n;i++)
if(arr[i]==small) //searching that no to delete
break;
for(;i<n-1;i++)
arr[i]=arr[i+1]; //delete by overloading no
return n-1;
}
void selectSort(int arr[],int sort[],int n)
{
int j=0,k=0,small;
while(n!=0)
{
small=arr[0];
for(j=0;j<n;j++)
if(arr[j]<small)
small=arr[j]; //finding smallest no
sort[k++]=small;
n=removex(arr,small,n); //removing that from list as we included that no into sorted list
}
}
void main()
{
int arr[10],arr2[10],i,n;
clrscr();
printf("Enter how many elements");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
selectSort(arr,arr2,n);
printf("sorted list is\n");
for(i=0;i<n;i++)
printf("%d\n",arr2[i]);
getch();
}

MergeSort - Compiling but not giving any output

This is based on the algorithm given in the Cormen's book. What am I doing wrong?
#include <stdio.h>
#include <conio.h>
void mergesort(int a[],int,int);
void merge(int a[],int,int,int);
int main()
{
int i,num,a[50];
printf("Enter the number of elements : ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
printf("\n%d) ",i);
scanf("%d",&a[i]);
}
mergesort(a,1,num);
for(i=1;i<=num;i++)
{
printf("SoRTED ArrAy \n");
printf("\n%d) ",a[i]);
}
getch();
return 0;
}
void mergesort(int a[],int i, int k)
{
int j;
j=(i+k)/2;
while(i<k)
{
mergesort(a,i,j);
mergesort(a,j+1,k);
merge(a,i,j,k);
}
}
void merge(int a[],int p,int q,int r)
{
int i,j,k,n1,n2;
n1=q-p+1;
n2=r-q;
int l[n1],s[n2];
for(i=1;i<=n1;i++)
{
l[i]=a[p+i-1];
}
for(j=1;i<=n2;j++)
{
s[j]=a[q+j];
}
l[n1+1]=1000;
s[n2+1]=1000;
i=1;
j=1;
for(k=p;k<=r;k++)
{
if(l[k]<s[k])
{
a[k]=l[i];
i++;
}
else
{
a[k]=s[j];
j++;
}
}
}
The main issue is that i and k are never updated inside your while loop in the mergesort function, leading to an infinite loop. It's likely that you don't even need the while loop.
void mergesort(int a[],int i, int k)
{
int j;
j=(i+k)/2;
while(i<k)
{
mergesort(a,i,j);
mergesort(a,j+1,k);
merge(a,i,j,k);
}
}
Nothing inside the while changes the value of i or k. So if i<k, the loop will repeat forever.

Resources