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