Manioc

wheysort

May 9th, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. package sorting.divideAndConquer.threeWayQuicksort;
  2.  
  3. import java.util.Random;
  4. import java.util.concurrent.ThreadLocalRandom;
  5.  
  6. import sorting.AbstractSorting;
  7. import util.Util;
  8.  
  9. public class ThreeWayQuickSort<T extends Comparable<T>> extends
  10.         AbstractSorting<T> {
  11.  
  12.     /**
  13.      * No algoritmo de quicksort, selecionamos um elemento como pivot,
  14.      * particionamos o array colocando os menores a esquerda do pivot
  15.      * e os maiores a direita do pivot, e depois aplicamos a mesma estrategia
  16.      * recursivamente na particao a esquerda do pivot e na particao a direita do pivot.
  17.      *
  18.      * Considerando um array com muitoe elementos repetidos, a estrategia do quicksort
  19.      * pode ser otimizada para lidar de forma mais eficiente com isso. Essa melhoria
  20.      * eh conhecida como quicksort tree way e consiste da seguinte ideia:
  21.      * - selecione o pivot e particione o array de forma que
  22.      *   * arr[l..i] contem elementos menores que o pivot
  23.      *   * arr[i+1..j-1] contem elementos iguais ao pivot.
  24.      *   * arr[j..r] contem elementos maiores do que o pivot.
  25.      *  
  26.      * Obviamente, ao final do particionamento, existe necessidade apenas de ordenar
  27.      * as particoes contendo elementos menores e maiores do que o pivot. Isso eh feito
  28.      * recursivamente.
  29.      **/
  30.     public void shuffle(T[] array, int leftIndex, int rightIndex){
  31.         Random rnd = ThreadLocalRandom.current();
  32.         for (int i = rightIndex; i >= leftIndex; i--){
  33.           int index = rnd.nextInt(i + 1);
  34.           T valor = array[index];
  35.           array[index] = array[i];
  36.           array[i] = valor;
  37.         }
  38.      }
  39.    
  40.     public void waysort(T[]array, int leftIndex, int rightIndex) {
  41.         if(leftIndex < rightIndex) {
  42.             T pivot = array[leftIndex];
  43.            
  44.             int low = leftIndex, high = rightIndex;
  45.            
  46.             int i = leftIndex;
  47.             while(i <= high) {
  48.                 int comp = array[i].compareTo(pivot);
  49.                 if(comp < 0) Util.swap(array, low++, i++);
  50.                 else if(comp > 0) Util.swap(array,  i, high--);
  51.                 else i++;
  52.             }
  53.            
  54.             sort(array, leftIndex, low-1);
  55.             sort(array, high+1, rightIndex);
  56.         }
  57.     }
  58.    
  59.     @Override
  60.     public void sort(T[]array, int leftIndex, int rightIndex) {
  61.         if(leftIndex < rightIndex && leftIndex >= 0 && rightIndex >= 0) {
  62.             shuffle(array, leftIndex, rightIndex);
  63.             waysort(array, leftIndex, rightIndex);
  64.         }
  65.     }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment