Jumat, 08 Juli 2011

quicksort

import jeliot.io.*;

public class QuickSort{
  public static void main(String a[]){
    int i;
    int array[] = {12,9,4,29,7,1,3,10};
    System.out.println("Values Before the sort:\n");
    //for(i = 0; i < array.length; i++)
      //System.out.print( array+"  ");

    System.out.println();
    quickSort(array,0,7);
    System.out.print("Values after the sort:\n");
    for(i = 0; i <array.length; i++)
      System.out.print(array+"  ");
    System.out.println();
    System.out.println("PAUSE");
  }

 public static int partition(int arr[], int left, int right) {
      int i = left, j = right;
      int tmp;
      int pivot = arr[(left + right) / 2];

      while (i <= j) {
            while (arr < pivot)
                  i++;
            while (arr[j] > pivot)
                  j--;
            if (i <= j) {
                  tmp = arr;
                  arr = arr[j];
                  arr[j] = tmp;
                  i++;
                  j--;
            }
      };
      return i;
    }

public static void quickSort(int arr[], int left, int right) {
      int index = partition(arr, left, right);
      if (left < index - 1)
            quickSort(arr, left, index - 1);
      if (index < right)
            quickSort(arr, index, right);
    }
}

0 komentar:

Posting Komentar

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Blogger Theme by Lasantha - Premium Blogger Templates | Affiliate Network Reviews