fork download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. void printArray(int array[], int size) {
  7. for (int i = 0; i < size; i++) {
  8. cout << array[i] << " ";
  9. }
  10. cout << endl;
  11. }
  12.  
  13. void insertionSort(int array[], int size) {
  14. for (int step = 1; step < size; step++) {
  15. int key = array[step];
  16. int j = step - 1;
  17.  
  18. while (j >=0 && key < array[j]) {
  19. array[j + 1] = array[j];
  20. --j;
  21. }
  22. array[j + 1] = key;
  23. }
  24. }
  25.  
  26. int main() {
  27. std::clock_t start;
  28. double duration;
  29.  
  30. start = std::clock();
  31. int data[] = {1000,100,10000,1000000};
  32. int size = sizeof(data) / sizeof(data[0]);
  33. insertionSort(data, size);
  34. cout << "Sorted array in ascending order:\n";
  35. printArray(data, size);
  36. duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
  37.  
  38. std::cout<<"printf: "<< duration <<'\n';
  39. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Sorted array in ascending order:
100 1000 10000 1000000 
printf: 0.000198