Background
Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays.
Steps :
- Pick an element, called a pivot, from the array.
- Partitioning: reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
- Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.
Code :
import java.io.IOException; import java.util.Arrays; /** * Created with IntelliJ IDEA. * User: aniket * Date: 19/2/14 * Time: 8:32 PM */ public class QuickSort { public void quickSort(int[] array, int start, int end){ int i = start; int j = end; int pivot = array[(start + end)/2]; while (i <= j) { while (array[i] < pivot) { i++; } while (array[j] > pivot) { j--; } if (i <= j) { swap(array, i, j); i++; j--; } } if (start < j) quickSort(array, start, j); if (i < end) quickSort(array, i, end); } public static void swap(int[] array, int index1, int index2){ if(index1 == index2) return; int temp = array[index1]; array[index1] = array[index2]; array[index2] = temp; } public static void main(String args[]) throws IOException { int[] array = new int[]{2,7,5,9,4,7,1,0}; System.out.println("Array Before Sort : " + Arrays.toString(array)); new QuickSort().quickSort(array,0,array.length-1); System.out.println("Array after Sort : " + Arrays.toString(array)); } }
Output :
Array Before Sort : [2, 7, 5, 9, 4, 7, 1, 0]
Array after Sort : [0, 1, 2, 4, 5, 7, 7, 9]
Array after Sort : [0, 1, 2, 4, 5, 7, 7, 9]
Complexity
Quick sort like merge sort has an average complexity of O(Nlog N)
If this happens repeatedly in every partition, then each recursive call processes a list of size one less than the previous list. Consequently, we can make n − 1 nested calls before we reach a list of size 1. This means that the call tree is a linear chain of n − 1 nested calls. The ith call does O(n − i) work to do the partition, and , so in that case, Quicksort takes O(n²) time.
Worst-case analysis
The most unbalanced partition occurs when one of the sublists returned by the partitioning routine is of size n − 1. This may occur if the pivot happens to be the smallest or largest element in the list, or in some implementations when all the elements are equal.If this happens repeatedly in every partition, then each recursive call processes a list of size one less than the previous list. Consequently, we can make n − 1 nested calls before we reach a list of size 1. This means that the call tree is a linear chain of n − 1 nested calls. The ith call does O(n − i) work to do the partition, and , so in that case, Quicksort takes O(n²) time.
No comments:
Post a Comment