0% found this document useful (0 votes)
36 views1 page

"/N Array of Elements Before Sorting Are:/n" "%D " "/N The Elements After Sorting Are:/n" "%D "

This C program implements merge sort to sort an array of integers. It contains functions to recursively split the array, sort the halves, and then merge the sorted halves. The main function initializes an array, calls the merge sort function to sort it, and prints the array before and after sorting. The merge sort function recursively splits the array in half until single elements remain, then calls the merge function to merge the sorted halves back together.

Uploaded by

mn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views1 page

"/N Array of Elements Before Sorting Are:/n" "%D " "/N The Elements After Sorting Are:/n" "%D "

This C program implements merge sort to sort an array of integers. It contains functions to recursively split the array, sort the halves, and then merge the sorted halves. The main function initializes an array, calls the merge sort function to sort it, and prints the array before and after sorting. The merge sort function recursively splits the array in half until single elements remain, then calls the merge function to merge the sorted halves back together.

Uploaded by

mn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1 /*

2 * merge sort.c
3 */
4
5
6 #include<stdio.h>
7 #include<stdlib.h>
8
9 int a[]={54,26,93,17,77,31,44,55,20,12,100};
10 int n=sizeof(a)/sizeof(a[0]);
11
12 void merge(int a[],int low,int mid,int high);
13 void merge_sort(int a[],int low,int high);
14 int main()
15 {
16 int i=0;
17 printf("\n array of elements before sorting are:\n");
18 for(i=0;i<n;i++)
19 printf("%d ",a[i]);
20 merge_sort(a,0,n-1);
21 printf("\n the elements after sorting are:\n");
22 for(i=0;i<n;i++)
23 printf("%d ",a[i]);
24 return 0;
25 }
26 //Splitting the array at middle
27 void merge_sort(int a[],int low,int high)
28 {
29 int mid;
30 if(low<high)
31 {
32 mid=(low+high)/2;
33 merge_sort(a,low,mid);
34 merge_sort(a,mid+1,high);
35 merge(a,low,mid,high);
36 }
37 }
38 //after checking smaller items merging each sub array that split before
39 void merge(int a[], int low, int mid, int high)
40 {
41 int b[n];
42 int i = low, j = mid + 1, k = 0;
43
44 while (i <= mid && j <= high) {
45 if (a[i] <= a[j])
46 b[k++] = a[i++];
47 else
48 b[k++] = a[j++];
49 }
50 while (i <= mid)
51 b[k++] = a[i++];
52
53 while (j <= high)
54 b[k++] = a[j++];
55
56 k--;
57 while (k >= 0) {
58 a[low + k] = b[k];
59 k--;
60 }
61 }
62

You might also like