Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- void InsertionSort(int *arr, int length)
- {
- for (int i = 1; i < length; i++)
- {
- if (arr[i - 1] > arr[i])
- {
- for (int j = i; j > 0; j--)
- {
- if (arr[j - 1] > arr[j])
- swap(arr[j], arr[j - 1]);
- else
- break;
- }
- for (int k(0); k < length; k++)
- {
- cout << arr[k] << " ";
- }
- cout << "\n";
- }
- }
- }
- int main()
- {
- int n;
- cin >> n;
- int *arr = new int[n];
- for (int i(0); i < n; i++)
- {
- cin >> arr[i];
- }
- InsertionSort(arr, n);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement