Advertisement
rihardmarius

insertion sort - array

Dec 8th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <array>
  3. #define N 5
  4.  
  5. using namespace std;
  6.  
  7. void swap (int& a, int& b)
  8. {
  9.     int aux = a;
  10.     a = b;
  11.     b = aux;
  12. }
  13.  
  14. void printarray (array<int,N>& a)
  15. {
  16.     for (int i=0; i<N; i++)
  17.         cout << a.at(i) << " ";
  18.     cout << "\n";
  19. }
  20.  
  21. void insertion_sort (array<int,N>& a)
  22. {
  23.     for (int i=1; i<N; i++)
  24.         for (int j=i; j>0 and a.at(j) < a.at(j-1); j--)
  25.             swap(a.at(j),a.at(j-1));
  26. }
  27.  
  28. int main()
  29. {
  30.     array<int,N> arr = {5,4,3,2,1};
  31.     insertion_sort(arr);
  32.     printarray(arr);
  33.  
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement