Saturday, 22 August 2015

Quick sort java

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.

The steps are:
a.   Pick an element, called a pivot, from the array.
b.   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.
c.    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.

Following video explains about quick sort clearly.

import java.util.Arrays;
import java.util.Objects;

public class QuickSort<T extends Comparable<T>> {
 private T[] arr;
 private boolean flag;

 private void swap(int i, int j) {
  T temp = arr[i];
  arr[i] = arr[j];
  arr[j] = temp;
 }

 private void quickSort(int low, int high) {
  if (low < high) {
   int position = partition(low, high);
   quickSort(low, position);
   quickSort(position + 1, high);
  }
 }

 private boolean compare(T i, T j) {
  if (flag == true)
   return i.compareTo(j) < 0;
  return i.compareTo(j) > 0;
 }

 private int partition(int low, int high) {
  int i = low + 1;
  T pivot = arr[low];

  for (int j = low + 1; j < high; j++) {
   if (compare(arr[j], pivot)) {
    swap(i, j);
    i++;
   }
  }

  i--;
  swap(low, i);
  return i;
 }

 /**
  * If flag is true, then elements are sorted in ascending order, else
  * descending order
  */
 public void sort(T arr[], boolean flag) {
  Objects.nonNull(arr);
  this.arr = arr;
  this.flag = flag;
  quickSort(0, arr.length);
 }

 public static void main(String args[]) {
  Integer arr[] = { 1, 3, 5, 7, 2, 4, 6, 8 };

  QuickSort<Integer> quickSort = new QuickSort<>();
  quickSort.sort(arr, true);

  System.out.println(Arrays.toString(arr));

  quickSort.sort(arr, false);
  System.out.println(Arrays.toString(arr));
 }

}


Following is the junit test case for above program.
import static org.junit.Assert.assertTrue;

import java.util.Arrays;

import org.junit.Test;

public class QuickSortTest {
 @Test
 public void test1() {
  QuickSort<Integer> obj1 = new QuickSort<>();

  Integer arr1[] = { 5, 46, 25, 13, 12 };
  Integer arr2[] = { 5, 46 };
  Integer arr3[] = { 5, 4, 3, 2, 1 };
  Integer arr4[] = { 1 };
  Integer arr5[] = { 1, 3, 5, 7, 2, 4, 6, 8 };

  obj1.sort(arr1, false);
  obj1.sort(arr2, false);
  obj1.sort(arr3, false);
  obj1.sort(arr4, false);
  obj1.sort(arr5, false);

  assertTrue(Arrays.equals(arr1, new Integer[] { 46, 25, 13, 12, 5 }));
  assertTrue(Arrays.equals(arr2, new Integer[] { 46, 5 }));
  assertTrue(Arrays.equals(arr3, new Integer[] { 5, 4, 3, 2, 1 }));
  assertTrue(Arrays.equals(arr4, new Integer[] { 1 }));
  assertTrue(Arrays
    .equals(arr5, new Integer[] { 8, 7, 6, 5, 4, 3, 2, 1 }));
 }

 @Test
 public void test2() {
  QuickSort<Integer> obj1 = new QuickSort<>();

  Integer arr1[] = { 5, 46, 25, 13, 12 };
  Integer arr2[] = { 5, 46 };
  Integer arr3[] = { 5, 4, 3, 2, 1 };
  Integer arr4[] = { 1 };
  Integer arr5[] = { 1, 3, 5, 7, 2, 4, 6, 8 };

  obj1.sort(arr1, true);
  obj1.sort(arr2, true);
  obj1.sort(arr3, true);
  obj1.sort(arr4, true);
  obj1.sort(arr5, true);

  assertTrue(Arrays.equals(arr1, new Integer[] { 5, 12, 13, 25, 46 }));
  assertTrue(Arrays.equals(arr2, new Integer[] { 5, 46 }));
  assertTrue(Arrays.equals(arr3, new Integer[] { 1, 2, 3, 4, 5 }));
  assertTrue(Arrays.equals(arr4, new Integer[] { 1 }));
  assertTrue(Arrays
    .equals(arr5, new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8 }));
 }
}


No comments:

Post a Comment