#include<iostream>
#include <ctime>
using namespace std;
void Insertion_sort(int arr[], int n){
int i,j, key;
for(i=1; i<n;i++){
key = arr[i];
j=i-1;
while(j>=0 && arr[j]>key){
arr[j+1] = arr[j];
j--;
}
arr[j+1]=key;
}
}
int main()
{
int n,i;
cout<<"Enter the size of an array: ";
cin>>n;
int arr[n];
cout<<"Enter the elements: ";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
clock_t start = clock();
Insertion_sort(arr,n);
clock_t end = clock();
cout << "Sorted array: ";
for(i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
double time_taken = double(end - start) / CLOCKS_PER_SEC * 1000;
cout<< endl<< "Time taken to sort the array: " << time_taken << " ms" << endl;
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlIDxjdGltZT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZvaWQgSW5zZXJ0aW9uX3NvcnQoaW50IGFycltdLCBpbnQgbil7CiAgICBpbnQgaSxqLCBrZXk7CiAgICBmb3IoaT0xOyBpPG47aSsrKXsKICAgICAgICBrZXkgPSBhcnJbaV07CiAgICAgICAgaj1pLTE7CiAgICAgICAgd2hpbGUoaj49MCAmJiBhcnJbal0+a2V5KXsKICAgICAgICAgICAgYXJyW2orMV0gPSBhcnJbal07CiAgICAgICAgICAgIGotLTsKICAgICAgICB9CiAgICAgICAgYXJyW2orMV09a2V5OwogICAgfQp9CgppbnQgbWFpbigpCnsKICAgIGludCBuLGk7CiAgICBjb3V0PDwiRW50ZXIgdGhlIHNpemUgb2YgYW4gYXJyYXk6ICI7CiAgICBjaW4+Pm47CiAgICBpbnQgYXJyW25dOwogICAgY291dDw8IkVudGVyIHRoZSBlbGVtZW50czogIjsKICAgIGZvcihpPTA7IGk8bjsgaSsrKQogICAgewogICAgICAgIGNpbj4+YXJyW2ldOwogICAgfQogICAgY2xvY2tfdCBzdGFydCA9IGNsb2NrKCk7CiAgICBJbnNlcnRpb25fc29ydChhcnIsbik7CiAgICBjbG9ja190IGVuZCA9IGNsb2NrKCk7CiAgICBjb3V0IDw8ICJTb3J0ZWQgYXJyYXk6ICI7CiAgICBmb3IoaT0wOyBpPG47IGkrKykKICAgIHsKICAgICAgICBjb3V0PDxhcnJbaV08PCIgIjsKICAgIH0KICAgICBkb3VibGUgdGltZV90YWtlbiA9IGRvdWJsZShlbmQgLSBzdGFydCkgLyBDTE9DS1NfUEVSX1NFQyAqIDEwMDA7CiAgICBjb3V0PDwgZW5kbDw8ICJUaW1lIHRha2VuIHRvIHNvcnQgdGhlIGFycmF5OiAiIDw8IHRpbWVfdGFrZW4gPDwgIiBtcyIgPDwgZW5kbDsKICAgIHJldHVybiAwOwp9Cgo=