Question

In: Computer Science

Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It...

Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It works as follows:

iqsort(A, p, q){

if p ≥ q, return;

r=partition(A, p, q);

//run quick sort on the low part

quicksort(A, p, r − 1);

//run insert sort on the high part

insertsort(A, r + 1, q); }

Compute the best-case, worst-case, and average-case complexities of iqsort.

Solutions

Expert Solution

Given
qsort(A, p, q){

if p = q, return;

r=partition(A, p, q);

//run quick sort on the low part

quicksort(A, p, r - 1);//worst case O(n^2) //best,average case : O(nlogn)

//run insert sort on the high part

insertsort(A, r + 1, q); }//worst, average case O(n^2), best case:

qSort complexity is depended on the pivot choosen and order of the list:

Best case:O(nlogn)
explanation:
consider list is in sorted(increase), pivot is choose such that, list is divided into half
in that case
half part will be executed by quick sort which will run in (n/2)log(n/2)=O(nlogn)
and
another half part will be executed by insertion which runs in n/2 = O(n)//since list sorted
total complexity : nlogn +n = O(nlogn)
Worst and average case: O(n^2)
explanation:
consider list is in reverse sorted order, and last element is choosen as pivot,
the list not be divided evenly
quicksort will get 1 element, and insertion will get n-1 elements
insertion sort will take O(n^2) to sort reverse sorted list
hence complexity is O(n^2)


Related Solutions

Let A be an array of non-decreasing N integers. Write an algorithm that returns the number...
Let A be an array of non-decreasing N integers. Write an algorithm that returns the number of pairs of integers in A that are equal. Analyze your algorithm thoroughly. Your analysis should include a thorough examination of both the best and the worst-case scenarios. This includes a description of what the best and worst cases would look like. You should include both space and time in your analysis, but keep in mind that space refers to “extra space,” meaning in...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1]...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1] of n elements. void ISORT (dtype A[ ], int n) { int i, j; for i = 1 to n – 1 {     //Insert A[i] into the sorted part A[0..i – 1]     j = i;     while (j > 0 and A[j] < A[j – 1]) {         SWAP (A[j], A[j – 1]);         j = j – 1 }     }...
Let A[1..n] be an array of distinct positive integers, and let t be a positive integer....
Let A[1..n] be an array of distinct positive integers, and let t be a positive integer. (a) Assuming that A is sorted, show that in O(n) time it can be decided if A contains two distinct elements x and y such that x + y = t. (b) Use part (a) to show that the following problem, re- ferred to as the 3-Sum problem, can be solved in O(n2) time: 3-Sum Given an array A[1..n] of distinct positive integers, and...
Let A[1..n] be an array of distinct positive integers, and let t be a positive integer....
Let A[1..n] be an array of distinct positive integers, and let t be a positive integer. (a) Assuming that A is sorted, show that in O(n) time it can be decided if A contains two distinct elements x and y such that x + y = t. (b) Use part (a) to show that the following problem, re- ferred to as the 3-Sum problem, can be solved in O(n2) time: 3-Sum Given an array A[1..n] of distinct positive integers, and...
Given an array A of n integers, describe an O(n log n) algorithm to decide if...
Given an array A of n integers, describe an O(n log n) algorithm to decide if the elements of A are all distinct. Why does your algorithm run in O(n log n) time?
1. Read 20 integers into an array. Next, use the unique algorithm to reduce the array...
1. Read 20 integers into an array. Next, use the unique algorithm to reduce the array to the unique values entered by the user. Use the copy algorithm to display the unique values. 2. Modify the Exercise 1 above to use the unique_copy algorith. The unique values should be inserted into a vector that's initially empty. Use a back_inserter to enable the vector to grow as new items are added. Use the copy algorithm to display the unique values.
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.
Let A[1 · · · n] be an array of n elements and B[1 · ·...
Let A[1 · · · n] be an array of n elements and B[1 · · · m] an array of m elements. We assume that m ≤ n. Note that neither A nor B is sorted. The problem is to compute the number of elements of A that are smaller than B[i] for each element B[i] with 1 ≤ i ≤ m. For example, let A be {30, 20, 100, 60, 90, 10, 40, 50, 80, 70} of ten...
Let A[1 · · · n] be an array of n elements and B[1 · ·...
Let A[1 · · · n] be an array of n elements and B[1 · · · m] an array of m elements. We assume that m ≤ n. Note that neither A nor B is sorted. The problem is to compute the number of elements of A that are smaller than B[i] for each element B[i] with 1 ≤ i ≤ m. For example, let A be {30, 20, 100, 60, 90, 10, 40, 50, 80, 70} of ten...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for find a pair of elements A[i] and A[i+1] such that A[i] != A[i+1]. can you always find such pair and why
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT