fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. void insertionSort(int arr[], int n)
  4. {
  5. for (int i = 1; i < n; ++i) {
  6. int key = arr[i];
  7. int j = i - 1;
  8. while (j >= 0 && arr[j] > key) {
  9. arr[j + 1] = arr[j];
  10. j = j - 1;
  11. }
  12. arr[j + 1] = key;
  13. }
  14. }
  15.  
  16. void printArray(int arr[], int n)
  17. {
  18. for (int i = 0; i < n; ++i)
  19. cout << arr[i] << " ";
  20. cout << endl;
  21. }
  22.  
  23. int main()
  24. {
  25. int arr[] = { 12, 11, 13, 5, 6 };
  26. int n = sizeof(arr) / sizeof(arr[0]);
  27. clock_t begin = clock();
  28. insertionSort(arr, n);
  29. printArray(arr, n);
  30. clock_t end = clock();
  31. cout<<"Time run: "<< fixed << (float)(end-begin)/CLOCKS_PER_SEC<<" s"<<endl;
  32.  
  33.  
  34. return 0;
  35. }
  36.  
  37.  
Success #stdin #stdout 0.01s 5252KB
stdin
Standard input is empty
stdout
5 6 11 12 13 
Time run: 0.000155 s