Advertisement
Shailrshah

Bubble Sort and Recursive Binary Search

Aug 4th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  1. #include <stdio.h>
  2. int a[10];
  3. void inputArray()
  4. {
  5.     int i;
  6.     printf("Enter 10 elements:-\n");
  7.     for(i = 0; i<10; i++)
  8.         scanf("%d", &a[i]);
  9. }
  10. void bSort()
  11. {
  12.     int i,j,temp;
  13.     for(i = 10-2; i>=0; i--)
  14.         for(j = 0; j <= i; j++)
  15.             if (a[j] > a[j+1])
  16.             {
  17.                 temp = a[j];
  18.                 a[j] = a[j+1];
  19.                 a[j+1] = temp;
  20.             }
  21. }
  22. void showArray()
  23. {
  24.     int i;
  25.     printf("The array in ascending order is:-\n");
  26.     for(i=0; i<10; i++)
  27.         printf("| %d ",a[i]);
  28. }
  29. int inputElement()
  30. {
  31.     int x;
  32.     printf("|\nEnter the element to be searched for: ");
  33.     scanf("%d",&x);
  34.     return x;
  35. }
  36. int bSearch(int x, int l, int h)
  37. {
  38.     int c = -1; int mid;
  39.     if(l <= h)
  40.     {
  41.         mid = (l+h)/2;
  42.         if(x == a[mid])
  43.             return mid;
  44.         if(x < a[mid])
  45.             return bSearch(x, l, mid-1);
  46.         else
  47.             return bSearch(x, mid+1, h);
  48.     }
  49.     else
  50.         return c;
  51. }
  52. void result(int c)
  53. {
  54.     if (c == -1)
  55.         printf("Not found!");
  56.     else
  57.         printf("Found at %d!", c);
  58. }
  59. int main()
  60. {
  61.     int c;
  62.     inputArray();
  63.     bSort();
  64.     showArray();
  65.     c = inputElement();
  66.     c = bSearch(c, 0, 10-1);
  67.     result(c);
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement