Question: Implement the Bubble Sort and Insertion Sort algorithms. Note that the implementation of the Bubble sort algorithm should involve the swapping-based optimization discussed in the

Implement the Bubble Sort and Insertion Sort algorithms. Note that the implementation of the Bubble sort algorithm should involve the swapping-based optimization discussed in the slides. A startup code is provided wherein (the main function) an array (size: numElements) of random integers is generated with the values in the range of 1...maxValue. Implement the two sorting algorithms (use one code file per sorting algorithm) and measure the sorting time (in milli seconds) to sort arrays of size (numElements) 10000 and 100000, with maxValue being 50000 in each case.

#include #include #include #include

#include #include

int main(){

using namespace std;

int numElements; cout << "Enter the number of elements: "; cin >> numElements; int maxValue; cout << "Enter the maximum value: "; cin >> maxValue; srand( static_cast(time(nullptr))); using namespace std::chrono;

int *array = new int[numElements]; for (int index = 0; index < numElements; index++){ array[index] = 1 + (rand() % maxValue); }

high_resolution_clock::time_point t1 = high_resolution_clock::now(); // Implement your sorting algorithm here

high_resolution_clock::time_point t2 = high_resolution_clock::now();

duration sortingTime_milli = t2 - t1; double sortingTime = sortingTime_milli.count();

cout << " Sorting Time (milliseconds): " << (sortingTime) << endl;

delete[] array; system("pause");

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!