Question: I need help on this C++ lab homework. Please indicate the separation by file name and type. Please comment the codes. Thank you Lab 12a:
I need help on this C++ lab homework. Please indicate the separation by file name and type. Please comment the codes. Thank you
Lab 12a: Sorting Algorithms
Using the following sequence of numbers, show each pass of the sorting algorithms Selection Sort, Insertion Sort, Merge Sort:
58 73 62 32 8 17 98 40
Lab 12b: Implementing Selection Sort
In this part of the lab youll be timing how long it takes to sort the numbers in an array using Selection Sort. Create a new Visual Studio project and add a new .cpp file to it; you will only need one source code file for this lab.
You will need to write two functions (in addition to main):
1. void fillArray(int data[], int SIZE);
This function takes an array (data) that holds SIZE elements. The function should fill the array from index 0 to SIZE-1 with integers. The numbers placed in the array should be in random order (note the change from the last lab). How you come up with the numbers to place in the array is up to you. I used the following formula: data[i] = (rand()%1001)*(rand()%1001) + rand();
2. void selectionSort(int data[], int SIZE);
This function takes an array (data) that holds SIZE elements and places the integers in the array in order using the Selection Sort algorithm.
Once you have these functions written, write a main() function to test them out. First, have main() create an array with 20 elements, call the fillArray function with the array, then call the selectionSort function with the array, and conclude by printing the values in the array to the screen (one per line). The numbers should be printed in order if your selection sort function actually works!
Once you are confident that these functions are working as expected, modify main()so that it contains a for loop with a counter variable called SIZE that is initialized to 20000. The for loop should run as long as SIZE <= 640000 and SIZE should be multiplied by 2 (SIZE *= 2) after each iteration of the loop. Inside of the loop, you should create a dynamically allocated array of integers with SIZE elements, pass this array to the fillArray function, and then time how long it takes to sort the array using your selectionSort function. You can time how long it takes to search in milliseconds by adding the 53Stopwatch.h file to your project and using the following code as a template:
Stopwatch timer; //You must include 53Stopwatch.h, this also begins the stopwatch
//have the code you are measuring here
double secs = timer.recordTime();
cout << "It took " << secs << " seconds." << endl;
Print to the screen how long it took to sort the array with the number of elements on each iteration of the for loop; something like the following would be fine:
Selection Sort: 20000 elements took 2.263 seconds
Also, remember to delete your dynamically allocated array at the end of each iteration of your for loop.
Lab 12c: Implementing other Sorting Algorithms
As time allows, write functions for Insertion Sort, Merge Sort, and Quick Sort. Feel free to use any implementation of these algorithms that you find online or in your zyBook - Just make sure you document the source of the code in the comments of your program. You should also verify that the function actually works before you run all of your tests with it.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
