Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bbeb640

Browse files
authored
Merge pull request fnplus#570 from aryankharbanda/master
updated merge sort with better code and comments
2 parents fb08e80 + 45ca0af commit bbeb640

File tree

1 file changed

+86
-56
lines changed

1 file changed

+86
-56
lines changed
Lines changed: 86 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,93 @@
1-
#include<stdio.h>
1+
#include<stdlib.h>
2+
#include<stdio.h>
3+
4+
void merge(int arr[], int l, int m, int r)
5+
{
6+
int i, j, k;
7+
int n1 = m - l + 1;
8+
int n2 = r - m;
9+
//merges two subarrays of arr[], i.e. arr[l..m] and arr[m+1..r]
10+
11+
int L[n1], R[n2];
12+
//created twwo temp arrays
213

3-
void mergesort(int a[],int i,int j);
4-
void merge(int a[],int i1,int j1,int i2,int j2);
14+
for (i = 0; i < n1; i++)
15+
L[i] = arr[l + i];
16+
for (j = 0; j < n2; j++)
17+
R[j] = arr[m + 1+ j];
18+
//copies data to arrays
19+
20+
i = 0; //first subarray
21+
j = 0; //econd subarray
22+
k = l; //merged subarray
523

6-
int main()
7-
{
8-
int a[30],n,i;
9-
printf("Enter no of elements:");
10-
scanf("%d",&n);
11-
printf("Enter array elements:");
24+
while (i < n1 && j < n2) // merges the temp arrays back to arr[l..r]
25+
{
26+
if (L[i] <= R[j])
27+
{
28+
arr[k] = L[i];
29+
i++;
30+
}
31+
else
32+
{
33+
arr[k] = R[j];
34+
j++;
35+
}
36+
k++;
37+
}
38+
1239

13-
for(i=0;i<n;i++)
14-
scanf("%d",&a[i]);
40+
while (i < n1) //copies remaining elements of L[]
41+
{
42+
arr[k] = L[i];
43+
i++;
44+
k++;
45+
}
46+
47+
while (j < n2) //copies remaining elements of r[]
48+
{
49+
arr[k] = R[j];
50+
j++;
51+
k++;
52+
}
53+
}
54+
1555

16-
mergesort(a,0,n-1);
56+
void mergeSort(int arr[], int l, int r) // L is left index and r is right index of sub array to be sorted
57+
{
58+
if (l < r)
59+
{
1760

18-
printf("\nSorted array is :");
19-
for(i=0;i<n;i++)
20-
printf("%d ",a[i]);
61+
int m = (l+r)/2;
62+
63+
// Sort first and second halves
64+
mergeSort(arr, l, m);
65+
mergeSort(arr, m+1, r);
66+
67+
merge(arr, l, m, r);
68+
}
69+
}
2170

22-
return 0;
23-
}
71+
int main()
72+
{
73+
//scanning array to sort
74+
printf("Enter size of array to be sorted \n");
75+
int arr_size;
76+
scanf("%d", &arr_size);
77+
int arr[arr_size];
78+
printf("Enter array \n");
79+
int i;
80+
for(i=0;i<arr_size;i++)
81+
scanf("%d", &arr[i]);
82+
83+
//to sort array, we call function
84+
mergeSort(arr, 0, arr_size - 1);
85+
86+
//printing final sorted array
87+
printf("\nSorted array is \n");
88+
for (i=0; i < arr_size; i++)
89+
printf("%d ", arr[i]);
90+
printf("\n");
2491

25-
void mergesort(int a[],int i,int j)
26-
{
27-
int mid;
28-
29-
if(i<j)
30-
{
31-
mid=(i+j)/2;
32-
mergesort(a,i,mid); //left recursion
33-
mergesort(a,mid+1,j); //right recursion
34-
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
35-
}
36-
}
37-
38-
void merge(int a[],int i1,int j1,int i2,int j2)
39-
{
40-
int temp[50]; //array used for merging
41-
int i,j,k;
42-
i=i1; //beginning of the first list
43-
j=i2; //beginning of the second list
44-
k=0;
45-
46-
while(i<=j1 && j<=j2) //while elements in both lists
47-
{
48-
if(a[i]<a[j])
49-
temp[k++]=a[i++];
50-
else
51-
temp[k++]=a[j++];
52-
}
53-
54-
while(i<=j1) //copy remaining elements of the first list
55-
temp[k++]=a[i++];
56-
57-
while(j<=j2) //copy remaining elements of the second list
58-
temp[k++]=a[j++];
59-
60-
//Transfer elements from temp[] back to a[]
61-
for(i=i1,j=0;i<=j2;i++,j++)
62-
a[i]=temp[j];
63-
}
92+
return 0;
93+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /