Quick Sort

If list is sorted ascending or descending order, time complexity of quick sort will be O(N^2)

In Java used to sort primitive types.


Improvements

1. Shuffle the input before sort: If array is already sorted in ascending or descending order then the worst case time complexity will be O(N^2). Can can avoid it by shuffling the array before sort. It will guarantee that worst case never happen and always time complexity will be NLogN. Java program

2. Use Insertion sort for small sub array : Quick and merge sorts are good for big array. Even quick sort has too much overhead for tiny subarrays. Cutoff to insertion sort for ≈ 10 items. ・Note: could delay insertion sort until one pass at end. Java program

3. Median of sample : Best choice of pivot item = median. Estimate true median by taking median of sample. Java Program

4. 3-Way partitioning : Quick sort with duplicate keys, Algorithm goes quadratic unless partitioning stops on equal keys! Put all items equal to the partitioning item on one side. ~ ½ N 2 compares when all keys equal. Solution is 3-way partitioning. Java Program


Comments

Popular posts from this blog

SQL basic interview question

gsutil Vs Storage Transfer Service Vs Transfer Appliance