Quick Sort
Quick Sort
ALGORITHM:
2. Make a pass to the array, called the PARTITION step, which rearranges the elements in the
array:
3. Recursively apply the above process to the left and right part of the pivot element.
CODE:
if (A == null || A.length == 0)
return;
int i = left;
int j = right;
while (i <= j) {
i++;
if (i <= j) {
exchange(i, j);
i++;
j--;
if(left < j)
quicksort(A,left,j);
quicksort(A,i,right);
int temp=A[i];
A[i]=A[j];
A[j]=temp;
String s = "";
s += "[" + A[0];
s += "]";
return s;
Complexity of QuickSort
This happens when the pivot is the smallest or the largest element. Then one of the partition is
empty and we repeat the recursion for N-1 elements
This is when the pivot is the median of the array and the left and right part are the of the same
size.There are logN partitions and to compare we do N comparisions
Advantages
Disadvantages
* Merge Sort guarantee O(NlogN) time, however it requires additional memory with size N.
* Quick Sort doesn't require additional memory but the running time is not guaranteed.
* Usually Merge Sort is not used for Memory Sorting. Its used only for external memory sorting.