Skip to content

Commit

Permalink
Added QuickSort example
Browse files Browse the repository at this point in the history
  • Loading branch information
onkar committed Sep 7, 2014
1 parent 2ca7939 commit 5ca04b1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
51 changes: 51 additions & 0 deletions Sorting/src/main/QuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main;

/**
* Best case time complexity : O(nlogn) when the pivoting logic always picks the medium element as
* the pivot. Worst case time complexity : O(n^2) when the pivoting logic always picks a smallest or
* the largest element as the pivot. Also, when the array is already sorted in ascending or
* descending order. Average case time complexity : O(nlogn). Space complexity : O(n).
*
* While quick sort works for most of the real world problems, merge sort is preferred when the data
* is huge and stored in external memory (i.e. main memory is not sufficient for storing the data).
*
* @author [email protected] (Onkar Deshpande)
*
*/
public class QuickSort {
private final int[] array;

public QuickSort(int[] array) {
this.array = array;
}

public void sort(int low, int high) {
if (low < high) {
// Get the partition
int p = partition(low, high);
// Recursively sort both halves of the array except the partitioned element
sort(low, p - 1);
sort(p + 1, high);
}
}

private int partition(int low, int high) {
// Pick the right most element as the pivot
int pivot = array[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
i++;
swap(i, j);
}
}
swap(i + 1, high);
return i + 1;
}

private void swap(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
18 changes: 13 additions & 5 deletions Sorting/src/test/SortingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import main.HeapSort;
import main.InsertionSort;
import main.MergeSort;
import main.QuickSort;
import main.SelectionSort;

import org.junit.Before;
Expand All @@ -31,10 +32,10 @@ public void setup() throws Exception {
public void testInsertionSort() throws Exception {
InsertionSort is = new InsertionSort(numbers, Size);
is.sort();
checkSorting();
testSorting();
}

private void checkSorting() {
private void testSorting() {
for (int i = 0; i < numbers.length - 1; i++) {
if (numbers[i] > numbers[i + 1]) {
fail("Array is not sorted...");
Expand All @@ -47,20 +48,27 @@ private void checkSorting() {
public void testSelectionSort() throws Exception {
SelectionSort ss = new SelectionSort(numbers, Size);
ss.sort();
checkSorting();
testSorting();
}

@Test
public void testMergeSort() throws Exception {
MergeSort ms = new MergeSort();
ms.sort(numbers);
checkSorting();
testSorting();
}

@Test
public void testHeapSort() throws Exception {
HeapSort hs = new HeapSort(numbers, Size);
hs.sort();
checkSorting();
testSorting();
}

@Test
public void testQuickSort() throws Exception {
QuickSort qs = new QuickSort(numbers);
qs.sort(0, Size - 1);
testSorting();
}
}

0 comments on commit 5ca04b1

Please sign in to comment.