Advertisement
Guest User

Untitled

a guest
May 24th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. package fonte;
  2.  
  3. public class QuickSort {
  4.      
  5.     private int array[];
  6.     private int length;
  7.  
  8.     public void sort(int[] inputArr) {
  9.          
  10.         if (inputArr == null || inputArr.length == 0) {
  11.             return;
  12.         }
  13.         this.array = inputArr;
  14.         length = inputArr.length;
  15.         quickSort(0, length - 1);
  16.     }
  17.  
  18.     private void quickSort(int menorindice, int maiorindice) {
  19.          
  20.         int i = menorindice;
  21.         int j = maiorindice;
  22.         int pivot = array[menorindice+(maiorindice-menorindice)/2];
  23.         while (i <= j) {
  24.             while (array[i] < pivot) {
  25.                 i++;
  26.             }
  27.             while (array[j] > pivot) {
  28.                 j--;
  29.             }
  30.             if (i <= j) {
  31.                 exchangeNumbers(i, j);
  32.                 i++;
  33.                 j--;
  34.             }
  35.         }
  36.         if (menorindice < j)
  37.             quickSort(menorindice, j);
  38.         if (i < maiorindice)
  39.             quickSort(i, maiorindice);
  40.     }
  41.  
  42.     private void exchangeNumbers(int i, int j) {
  43.         int temp = array[i];
  44.         array[i] = array[j];
  45.         array[j] = temp;
  46.     }
  47.      
  48.     public static void main(String a[]){
  49.          
  50.         QuickSort sorter = new QuickSort();
  51.         int[] input = {4, 7, 1, 8, 0, 10, 55, 34, 88};
  52.         sorter.sort(input);
  53.         for(int i:input){
  54.             System.out.print(i);
  55.             System.out.print(" ");
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement