# QuickSort

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?

Recurrence is T(n) = T(n-2) + O(n) and time complexity is O(n^2) | |

Recurrence is T(n) = T(n-1) + O(n) and time complexity is O(n^2) | |

Recurrence is T(n) = 2T(n/2) + O(n) and time complexity is O(nLogn) | |

Recurrence is T(n) = T(n/10) + T(9n/10) + O(n) and time complexity is O(nLogn) |

**Analysis of Algorithms**

**Sorting**

**QuickSort**

**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.

O(n^2 Logn) | |

O(n^2) | |

O(n Logn Logn) | |

O(nLogn) |

**Analysis of Algorithms**

**Sorting**

**QuickSort**

**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.

Insertion Sort | |

Merge Sort | |

Quick Sort | |

Bubble Sort |

**Sorting**

**QuickSort**

**InsertionSort**

**MergeSort**

**Discuss it**

Question 3 Explanation:

See following for details.
http://www.geeksforgeeks.org/stability-in-sorting-algorithms/

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).

Quick Sort | |

Heap Sort | |

Merge Sort | |

Insertion Sort |

**Sorting**

**QuickSort**

**InsertionSort**

**HeapSort**

**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 lienear 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?

Insertion Sort with time complexity O(kn) | |

Heap Sort with time complexity O(nLogk) | |

Quick Sort with time complexity O(kLogk) | |

Merge Sort with time complexity O(kLogk) |

**Analysis of Algorithms**

**Sorting**

**QuickSort**

**HeapSort**

**Discuss it**

Question 5 Explanation:

See http://www.geeksforgeeks.org/nearly-sorted-algorithm/ for explanation and implementation.

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?

The pivot could be either the 7 or the 9. | |

The pivot could be the 7, but it is not the 9 | |

The pivot is not the 7, but it could be the 9 | |

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?

Heap sort | |

Merge sort | |

Quick sort | |

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 | |

B | |

C | |

D |

**Analysis of Algorithms**

**Sorting**

**QuickSort**

**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

T(n) <= 2T(n/5) + n | |

T(n) <= T(n/5) + T(4n/5) + n | |

T(n) <= 2T(4n/5) + n | |

T(n) <= 2T(n/2) + n |

**Analysis of Algorithms**

**Sorting**

**QuickSort**

**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?

Quick sort | |

Heap sort | |

Merge sort | |

Counting sort |

**Sorting**

**QuickSort**

**HeapSort**

**CountingSort**

**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