1: #include<stdio.h>
2: #include<conio.h>
3: #define MAX 20
4: void mergesort(int a[],int low,int high);
5: void merge(int a[],int beg,int mid,int end);
6: void display(int a[],int num);
7: void main()
8: {
9: int n,i,a[MAX];
10: clrscr();
11: printf("Enter the number of elements (<MAX )\n");
12: scanf("%d",&n);
13: printf("Enter the elements\n");
14: for(i=0;i<n;i++)
15: {
16: scanf("%d",&a[i]);
17: }
18: printf("\nArray elements before sorting are:\n");
19: display(a,n);
20: mergesort(a,0,n-1);
21: printf("\nArray elements after sorting are:\n");
22: display(a,n);
23: getch();
24: }
25: void mergesort(int a[],int low,int high)
26: {
27: int mid;
28: if(low<high)
29: {
30: mid=(low+high)/2;
31: mergesort(a,low,mid);
32: mergesort(a,mid+1,high);
33: merge(a,low,mid,high);
34: }
35: }
36: void merge(int a[],int beg, int mid,int end)
37: {
38: int temp,b[MAX],left,right,i;
39: left=beg;
40: right=mid+1;
41: temp=beg;
42: while((left<=mid) && (right<=end))
43: {
44: if(a[left]<a[right])
45: {
46: b[temp]=a[left];
47: left=left+1;
48: }
49: else
50: {
51: b[temp]=a[right];
52: right=right+1;
53: }
54: temp=temp+1;
55: }
56: while(left<=mid)
57: {
58: b[temp]=a[left];
59: left=left+1;
60: temp=temp+1;
61: }
62: while(right<=end)
63: {
64: b[temp]=a[right];
65: right=right+1;
66: temp=temp+1;
67: }
68: for(i=beg;i<=end;i++)
69: {
70: a[i]=b[i];
71: }
72: }
73: void display(int a[],int num)
74: {
75: int i;
76: for(i=0;i<num;i++)
77: {
78: printf("%d\t",a[i]);
79: }
80: }
getsprogramming Tutorials , C Programming, CPP Programming and JAVA Programming , Python Programming , Javascript Programming Welcome to getsprogramming Blog Here You Find Different Programs in Easier way to Explain and gain Knowledge and Try Your Self..Thanks
Tuesday, 29 September 2015
Write a C Program That performing Merge Sort(Sorting Techniues)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment