Merge Sort

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  int a[10],b[10], k[20], first, second, third, n_a, n_b, n;  
4:  void main()  
5:  {  
6:         int i, l;  
7:         void sort();  
8:         clrscr();  
9:         printf("Enter the number of elements : ");  
10:         scanf("%d", &n_a);  
11:         printf("\nEnter the elements in the sorted manner : \n");  
12:         for(i=1;i<=n_a;i++)  
13:                scanf("%d", &a[i]);  
14:         printf("Enter the number of elements : ");  
15:         scanf("%d", &n_b);  
16:         printf("\nEnter the elements in the sorted manner : \n");  
17:         for(i=1;i<=n_b;i++)  
18:                scanf("%d", &b[i]);  
19:         n=n_a+n_b;  
20:         l=1;  
21:         for(i=1;i<=n_a;i++)  
22:         {  
23:                k[l]=a[i];  
24:                l++;  
25:         }  
26:         for(i=1;i<=n_b;i++)  
27:         {  
28:                k[l]=b[i];  
29:                l++;  
30:         }  
31:         first=1;  
32:         second=n_a+1;  
33:         third=n;  
34:         sort(k, first, second, third);  
35:    getch();  
36:  }  
37:  void sort()  
38:  {  
39:         int i=first, j=second, l=0, temp[20];  
40:         void display(int temp[20], int i, int j, int l);  
41:         clrscr();  
42:         while((i<second)&&(j<=third))  
43:         {  
44:                if(k[i]<=k[j])  
45:                {  
46:                   temp[l]=k[i];  
47:                   l++;  
48:                   i++;  
49:                   display(temp, i, j, l);  
50:                }  
51:                else  
52:                {  
53:                   temp[l]=k[j];  
54:                   l++;  
55:                   j++;  
56:                   display(temp, i, j, l);  
57:                }  
58:         }  
59:         if(i>=second)  
60:         {  
61:                while(j<=third)  
62:                {  
63:                  temp[l]=k[j];  
64:                  l++;  
65:                  j++;  
66:                  display(temp, i, j, l);  
67:                }  
68:         }  
69:         else  
70:         {  
71:                while(i<second)  
72:                {  
73:                   temp[l]=k[i];  
74:                   l++;  
75:                   i++;  
76:                   display(temp, i, j, l);  
77:                }  
78:         }  
79:         printf("\n\n\t\t\tSORTED ARRAY\n\n\t\t");  
80:        for(i=0;i<l;i++)  
81:         {  
82:                k[i]=temp[i];  
83:                printf("%d\t", k[i]);  
84:         }  
85:  }  
86:  void display(int temp[20], int i, int j, int l)  
87:  {  
88:         int z;  
89:         printf("\n\tTABLE 1     : \t");  
90:         for(z=i;z<second; z++)  
91:                printf("%d \t", k[z]);  
92:         printf("\n");  
93:         printf("\n\tTABLE 2     : \t");  
94:         for(z=j;z<=third; z++)  
95:                printf("%d \t", k[z]);  
96:         printf("\n");  
97:         printf("\n\tNEW TABLE: \t");  
98:         for(z=0;z<l; z++)  
99:                printf("%d \t", temp[z]);  
100:         printf("\n");  
101:  }  

No comments:

Post a Comment

Code Of The day - Suggest An Output For The Snippet

  #include<stdio.h>   
  int main()   
  {   
   float a=3.15529;   
   printf("%2.1f\n", a);   
   return 0;   
  }   
· A Code Archive - code1archive