Description
The purpose of this challenge is to explore the bubble sorting algorithm
Requirements
- Write a function void fill_array_random(int array[], int size, int lbounds, int ubounds). This function will fill an array of size with values that are between lbounds and ubounds.
int num = (rand() % (ubounds - lbounds)) + lbounds;
- Write a function void fill_array_inc(int array[], int size). This function will fill the array with each element containing increasing values, starting from 1 to size.
- Write a function void fill_array_dec(int array[], int size). This function will fill the array with each element containing decreasing values, starting from size to 1.
- Write a function void bubble_sort(int array[], int size). This function will sort array using the bubble sort algorithm.
- You could also write a show_array(int array[], int size) function. Then, you could call this function using small values of size to prove that your bubble_sort() functions are working correctly.
- In main(), ask the user for the sample data size, and create an int array of this size.
- Use the code sequence below, or something similar, to test sort times using the bubble sort algorithm. Assuming you wrote the show_array() function, you can also call show_array() to ensure that your sorting algorithms work, before you add any timing code.
#include <chrono> #include <cstdlib> #include <ctime> ... int main() { srand(time(NULL)); // more code here cout << "Bubble sort (Random) took "; fill_array_random(array, arraysize, 1000, 1000000); auto start = chrono::steady_clock::now(); bubble_sort(array, arraysize); auto end = chrono::steady_clock::now(); cout << chrono::duration_cast<chrono::milliseconds>(end-start).count() << endl; cout << "Bubble sort (Increasing) took "; fill_array_inc(array, arraysize); start = chrono::steady_clock::now(); bubble_sort(array, arraysize); end = chrono::steady_clock::now(); cout << chrono::duration_cast<chrono::milliseconds>(end-start).count() << endl; cout << "Bubble sort (Decreasing) took "; fill_array_dec(array, arraysize); start = chrono::steady_clock::now(); bubble_sort(array, arraysize); end = chrono::steady_clock::now(); cout << chrono::duration_cast<chrono::milliseconds>(end-start).count() << endl; }
- Run the program with different array sizes (100, 1000, 1000000, etc) to get a more accurate depiction of the difference in timings.
CATALOG ID: CPP-CHAL00055a
Print Requirements