fork(1) download
  1. #include<iostream>
  2. #include <ctime>
  3. using namespace std;
  4.  
  5. void Selection_sort(int arr[], int n)
  6. {
  7. int min, i, j;
  8. for (i = 0; i < n - 1; i++)
  9. {
  10. min = i;
  11. for (j = i + 1; j < n; j++)
  12. {
  13. if (arr[j] < arr[min])
  14. {
  15. min = j;
  16. }
  17. }
  18. swap(arr[min], arr[i]);
  19. }
  20. }
  21.  
  22.  
  23. int main()
  24. {
  25. int n,i;
  26. cout<<"Enter the size of an array: ";
  27. cin>>n;
  28. int arr[n];
  29. cout<<"Enter the elements: ";
  30. for(i=0; i<n; i++)
  31. {
  32. cin>>arr[i];
  33. }
  34. clock_t start = clock();
  35. Selection_sort(arr,n);
  36. clock_t end = clock();
  37. cout << "Sorted array: ";
  38. for(i=0; i<n; i++)
  39. {
  40. cout<<arr[i]<<" ";
  41. }
  42. double time_taken = double(end - start) / CLOCKS_PER_SEC * 1000;
  43. cout << endl<<"Time taken to sort the array: " << time_taken << " ms" << endl;
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0.01s 5280KB
stdin
5
1 2 3  4 5
stdout
Enter the size of an array: Enter the elements: Sorted array: 1 2 3 4 5 
Time taken to sort the array: 0 ms