Advertisement
Shailrshah

Merge Sort and Binary Search

Jul 27th, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.35 KB | None | 0 0
  1. #include <stdio.h>
  2. int na,nb;
  3. void mergeSort(int c[], int low, int mid, int high);
  4. void input(int a[],int b[])
  5. {
  6.         int i;
  7.         printf("Enter the limit of A: ");
  8.         scanf("%d",&na);
  9.         printf("Enter the limit of B: ");
  10.         scanf("%d", &nb);
  11.         printf("Enter %d elements of A:-\n", na);
  12.         for(i=0; i<na; i++)
  13.                 scanf("%d", &a[i]);
  14.         printf("Enter %d elements of B:-\n", nb);
  15.         for(i=0; i<nb; i++)
  16.                 scanf("%d", &b[i]);
  17. }
  18. void merge(int a[], int b[], int c[])
  19. {
  20.         int i,j = 0;
  21.         for(i=0; i<na; i++)
  22.                 c[i] = a[i];
  23.         for (i; i<(na+nb); i++)
  24.                 c[i] = b[j++];
  25.         printf("The unsorted array is:\t");
  26.         for(i=0; i<(na+nb); i++)
  27.                 printf("%d ", c[i]);
  28.  
  29. }
  30. void partition(int c[], int low, int high)
  31. {
  32.         int mid;
  33.         if(low < high)
  34.         {
  35.                 mid = (low + high)/2;
  36.                 partition(c, low, mid);
  37.                 partition(c,(mid+1), high);
  38.                 mergeSort(c, low, mid, high);
  39.         }
  40. }
  41. void mergeSort(int c[], int low, int mid, int high)
  42. {
  43.         int i = low, l = low, m = mid + 1, k, temp[200];
  44.         while ((l <= mid) && (m <= high))
  45.         {
  46.                 if(c[l] <= c[m])
  47.                 {
  48.                         temp[i] = c[l];
  49.                         l++;
  50.                 }
  51.                 else
  52.                 {
  53.                         temp[i] = c[m];
  54.                         m++;
  55.                 }
  56.                 i++;
  57.         }
  58.         if( l > mid)
  59.         {
  60.                 for (k = m; k <= high; k++)
  61.                 {
  62.                         temp[i] = c[k];
  63.                         i++;
  64.                 }
  65.         }
  66.         else
  67.         {
  68.                 for(k = l;k <= mid; k++)
  69.                 {
  70.                         temp[i] = c[k];
  71.                         i++;
  72.                 }
  73.         }
  74.         for (k = low; k <= high; k++)
  75.                 c[k] = temp[k];
  76. }
  77. void display(int c[])
  78. {
  79.         int i;
  80.         printf("\nThe sorted array is:\t");
  81.         for(i = 0; i < (na+nb); i++)
  82.                 printf("%d ", c[i]);
  83. }
  84. void search(int c[])
  85. {
  86.         int left = 0, right = (na + nb - 1), mid, key, found = 0;
  87.         printf("\nEnter the element you want to find: ");
  88.         scanf("%d",&key);
  89.         while(left <= right)
  90.         {
  91.                 mid = (left + right)/2;
  92.                 if(key == c[mid])
  93.                 {
  94.                         found = 1;
  95.                         break;
  96.                 }
  97.                 else if(key < c[mid])
  98.                         right = mid - 1;
  99.                 else
  100.                         left = mid + 1;
  101.         }
  102.         if (found == 1)
  103.                 printf("\n%d is found! It is at position %d.",key, mid);
  104.         else
  105.                 printf("\n%d is not found in the array!", key);
  106. }
  107. int main()
  108. {
  109.         int a[100],b[200],c[200];
  110.         input(a, b);
  111.         merge(a, b, c);
  112.         partition(c, 0, (na+nb-1));
  113.         display(c);
  114.         search(c);
  115. }
  116.  
  117. //Output
  118. //Enter the limit of A: 3
  119. //Enter the limit of B: 3
  120. //Enter 3 elements of A:-
  121. //3
  122. //2
  123. //1
  124. //Enter 3 elements of B:-
  125. //6
  126. //5
  127. //4
  128. //The unsorted array is:  3 2 1 6 5 4
  129. //The sorted array is:    1 2 3 4 5 6
  130. //Enter the element you want to find: 4
  131. //
  132. //4 is found! It is at position 3.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement