Question

In: Computer Science

Consider the quick sort algorithm. The quick sort algorithm is a divide and conquer approach which...

Consider the quick sort algorithm.

The quick sort algorithm is a divide and conquer approach which chooses a pivot value and divides the subarrays as the lower values on the left and the bigger values on the right (comparing them to pivot). Then, each subarray is processed similarly until the array becomes sorted.

Quick sort algorithm has an average time complexity of O(nlogn). However, if the array to be used in the quicksort is already sorted, then the complexity becomes O(n2).

In this question, modify the quicksort algorithm. Therefore, you will use a randomized partitioning algorithm that shuffles the array first and then chooses a pivot value. (in your cpp file)

Also analyze, what will change by following such an approach. What will be the complexity? Write down your analysis (in a docx file).

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP

#include <cstdlib>
#include <iostream>
using namespace std;
int p(int quick[], int l, int h)
{
int pt = quick[h];
int i = (l - 1);

for (int j = l; j <= h - 1; j++) {
if (quick[j] <= pt) {

i++;
swap(quick[i], quick[j]);
}
}
swap(quick[i + 1], quick[h]);
return (i + 1);
}
int p_random(int quick[], int l, int h)
{
srand(time(NULL));
int random = l + rand() % (h - l);
swap(quick[random], quick[h]);
return p(quick, l, h);
}
void qucikRandomSort(int quick[], int l, int h)
{
if (l < h) {
int pi = p_random(quick, l, h);   
qucikRandomSort(quick, l, pi - 1);
qucikRandomSort(quick, pi + 1, h);
}
}
int main()
{
int quick[] = { 85, 69, 25, 35 };
qucikRandomSort(quick, 0, 3);
for(int i=0; i<4;i++)
cout<<quick[i]<<" ";
cout<<endl;
return 0;
}

By using such type of approach we can improve the expected time complexity to O(Nlog N)


Related Solutions

Explain why selection sort can be viewed as a divide and conquer algorithm. Compare selection sort...
Explain why selection sort can be viewed as a divide and conquer algorithm. Compare selection sort with insertion sort with respect to performance (consider worst case and best case running times).
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After...
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After division, each process sorts its part of the array using an efficient algorithm. Then, the subarrays are merged into larger sorted subarrays. b. Analyze the communication and computation times if the number of processes is equal to the number of array elements, n. c. Repeat Part b if the number of processes is less than the number of array elements. Assume that the computation...
Consider a sorting algorithm that combines merge sort and insertion sort algorithm. We still use divide...
Consider a sorting algorithm that combines merge sort and insertion sort algorithm. We still use divide and conquer like merge sort, however when the number of elements in an array is at most k elements (k is a parameter), we stop dividing the elements as the regular merge sort, instead, we call the insertion sort. Assuming we have defined the following two procedures: insertion-sort(A[p..q]) which sort the subarray A[p..q] merge(A[p,q,r]) which merges the sorted subarray A[p..r] and A[r+1..q] Try to...
1. If we view the selection sort as an application of "divide and conquer" strategy, what...
1. If we view the selection sort as an application of "divide and conquer" strategy, what is the two-part partitioning of the index range [0, n)? Is it a balanced partition? 2. For the "divide and conquer" strategy for developing algorithm, do we pursue balanced partitioning or unbalanced partitioning? Why? 3. For the quicksort, the selection of pivot determines the quality of partitioning. Do we have any easy or cheap way for selecting a good pivot that always leads to...
3. Suppose that a divide and conquer algorithm for multiplication of n x n matrices is...
3. Suppose that a divide and conquer algorithm for multiplication of n x n matrices is found such that it requires 6 multiplications and 31 additions of n/2 x n/2 submatrices. Write the recurrence for the running time T(n) of this algorithm and find the order of T(n).
The following divide-and-conquer algorithm is designed to return TRUE if and only if all elements of...
The following divide-and-conquer algorithm is designed to return TRUE if and only if all elements of the array have equal values. For simplicity, suppose the array size is n=2k for some integer k. Input S is the starting index, and n is the number of elements starting at S. The initial call is SAME(A, 0, n). Boolean SAME (int A[ ], int S, int n) { Boolean T1, T2, T3; if (n == 1) return TRUE; T1 = SAME (A,...
Q. Explain how a divide and conquer algorithm for detecting whether or not the number 5...
Q. Explain how a divide and conquer algorithm for detecting whether or not the number 5 exists in an array would work. Your explanation should include: - a description of your algorithm using psuedocode - a proof of correctness of your algorithm - an analysis of the runtime of the algorithm
Let A be an integer array of length n. Design a divide and conquer algorithm (description...
Let A be an integer array of length n. Design a divide and conquer algorithm (description and pseudo code) to find the index of an element of the minimum value in the array. If there are several such elements, your algorithm must return the index of the rightmost element. For instance, if A = {0,2,4,5,2,0,3,10}, then the algorithm should return 5, which is the index of the second 0.
Java 3. Describe the divide-and-conquer search algorithm and explain its efficiency. Consider two different Split functions:...
Java 3. Describe the divide-and-conquer search algorithm and explain its efficiency. Consider two different Split functions: Split = Lo, and Split = (Lo + Hi) / 2. Draw the trees of recursive calls. Assume that we are searching a large data base with 4 million items represented as an ordered array. How many probes into the list will the binary search require before finding the target or concluding that it cannot be found? Assume that it takes 1 microsecond to...
1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element...
1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element in an array of n numbers. 5. Find the order of growth for solutions of the following recurrences. a . T(n)=4T(n/2)+n,T(1)=1 b. T(n)=4T(n/2)+n2,T(1)=1 c. T(n)=4T(n/2)+n3,T(1)=1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT