# QuickSort

• Last Updated : 28 Jun, 2021

Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways … More on Quick Sort

 Question 1
What is recurrence for worst case of QuickSort and what is the time complexity in Worst case?
 A Recurrence is T(n) = T(n-2) + O(n) and time complexity is O(n^2) B Recurrence is T(n) = T(n-1) + O(n) and time complexity is O(n^2) C Recurrence is T(n) = 2T(n/2) + O(n) and time complexity is O(nLogn) D Recurrence is T(n) = T(n/10) + T(9n/10) + O(n) and time complexity is O(nLogn)
Sorting    QuickSort    Analysis of Algorithms
Discuss it

Question 1 Explanation:
The worst case of QuickSort occurs when the picked pivot is always one of the corner elements in sorted array. In worst case, QuickSort recursively calls one subproblem with size 0 and other subproblem with size (n-1). So recurrence is T(n) = T(n-1) + T(0) + O(n) The above expression can be rewritten as T(n) = T(n-1) + O(n) void exchange(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } int partition(int arr[], int si, int ei) { int x = arr[ei]; int i = (si - 1); int j; for (j = si; j <= ei - 1; j++) { if(arr[j] <= x) { i++; exchange(&arr[i], &arr[j]); } } exchange (&arr[i + 1], &arr[ei]); return (i + 1); } /* Implementation of Quick Sort arr[] --> Array to be sorted si --> Starting index ei --> Ending index */ void quickSort(int arr[], int si, int ei) { int pi; /* Partitioning index */ if(si < ei) { pi = partition(arr, si, ei); quickSort(arr, si, pi - 1); quickSort(arr, pi + 1, ei); } } [/sourcecode]
 Question 2
Suppose we have a O(n) time algorithm that finds median of an unsorted array. Now consider a QuickSort implementation where we first find median using the above algorithm, then use median as pivot. What will be the worst case time complexity of this modified QuickSort.
 A O(n^2 Logn) B O(n^2) C O(n Logn Logn) D O(nLogn)
Sorting    QuickSort    Analysis of Algorithms
Discuss it

Question 2 Explanation:
If we use median as a pivot element, then the recurrence for all cases becomes T(n) = 2T(n/2) + O(n) The above recurrence can be solved using Master Method. It falls in case 2 of master method.
 Question 3
Which of the following is not a stable sorting algorithm in its typical implementation.
 A Insertion Sort B Merge Sort C Quick Sort D Bubble Sort
Sorting    QuickSort    InsertionSort    MergeSort
Discuss it

Question 3 Explanation:
 Question 4

Which of the following sorting algorithms in its typical implementation gives best performance when applied on an array which is sorted or almost sorted (maximum 1 or two elements are misplaced).

 A Quick Sort B Heap Sort C Merge Sort D Insertion Sort
Sorting    QuickSort    HeapSort    InsertionSort
Discuss it

Question 4 Explanation:

Insertion sort takes linear time when input array is sorted or almost sorted (maximum 1 or 2 elements are misplaced). All other sorting algorithms mentioned above will take more than linear time in their typical implementation.

 Question 5
Given an unsorted array. The array has this property that every element in array is at most k distance from its position in sorted array where k is a positive integer smaller than size of array. Which sorting algorithm can be easily modified for sorting this array and what is the obtainable time complexity?
 A Insertion Sort with time complexity O(kn) B Heap Sort with time complexity O(nLogk) C Quick Sort with time complexity O(kLogk) D Merge Sort with time complexity O(kLogk)
Sorting    QuickSort    Analysis of Algorithms    HeapSort
Discuss it

Question 5 Explanation:
1) to sort the array firstly create a min-heap with first k+1 elements and a separate array as resultant array. 2) because elements are at most k distance apart from original position so, it is guranteed that the smallest element will be in this K+1 elements. 3) remove the smallest element from the min-heap(extract min) and put it in the result array. 4) Now,insert another element from the unsorted array into the mean-heap, now,the second smallest element will be in this, perform extract min and continue this process until no more elements are in the unsorted array.finally, use simple heap sort for the remaining elements Time Complexity ------------------------ 1) O(k) to build the initial min-heap 2) O((n-k)logk) for remaining elements... 3) 0(1) for extract min so overall O(k) + O((n-k)logk) + 0(1) = O(nlogk)
 Question 6
Suppose we are sorting an array of eight integers using quicksort, and we have just finished the first partitioning with the array looking like this:
2  5  1  7  9  12  11  10

Which statement is correct?
 A The pivot could be either the 7 or the 9. B The pivot could be the 7, but it is not the 9 C The pivot is not the 7, but it could be the 9 D Neither the 7 nor the 9 is the pivot.
Sorting    QuickSort
Discuss it

Question 6 Explanation:
7 and 9 both are at their correct positions (as in a sorted array). Also, all elements on left of 7 and 9 are smaller than 7 and 9 respectively and on right are greater than 7 and 9 respectively.
 Question 7
You have to sort 1 GB of data with only 100 MB of available main memory. Which sorting technique will be most appropriate?
 A Heap sort B Merge sort C Quick sort D Insertion sort
Sorting    QuickSort    MergeSort    HeapSort
Discuss it

Question 7 Explanation:
The data can be sorted using external sorting which uses merging technique. This can be done as follows: 1. Divide the data into 10 groups each of size 100. 2. Sort each group and write them to disk. 3. Load 10 items from each group into main memory. 4. Output the smallest item from the main memory to disk. Load the next item from the group whose item was chosen. 5. Loop step #4 until all items are not outputted. The step 3-5 is called as merging technique.
 Question 8
In quick sort, for sorting n elements, the (n/4)th smallest element is selected as pivot using an O(n) time algorithm. What is the worst case time complexity of the quick sort? (A) (n) (B) (nLogn) (C) (n^2) (D) (n^2 log n)
 A A B B C C D D
Sorting    QuickSort    Analysis of Algorithms
Discuss it

Question 8 Explanation:
The recursion expression becomes: T(n) = T(n/4) + T(3n/4) + cn After solving the above recursion, we get (nLogn).
 Question 9
Consider the Quicksort algorithm. Suppose there is a procedure for finding a pivot element which splits the list into two sub-lists each of which contains at least one-fifth of the elements. Let T(n) be the number of comparisons required to sort n elements. Then
 A T(n) <= 2T(n/5) + n B T(n) <= T(n/5) + T(4n/5) + n C T(n) <= 2T(4n/5) + n D T(n) <= 2T(n/2) + n
Sorting    QuickSort    Analysis of Algorithms
Discuss it

Question 9 Explanation:
For the case where n/5 elements are in one subset, T(n/5) comparisons are needed for the first subset with n/5 elements, T(4n/5) is for the rest 4n/5 elements, and n is for finding the pivot. If there are more than n/5 elements in one set then other set will have less than 4n/5 elements and time complexity will be less than T(n/5) + T(4n/5) + n because recursion tree will be more balanced.
 Question 10
Which sorting algorithms is most efficient to sort string consisting of ASCII characters?
 A Quick sort B Heap sort C Merge sort D Counting sort
Sorting    QuickSort    CountingSort    HeapSort
Discuss it

Question 10 Explanation:
Counting sort algorithm is efficient when range of data to be sorted is fixed. In the above question, the range is from 0 to 255(ASCII range). Counting sort uses an extra constant space proportional to range of data.
There are 28 questions to complete.

## Coding practice for sorting

My Personal Notes arrow_drop_up