upsidedown

bubble sort

Sep 27th, 2011
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.18 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3.  
  4. void Bubblesort (int data[ ],int n)
  5.    {    int tmp,i,j;
  6.  
  7.         printf("\ninitial array is ");
  8.         for (i=0; i<n; i++)
  9.         {
  10.             printf("%d ",data[i]);
  11.         }
  12.  
  13.  
  14.         for (i=0; i<n-1; i++)
  15.         {
  16.               for (j=0; j<n-i-1; j++)
  17.                   if (data[j] > data[j+1])
  18.                   {
  19.                      tmp = data[j];
  20.                     data[j] = data[j+1];
  21.                     data[j+1] = tmp;
  22.                   }
  23.             printf("\nThe array after iteration %d is ",i+1);
  24.             for (j=0; j<n; j++)
  25.             {
  26.                 printf("%d ",data[j]);
  27.             }
  28.                
  29.  
  30.         }
  31.  
  32.         printf("\nThe final  array is ");
  33.         for (i=0; i<n; i++)
  34.         {
  35.             printf("%d ",data[i]);
  36.         }
  37.        
  38.     }
  39.  
  40.  
  41.  
  42. int main()
  43. {
  44.     int x[100],n,i;
  45.     printf("Enter the no of element");
  46.     scanf("%d",&n);
  47.     printf("\nEnter element");
  48.     for(i=0;i<n;i++)
  49.         scanf("%d",&x[i]);
  50.     Bubblesort(x,n);
  51.     return 0;
  52. }
  53.  
  54.  
  55.  
  56. OUTPUT:
  57. Enter the no of element5
  58.  
  59. Enter element43 36 60 12 7
  60.  
  61. initial array is 43 36 60 12 7
  62. The array after iteration 1 is 36 43 12 7 60
  63. The array after iteration 2 is 36 12 7 43 60
  64. The array after iteration 3 is 12 7 36 43 60
  65. The array after iteration 4 is 7 12 36 43 60
  66. The final  array is 7 12 36 43 60 Press any key to continue
Advertisement
Add Comment
Please, Sign In to add comment