To implement three different sorting algorithms to sort an array of 50000 random integers.
Question:
Please talk about the question. Don’t give me the code. Just need the specific approach. Thanks.
In this lab, you are going to implement three different sorting algorithms to sort an array of 50,000 random integers. You should measure the elapsed time for each method. Repeat the experiment on the sorted array as well and compare the elapsed time. You may include the library and use the following sample code to measure the running time of your function.
const int CLOCKS_PER_MS = CLOCKS_PER_SEC/1000; //clock per milliseconds
…
clock_t Start = clock();
//call sort function here
clock_t End = clock();
int elapsedTime = (End – Start)/CLOCKS_PER_MS; // converts elapsed time from microseconds to milliseconds.
Implement the following function to sort an array of integers:
void Quicksort_midpoint(int numbers[], int i, int k): this function sorts the given array in the range from i to k using quicksort method. In this function, pivot is the midpoint element (numbers[(i+k)/2]). (implementation of this function is given in section 21.5)
void Quicksort_medianOfThree(int numbers[], int i, int k): this function utilizes different pivot selection technique in quicksort algorithm. The pivot is the median of the following three values: leftmost (numbers[i]), midpoint (numbers[(i+k)/2]) and rightmost (numbers[k]). You should modify the partition function given in section 21.5 to select the pivot using median-of-three technique.
void Insertion Sort(int numbers[], int numbers Size): this function sorts the given array using Insertion Sort method. (implementation of this method is provided in section 21.3).
You can use the following function to generate three similar array of random integers.
const int NUMBERS_SIZE = 50000;
…
int genRandInt(int low, int high) {
return low + rand() % (high – low + 1);
}
void fill Arrays(int arr1[], int arr2[],int arr3[]){
for(int i = 0; i < NUMBERS_SIZE; ++i){
arr1[i] = genRandInt(0,NUMBERS_SIZE);
arr2[i] = arr1[i];
arr3[i] = arr1[i];
}
}
Summary:
Basically, three arrays are declared, and the fill Arrays method is used to assign values to the arrays.
The clock is then started for each function before it is called and then closed in the next step of the function call.
As a result, we have three clock speeds to compare for insertion sort, quicksort midpoint, and quicksort median OdThree.
Explanation:
Three 5000-by-5000-by-5000-by-5000-by-5000-by-5000-by-5000-by-5000-by-5000-by-5000-by-5000-by-5000-by
To initialise the three arrays, use the function “genRandInt” given below.
return low + rand() percent (high – low + 1);
{ int genRandInt(int low, int high) }
The array element is allocated the return value of for each index in the array “genRandInt()” called.
The function fillArrays is responsible for assigning random integers to the arrays ().
The three arrays are supplied to the fillArrays function and 5000 numbers are allocated to them.
The Insertion sort function definition is then written.
Sorting by insertion:
The set of items preceding that element is checked for every element, as illustrated in the figure above, and the current element is placed in its correct position in the sorted set. The procedure then continues till it reaches its conclusion.
The function header of the insertion sort can be: \s
Void insertion Sort(int arr[5000]); \s
The clock function is called, and the clock will be started before calling this function.
The above function is then called.
The clock is then stopped, and the difference is calculated, yielding the time required to sort 5000 items using insertion sort, which is then displayed on the console.
Quicksort:
Divide and conquer is a tactic used by Quick Sort.
The pivot in the preceding diagram is the last element, just to demonstrate an example.
However, the pivot should be the middle element, as stated in the question.
Similarly, the pivot divides the set into two halves, one on the left and one on the right, each time.
All elements fewer than the pivot will be on the left, while all elements bigger than the pivot will be on the right.
The clock has begun to tick.
It needs to use the method Quicksort midpoint(int arr2[5000]).
The clock then comes to a halt.
The time difference between the clocks is discovered and printed on the console.
The pivot in Quicksort median of three is the median of three items, one on the left, one on the right, and the third on the middle indexed element.
The clock has begun to tick.
The Quicksort medainOfThree(int arr3[5000]) function is used.
The clock then comes to a halt.
The time difference between the clocks is discovered and printed on the console.
Insertion sort has an O time complexity (n log n)
Both quick sorts have a time complexity of O(n log n), and if the input is in the worst case, the complexity is O(n2).
The tree algorithms’ clock times are compared.
Also, read the Given below-defined UML class diagram.