• Source
    1. #include <iostream>
    2. #include <vector>
    3. #include <chrono>
    4. #include <random>
    5. #include <algorithm>
    6.  
    7. // Function to perform Bubble Sort
    8. void bubbleSort(std::vector<int>& arr) {
    9. int n = arr.size();
    10. for (int i = 0; i < n - 1; i++) {
    11. for (int j = 0; j < n - i - 1; j++) {
    12. if (arr[j] > arr[j + 1]) {
    13. std::swap(arr[j], arr[j + 1]);
    14. }
    15. }
    16. }
    17. }
    18.  
    19. // Function to perform Insertion Sort
    20. void insertionSort(std::vector<int>& arr) {
    21. int n = arr.size();
    22. for (int i = 1; i < n; i++) {
    23. int key = arr[i];
    24. int j = i - 1;
    25. while (j >= 0 && arr[j] > key) {
    26. arr[j + 1] = arr[j];
    27. j--;
    28. }
    29. arr[j + 1] = key;
    30. }
    31. }
    32.  
    33. // Function to perform Quick Sort
    34. int partition(std::vector<int>& arr, int low, int high) {
    35. int pivot = arr[high];
    36. int i = (low - 1);
    37. for (int j = low; j <= high - 1; j++) {
    38. if (arr[j] < pivot) {
    39. i++;
    40. std::swap(arr[i], arr[j]);
    41. }
    42. }
    43. std::swap(arr[i + 1], arr[high]);
    44. return (i + 1);
    45. }
    46.  
    47. void quickSort(std::vector<int>& arr, int low, int high) {
    48. if (low < high) {
    49. int pi = partition(arr, low, high);
    50. quickSort(arr, low, pi - 1);
    51. quickSort(arr, pi + 1, high);
    52. }
    53. }
    54.  
    55. // Function to measure execution time
    56. template <typename Func>
    57. double measureExecutionTime(Func func, std::vector<int>& arr) {
    58. auto start = std::chrono::high_resolution_clock::now();
    59. func(arr);
    60. auto end = std::chrono::high_resolution_clock::now();
    61. std::chrono::duration<double> duration = end - start;
    62. return duration.count();
    63. }
    64.  
    65. int main() {
    66. // Define input sizes
    67. std::vector<int> inputSizes = {100, 1000, 10000};
    68.  
    69. for (int inputSize : inputSizes) {
    70. std::vector<int> arr(inputSize);
    71.  
    72. // Generate random data
    73. std::random_device rd;
    74. std::mt19937 gen(rd());
    75. std::uniform_int_distribution<int> distribution(1, 10000);
    76. for (int& value : arr) {
    77. value = distribution(gen);
    78. }
    79.  
    80. // Clone the array for each sorting algorithm
    81. std::vector<int> bubbleArr = arr;
    82. std::vector<int> insertionArr = arr;
    83. std::vector<int> quickArr = arr;
    84.  
    85. // Measure execution times for each algorithm
    86. double bubbleTime = measureExecutionTime([&](std::vector<int>& arr) { bubbleSort(arr); }, bubbleArr);
    87. double insertionTime = measureExecutionTime([&](std::vector<int>& arr) { insertionSort(arr); }, insertionArr);
    88. double quickTime = measureExecutionTime([&](std::vector<int>& arr) { quickSort(arr, 0, arr.size() - 1); }, quickArr);
    89.  
    90. std::cout << "Input Size: " << inputSize << std::endl;
    91. std::cout << "Bubble Sort Execution Time: " << bubbleTime << " seconds" << std::endl;
    92. std::cout << "Insertion Sort Execution Time: " << insertionTime << " seconds" << std::endl;
    93. std::cout << "Quick Sort Execution Time: " << quickTime << " seconds" << std::endl;
    94. std::cout << std::endl;
    95. }
    96.  
    97. return 0;
    98. }
    99.