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