Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. void InsertionSort(int *arr, int length)
  6. {
  7. for (int i = 1; i < length; i++)
  8. {
  9. if (arr[i - 1] > arr[i])
  10. {
  11. for (int j = i; j > 0; j--)
  12. {
  13. if (arr[j - 1] > arr[j])
  14. swap(arr[j], arr[j - 1]);
  15. else
  16. break;
  17. }
  18. for (int k(0); k < length; k++)
  19. {
  20. cout << arr[k] << " ";
  21. }
  22. cout << "\n";
  23. }
  24. }
  25. }
  26.  
  27.  
  28. int main()
  29. {
  30.  
  31. int n;
  32. cin >> n;
  33.  
  34. int *arr = new int[n];
  35.  
  36. for (int i(0); i < n; i++)
  37. {
  38. cin >> arr[i];
  39. }
  40.  
  41. InsertionSort(arr, n);
  42.  
  43. system("pause");
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement