#include <iostream> using namespace std; int InsertionSort(int a[], int size) { int numberOfSwaps = 0; for(int i = 1; i < size; ++i) { int key = a[i]; int j = i-1; while(j >= 0 && a[j] > key) { a[j+1] = a[j]; --j; ++numberOfSwaps; } a[j+1] = key; } return numberOfSwaps; } void PrintArray(int a[], int size) { for(int i = 0; i < size; ++i) { cout << a[i] << " "; } cout << "\n"; } int main() { int n; cin >> n; int a[n]; for(int i = 0; i < n; ++i) { cin >> a[i]; } cout << InsertionSort(a, n); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter