Question

Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows: a....

Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows:

  • a. Sort the array using pivot as the middle element of the array.
  • b. Sort the array using pivot as the median of the first, last, and middle elements of the array.
  • c. Sort the array using pivot as the middle element of the array. However, when the size of any sublist reduces to less than 20, sort thesublis t using an insertion sort.
  • d. Sort the array using pivot as the median of the first, last, and middle elements of the array. When the size of any sublist reduces to less than 20, sort the sublist using an insertion sort.
  • e. Calculate and print the CPU time for each of the preceding four steps. To find the current CPU time, declare a variable, say, x, of type clock_t.The statement x = clock(); stores the current CPU time in x. You can check the CPU time before and after a particular phase of a program. Then, to find the CPU time for that particular phase of the program, subtract the before time from the after time. Moreover, you must include the header file ctime to use the data type clock_t and the function clock. Use a random number generator to initially fill the array.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

Answer:

Note: Code has been implemented as per user requirements.

#include <sstream>

#include <fstream>

#include <iostream>

#include <ctime>

using namespace std;

//FUNCTION HEADERS

void qsMethodA(int *myQSList, int myStartInd, int myEndInd);

void qsMethodB(int *myQSList, int myStartInd, int myEndInd);

void qsMethodC(int *myQSList, int myStartInd, int myEndInd);

void qsMethodD(int *myQSList, int myStartInd, int myEndInd);

void insertionSort(int *myQSList, int myStartInd,int myEndInd);

//a. Sort the array using pivot as the middle element of the array.

void qsMethodA(int *myQSList, int myStartInd, int myEndInd)

{

//PIVOT IS THE MEDIAN OF FIRST ANDLAST ELEMENT

int myPivotInd = (myStartInd + myEndInd)/2;

int myInd = myStartInd;

//PARTITION THE LIST

if ( myStartInd < myEndInd )

{

  

int tmpValA = myQSList[myStartInd];

myQSList[myStartInd] = myQSList[myPivotInd];

myQSList[myPivotInd] = tmpValA;

//ARRANGE THE ELEMENTS IN UPPER AND LOWER SUBLIST   

for ( int ii = myStartInd + 1; ii <= myEndInd; ii++ )

{

  

if ( myQSList[ii] <= myQSList[myStartInd] )

{

myInd+=1;

int tmpValB = myQSList[ii];

myQSList[ii] = myQSList[myInd];

myQSList[myInd] = tmpValB;

}

}

  

int tmpValC = myQSList[myStartInd];

  

myQSList[myStartInd] = myQSList[myInd];

myQSList[myInd] = tmpValC;

  

myPivotInd = myInd;

//RECURSIVELY CALL THIS METHOD TO SORT FOR THE SUBLISTS

qsMethodA(myQSList,myStartInd,myPivotInd - 1); //LOWER SUBLIST

qsMethodA(myQSList,myPivotInd + 1,myEndInd);//UPPPER SUBLIST

  

  

}

}

//b. Sort the array using pivot as the median of the first, last, and middle elements of the array.

void qsMethodB(int *myQSList, int myStartInd, int myEndInd)

{

int m=(myStartInd+myEndInd)/2;

//PIVOT IS THE MEDIAN OF FIRST AND LAST AND MIDDLE ELEMENT

int myPivotInd = (myStartInd + myEndInd+m)/3;

int myInd = myStartInd;

//DIVIDE LIST INTO 2 HALVES   

if ( myStartInd < myEndInd )

{

  

int tmpValA = myQSList[myStartInd];

myQSList[myStartInd] = myQSList[myPivotInd];

myQSList[myPivotInd] = tmpValA;

//ARRANGE THE ELEMENTS   

for ( int ii = myStartInd + 1; ii <= myEndInd; ii++ )

{

  

if ( myQSList[ii] <= myQSList[myStartInd] )

{

myInd+=1;

int tmpValB = myQSList[ii];

myQSList[ii] = myQSList[myInd];

myQSList[myInd] = tmpValB;

}

}

  

int tmpValC = myQSList[myStartInd];

myQSList[myStartInd] = myQSList[myInd];

myQSList[myInd] = tmpValC;

  

myPivotInd = myInd;

//RECURSIVELY CALL THIS METHOD TO SORT FOR THE SUBLISTS

qsMethodB(myQSList,myStartInd,myPivotInd - 1);//LOWER SUBLIST

qsMethodB(myQSList,myPivotInd + 1,myEndInd);//UPPPER SUBLIST

  

}

}

//INSERTION SORT

void insertionSort(int *myQSList, int myStartInd,int myEndInd)

{

int ii, kk, jj;

for (ii = myStartInd+1; ii < myEndInd+1; ii++)

{

kk = myQSList[ii];

jj = ii-1;

  

while (jj >= 0 && myQSList[jj] > kk)

{

myQSList[jj+1] = myQSList[jj];

jj = jj-1;

}

myQSList[jj+1] = kk;

}

}

//c. Sort the array using pivot as the middle element of the array. How- ever, when the size of any sublist reduces to less than 20, sort the sublist using an insertion sort.

void qsMethodC(int *myQSList, int myStartInd, int myEndInd)

{

//PIVOT IS THE MEDIAN OF FIRST ANDLAST ELEMENT

int myPivotInd = (myStartInd + myEndInd)/2;

int myInd = myStartInd;

//IF NO OF ELEMENTS<20 SORT BY INSERTION SORT

if(myEndInd-myStartInd<20)

{

  

insertionSort(myQSList,myStartInd,myEndInd);

}

//WHEN NO OF ELEMENTS>=20 SORT BY QUICK SORT

//DIVIDE LIST

else if ( myStartInd < myEndInd )

{

  

int tmpValA = myQSList[myStartInd];

myQSList[myStartInd] = myQSList[myPivotInd];

myQSList[myPivotInd] = tmpValA;

for ( int ii = myStartInd + 1; ii <= myEndInd; ii++ )

{

  

if ( myQSList[ii] <= myQSList[myStartInd] )

{

myInd+=1;

int tmpValB = myQSList[ii];

myQSList[ii] = myQSList[myInd];

myQSList[myInd] = tmpValB;

}

}

  

int tmpValC = myQSList[myStartInd];

myQSList[myStartInd] = myQSList[myInd];

myQSList[myInd] = tmpValC;

  

myPivotInd = myInd;

//RECURSIVELY CALL THIS METHOD TO SORT FOR THE SUBLISTS

qsMethodC(myQSList,myStartInd,myPivotInd - 1);

qsMethodC(myQSList,myPivotInd + 1,myEndInd);

  

}

}

//d.Sort the array using pivot as the median of the first, last, and middle elements of the array. When the size of any sublist reduces to less than 20, sort the sublist using an insertion sort.

void qsMethodD(int *myQSList, int myStartInd, int myEndInd)

{

  

int m=(myStartInd+myEndInd)/2;

//PIVOT IS THE MEDIAN OF FIRST AND LAST AND MIDDLE ELEMENT

int myPivotInd = (myStartInd + myEndInd+m)/3;

int myInd = myStartInd;

//IF NO OF ELEMENTS<20 SORT BY INSERTION SORT

if(myEndInd-myStartInd<20)

{   

insertionSort(myQSList,myStartInd,myEndInd);

}

//WHEN NO OF ELEMENTS>=20 SORT BY QUICK SORT

//DIVIDE LIST

else if ( myStartInd < myEndInd )

{

int tmpValA = myQSList[myStartInd];

myQSList[myStartInd] = myQSList[myPivotInd];

myQSList[myPivotInd] = tmpValA;

  

for ( int ii = myStartInd + 1; ii <= myEndInd; ii++ )

{

  

if ( myQSList[ii] <= myQSList[myStartInd] )

{

myInd+=1;

int tmpValB = myQSList[ii];

myQSList[ii] = myQSList[myInd];

myQSList[myInd] = tmpValB;

}

}

  

int tmpValC = myQSList[myStartInd];

myQSList[myStartInd] = myQSList[myInd];

myQSList[myInd] = tmpValC;

  

myPivotInd = myInd;

//RECURSIVELY CALL THIS METHOD TO SORT FOR THE SUBLISTS

qsMethodD(myQSList,myStartInd,myPivotInd - 1);

qsMethodD(myQSList,myPivotInd + 1,myEndInd);

  

}

}

//PRINT THE LIST

void printmyQSList(int *myQSList,int myListSize)

{

for ( int ii = 0; ii < myListSize; ii++ )

{

cout<<myQSList[ii]<<" ";

}

cout<<endl;

}

int main()

{

  

srand(time(NULL));

int myListSize = 1000000;

int *myQSList = new int[myListSize];

//GENERATE RANDOMNUMBERS TO FILL ARRAY

for ( int ii = 0; ii < myListSize; ii++ )

{

myQSList[ii]=rand()%1000000+1;

}

//COPY THE LIST INTO THREE MORE LISTS

int *myQSList2=myQSList;

int *myQSList3=myQSList;

int *myQSList4=myQSList;

//OPTION e

//FIND SORT TIME FOR METHOD a

clock_t myStartTime = clock();

//CALLING METHOD A OF QUICK SORT

qsMethodA(myQSList, 0, myListSize - 1);

clock_t myEndTime = clock();

clock_t sortTime = double(myEndTime - myStartTime);

cout<<"After sorting by median of first and last"<<endl;

cout<<"Sort time:"<<sortTime<<" ms"<<endl;

//FIND SORT TIME FOR METHOD b

myStartTime = clock();

//CALLING METHOD b OF QUICK SORT

qsMethodB(myQSList2, 0, myListSize - 1);

myEndTime = clock();

sortTime = double(myEndTime - myStartTime);

cout<<"After sorting by median of first last middle"<<endl;

cout<<"Sort time:"<<sortTime<<" ms"<<endl;

//FIND SORT TIME FOR METHOD c

myStartTime = clock();

//CALLING METHOD c OF QUICK SORT

qsMethodC(myQSList3, 0, myListSize - 1);

myEndTime = clock();

sortTime = double(myEndTime - myStartTime);

cout<<"After sorting by median of first last and using insertion sort"<<endl;

cout<<"Sort time:"<<sortTime<<" ms"<<endl;

//FIND SORT TIME FOR METHOD d   

myStartTime = clock();

//CALLING METHOD d OF QUICK SORT

qsMethodD(myQSList4, 0, myListSize - 1);

myEndTime = clock();

sortTime = double(myEndTime - myStartTime);

cout<<"After sorting by median of first last middle and using insertion sort"<<endl;

cout<<"Sort time:"<<sortTime<<" ms"<<endl;

return 0;

}

Output:

Add a comment
Know the answer?
Add Answer to:
Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows: a....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please include all .cpp/.h files if neccesary, this is in C++ Sort an array of 10,000...

    Please include all .cpp/.h files if neccesary, this is in C++ Sort an array of 10,000 elements using the quick sort algorithm as follows: a- sort the array using pivot as the middle element of the array. b- Sort the array using pivot as the median of the fist,last and middle elements of the array . c- sort the array using pivot as the middle element of the array.However ,when the size of any sub list reduces to less than...

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • 6) Assume that we are using quick sort algorithm to sort the following elements in the...

    6) Assume that we are using quick sort algorithm to sort the following elements in the array 26, 15,30,11,8,17 22, 40, 4, 10. Use the first element in the array as pivot. (20 pts.) 1- How total iterations it would take to complete the sorting process? 2- Simulate the entire sorting process. (If you need additional space, complete it at the other side of the paper) public static void quick_sort(intl] a, int left, int right) if (left < right) (...

  • Write a C++ program that does the following : Accepts a positive integer ( n )...

    Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following 1. In the first function: display elements of the array. Display the first 20 elements If the size is > 20 2. In the second function : Using recursion, Search for...

  • Problem: Write a C++ program that does the following : Accepts a positive integer ( n...

    Problem: Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following In the first function: display elements of the array. Display the first 20 elements If the size is > 20 In the second function : Using recursion, Search for a...

  • Assume that you are sorting an array of 8 elements with quick sort. You just finished...

    Assume that you are sorting an array of 8 elements with quick sort. You just finished the first pass and the array looks like below. Which statement is true for the pivot value? 4 8 12 16 18 20 22 24 QUICKSORT ALGORITHM Quicksort selects a specific value called a pivot and rearranges the array into two parts (called partioning). If the array is randomly ordered, it does not matter which element is the pivot. For simplicity, the first element...

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • And the related algorithms: (20 points) Consider the following strategy for choosing a pivot element for...

    And the related algorithms: (20 points) Consider the following strategy for choosing a pivot element for the Partition subroutine of QuickSort, applied to an array A. .Let n be the number of elements of the array A. If n 24, perform an Insertion Sort of A and return. Otherwise: Choose 2n/2)| elements at random from n; let S be the new list with the chosen elements. Sort the list S using Insertion Sort and use the median m of S...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT