quick Sort - Java Data Structure

Java examples for Data Structure:Sort

Description

quick Sort

Demo Code


public class Main {

  public static void quickSort(int A[]) {

    quickSort(A, 0, A.length - 1);/*w ww.  ja v a2 s.  c o m*/
  }

  private static void quickSort(int A[], int start, int end) {

    if (start == end)
      return;

    int q = partition(A, start, end);
    quickSort(A, start, q);
    quickSort(A, q + 1, end);
  }

  private static int partition(int A[], int start, int end) {

    int i = start - 1;
    int x = A[end];

    for (int j = start; j < end; j++) {
      if (A[j] < x) {
        int temp = A[j];
        A[j] = A[i + 1];
        A[i + 1] = temp;
        i++;
      }
    }
    int temp = A[end];
    A[end] = A[i + 1];
    A[i + 1] = temp;
    return i;

  }
}

Related Tutorials