Advertisement
Mary_99

Buble sort in main funcion

Nov 27th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.94 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. int main(int argc, char *argv[]) {
  7.    
  8.     int i ;
  9.     int tab[100];
  10.     int n,step, temp; // n elemnts need to be sorted
  11.     printf(" Enter the natural number of elemnts to be sorted: ");
  12.     scanf("%d", &n);
  13.    
  14.     for(i=0; i<n; ++i)
  15.         {
  16.             printf("%d Enter elemnt:  ", i+1);
  17.             scanf("%d", &tab[i]); //wybieram po kolei numery do sortowania
  18.         }
  19.    
  20.     for( step=0; step<n-1; ++step)
  21.     for(i=0; i<n-step-1;++i)
  22.     {
  23.         if(tab[i]>tab[i+1])
  24.         {
  25.             temp = tab[i];         //sortuje przez porównanie par
  26.             tab[i]=tab[i+1];
  27.             tab[i+1]= temp;
  28.         }
  29.     }
  30.    
  31.     printf("Sorted numbers:  ");
  32.     for(i = 0; i<n; ++i)
  33.     printf("%d    ", tab[i]);  // drukowanie opowiedniej kolejności
  34.  
  35.    
  36.    
  37.    
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement