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 }     }...
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.
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
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find out the total number of inversions in the given sequence. For example, for the sequence of 2, 4, 1, 3, 5, there are three inversions (2,1), (4,1), and (4,3). Give a brute-force algorithm with running time of O(n^2). Using the technique of divide-and-conquer, design an algorithm with running time of O(nlog n).
Let X1 and X2 be uniform on the consecutive integers -n, -(n+1), ... , n-1, n....
Let X1 and X2 be uniform on the consecutive integers -n, -(n+1), ... , n-1, n. Use convolution to find the mass function for X1 + X2.
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
Present an O(n) algorithm that sorts n positive integer numbers a1, a2, . . . ,...
Present an O(n) algorithm that sorts n positive integer numbers a1, a2, . . . , an which are known to be bounded by n 2 (so ai ≤ n 2 , for every i = 1, . . . , n. Use the idea of Radix Sort (discussed in class and presented in Section 8.3 in the textbook). Illustrate your algorithm by showing on paper similar to Fig. 8.3, page 198 in the textbook (make sure you indicate clearly...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT