Advertisement
Wojtekd

InsertionSort

Mar 18th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void swap(int* a, int* b)
  4. {
  5.     int temp = *a;
  6.     *a = *b;
  7.     *b = temp;
  8. }
  9. void InsertionSort(int* tab)
  10. {
  11.     for(int i = 1; i < 10; i++)
  12.     {
  13.         for(int j = i; j > 0; j--)
  14.         {
  15.             if(tab[j] < tab[j - 1])
  16.             {
  17.                 swap(&tab[j],&tab[j-1]);
  18.             }
  19.         }
  20.     }
  21. }
  22. void displayArray(int* tab)
  23. {
  24.     printf("\n");
  25.     for(int i = 0; i < 10; i++)
  26.     {
  27.         printf("%d\t",tab[i]);
  28.     }
  29. }
  30. int main( void )
  31. {
  32.     int tab[10] = {0, -3, 7, 1, 4, 2, 5, 9, 8, 6};
  33.     displayArray(tab);
  34.     InsertionSort(tab);
  35.     displayArray(tab);
  36.    
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement