Quick sort: uses an insertion sort to handle subarrays of fewer than 10 cells : Sort « Collections « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Collections » Sort 
9.53.10.Quick sort: uses an insertion sort to handle subarrays of fewer than 10 cellsPrevious/Next
public class MainClass {
  public static void main(String[] args) {
    int[] intArray = 19283746};
    for (int i : intArray) {
      System.out.println(i);
    }
    quickSort(intArray);
    for (int i : intArray) {
      System.out.println(i);
    }
  }

  public static void quickSort(int[] intArray) {
    recQuickSort(intArray, 0, intArray.length - 1);
    insertionSort(intArray, 0, intArray.length - 1);
  }

  public static void recQuickSort(int[] intArray, int left, int right) {
    int size = right - left + 1;
    if (size < 10)
      insertionSort(intArray, left, right);
    else {
      double median = medianOf3(intArray, left, right);
      int partition = partitionIt(intArray, left, right, median);
      recQuickSort(intArray, left, partition - 1);
      recQuickSort(intArray, partition + 1, right);
    }
  }

  public static double medianOf3(int[] intArray, int left, int right) {
    int center = (left + right2;

    if (intArray[left> intArray[center])
      swap(intArray, left, center);

    if (intArray[left> intArray[right])
      swap(intArray, left, right);

    if (intArray[center> intArray[right])
      swap(intArray, center, right);

    swap(intArray, center, right - 1);
    return intArray[right - 1];
  }

  public static void swap(int[] intArray, int dex1, int dex2) {
    int temp = intArray[dex1];
    intArray[dex1= intArray[dex2];
    intArray[dex2= temp;
  }

  public static int partitionIt(int[] intArray, int left, int right, double pivot) {
    int leftPtr = left;
    int rightPtr = right - 1;
    while (true) {
      while (intArray[++leftPtr< pivot)
        ;
      while (intArray[--rightPtr> pivot)
        ;
      if (leftPtr >= rightPtr)
        break;
      else
        swap(intArray, leftPtr, rightPtr);
    }
    swap(intArray, leftPtr, right - 1);
    return leftPtr;
  }

  public static void insertionSort(int[] intArray, int left, int right) {
    int in, out;

    for (out = left + 1; out <= right; out++) {
      int temp = intArray[out];
      in = out;

      while (in > left && intArray[in - 1>= temp) {
        intArray[in= intArray[in - 1];
        --in;
      }
      intArray[in= temp;
    }
  }
}
1
9
2
8
3
7
4
6
5
1
2
3
4
5
6
7
8
9
9.53.Sort
9.53.1.Bubble Sort
9.53.2.Selection Sort
9.53.3.Insertion Sort
9.53.4.Sorting Objects using insertion sort
9.53.5.Mergesort: merging two arrays into a third
9.53.6.Generic Merge Sorter with generic Comparator
9.53.7.Shellsort
9.53.8.Quicksort: simple version of quick sort
9.53.9.Quick sort with median-of-three partitioning
9.53.10.Quick sort: uses an insertion sort to handle subarrays of fewer than 10 cells
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.