Fast Quick Sort : Sort Search « Collections Data Structure « Java






Fast Quick Sort

    
// Copyright (c) 2003-2009, Jodd Team (jodd.org). All Rights Reserved.


import java.util.Comparator;


/**
 * Maybe the fastest implementation of famous Quick-Sort
 * algorithm. It is even faster than Denisa Ahrensa implementation that
 * performs 7.5s for sorting million objects, this implementation
 * sorts for 6.8s. However, {@link FastMergeSort} is much faster.     
 */
public class FastQuickSort  {
  
  @SuppressWarnings({"unchecked"})
  public static void qsort(Object[] c, Comparator comparator) {
    int i, j, left = 0, right = c.length - 1, stack_pointer = -1;
    int[] stack = new int[128];
    Object swap, temp;
    while (true) {
      if (right - left <= 7) {
        for (j = left + 1; j <= right; j++) {
          swap = c[j];
          i = j - 1;
          while (i >= left && comparator.compare(c[i], swap) > 0) {
            c[i + 1] = c[i--];
          }
          c[i + 1] = swap;
        }
        if (stack_pointer == -1) {
          break;
        }
        right = stack[stack_pointer--];
        left = stack[stack_pointer--];
      } else {
        int median = (left + right) >> 1;
        i = left + 1;
        j = right;
        swap = c[median]; c[median] = c[i]; c[i] = swap;
        if (comparator.compare(c[left], c[right]) > 0) {
          swap = c[left]; c[left] = c[right]; c[right] = swap;
        }
        if (comparator.compare(c[i], c[right]) > 0) {
          swap = c[i]; c[i] = c[right]; c[right] = swap;
        }
        if (comparator.compare(c[left], c[i]) > 0) {
          swap = c[left]; c[left] = c[i]; c[i] = swap;
        }
        temp = c[i];
        while (true) {
          //noinspection ControlFlowStatementWithoutBraces,StatementWithEmptyBody
          while (comparator.compare(c[++i], temp) < 0);
          //noinspection ControlFlowStatementWithoutBraces,StatementWithEmptyBody
          while (comparator.compare(c[--j], temp) > 0);
          if (j < i) {
            break;
          }
          swap = c[i]; c[i] = c[j]; c[j] = swap;
        }
        c[left + 1] = c[j];
        c[j] = temp;
        if (right - i + 1 >= j - left) {
          stack[++stack_pointer] = i;
          stack[++stack_pointer] = right;
          right = j - 1;
        } else {
          stack[++stack_pointer] = left;
          stack[++stack_pointer] = j - 1;
          left = i;
        }
      }
    }
  } 

  public void sort(Object a[], Comparator comparator) {
    qsort(a, comparator);
  }
  
  public void sort(Comparable a[]) {
    qsort(a, new ComparableComparator());
  }

  // ---------------------------------------------------------------- static

  public static void doSort(Object a[], Comparator comparator) {
    qsort(a, comparator);
  }

  public static void doSort(Comparable a[]) {
    qsort(a, ComparableComparator.INSTANCE);
  }

}

// Copyright (c) 2003-2009, Jodd Team (jodd.org). All Rights Reserved.

/**
 * Comparator that adapts <code>Comparables</code> to the
 * <code>Comparator</code> interface.
 */
class ComparableComparator<T extends Comparable<T>> implements Comparator<T> {

  /**
   * Cached instance.
   */
  public static final ComparableComparator INSTANCE = new ComparableComparator();

  public int compare(T o1, T o2) {
    return o1.compareTo(o2);
  }

}

   
    
    
    
  








Related examples in the same category

1.Linear search
2.Animation for quick sort
3.Quick Sort Implementation with median-of-three partitioning and cutoff for small arrays
4.Simple Sort DemoSimple Sort Demo
5.A simple applet class to demonstrate a sort algorithm
6.Sorting an array of StringsSorting an array of Strings
7.Simple version of quick sortSimple version of quick sort
8.Combine Quick Sort Insertion SortCombine Quick Sort Insertion Sort
9.Quick sort with median-of-three partitioningQuick sort with median-of-three partitioning
10.Selection sortSelection sort
11.Insert Sort for objectsInsert Sort for objects
12.Insert sortInsert sort
13.Bubble sortBubble sort
14.Merge sortMerge sort
15.Fast Merge Sort
16.Binary SearchBinary Search
17.Shell sortShell sort
18.Recursive Binary Search Implementation in Java
19.Topological sortingTopological sorting
20.Heap sortHeap sort
21.Sort NumbersSort Numbers
22.A quick sort demonstration algorithmA quick sort demonstration algorithm
23.Performing Binary Search on Java byte Array Example
24.Performing Binary Search on Java char Array Example
25.Performing Binary Search on Java double Array Example
26.Performing Binary Search on Java float Array Example
27.Performing Binary Search on Java int Array Example
28.Performing Binary Search on Java long Array Example
29.Performing Binary Search on Java short Array
30.Sort items of an array
31.Sort an array of objects
32.Sort a String array
33.Sort string array with Collator
34.Binary search routines
35.FastQ Sorts the [l,r] partition (inclusive) of the specfied array of Rows, using the comparator.FastQ Sorts the [l,r] partition (inclusive) of the specfied array of Rows, using the comparator.
36.A binary search implementation.
37.Handles QuickSort and all of its methods.
38.Implements QuickSort three different ways
39.A quick sort algorithm to sort Vectors or arrays. Provides sort and binary search capabilities.A quick sort algorithm to sort Vectors or arrays. Provides sort and binary search capabilities.
40.Returns an array of indices indicating the order the data should be sorted in.