Advertisement
Mary_99

bouble sort in function

Jan 9th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  5.  
  6. void bubble_sort( int tab[], int n)
  7. {
  8.     int step;
  9.     int temp;
  10.     int i;
  11.     for( step=0; step<n-1; ++step)
  12.     for(i=0; i<n-step-1;++i)
  13.     {
  14.         if(tab[i]>tab[i+1])
  15.         {
  16.             temp = tab[i];        
  17.             tab[i]=tab[i+1];
  18.             tab[i+1]= temp;
  19.            
  20.         }
  21.     }
  22. }
  23.  
  24.  int main ()
  25.  {    int i;
  26.     int tab[100];
  27.     int n,step, temp;
  28.     printf("\nEnter then natural number of  elements to be sorted: \n ");
  29.     scanf("%d", &n);
  30.  
  31.     for (i=0; i<n; i++)
  32.     {
  33.     printf("\nEnter the element : % d   ", i+1);
  34.     scanf( "%d" , &tab[i]); // not put space after d :>
  35.     fflush(stdin);
  36.     }
  37.    
  38.    
  39.     for (i=0; i<n; i++)
  40.     {
  41.        
  42.         bubble_sort(tab, n);
  43.         printf("%d ", tab[i]);
  44.        
  45.     }
  46.  
  47.    
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement