## Friday, 21 February 2014

### Quick Sort in Java

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

1. Pick an element, called a pivot, from the array.
2. 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.
3. 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]

#### Complexity

Quick sort like merge sort has an average complexity of O(Nlog N)

#### Other Info

As of Perl 5.8, merge sort is its default sorting algorithm (it was quicksort in previous versions of Perl). In Java, the Arrays.sort() methods use merge sort or a tuned quicksort depending on the datatypes and for implementation efficiency switch to insertion sort when fewer than seven array elements are being sorted. Python uses Timsort, another tuned hybrid of merge sort and insertion sort, that has become the standard sort algorithm in Java SE & on the Android platform, and in GNU Octave.