Fast sorting algorithm implemented in Java

From , 4 Years ago, written in Java, viewed 50 times.
URL https://pastebin.vip/view/b928fec5
  1.  import java.util.Arrays;
  2.  import java.util.Random;
  3.  
  4.  public class QuickSort {
  5.      public static final Random RND = new Random();
  6.  
  7.      private static void swap(Object[] array, int i, int j) {
  8.          Object tmp = array[i];
  9.          array[i] = array[j];
  10.          array[j] = tmp;
  11.      }
  12.  
  13.      private static <E extends Comparable<? super E>> int partition(E[] array, int begin, int end) {
  14.          int index = begin + RND.nextInt(end - begin + 1);
  15.          E pivot = array[index];
  16.          swap(array, index, end);
  17.          for (int i = index = begin; i < end; ++i) {
  18.              if (array[i].compareTo(pivot) <= 0) {
  19.                  swap(array, index++, i);
  20.              }
  21.          }
  22.          swap(array, index, end);
  23.          return (index);
  24.      }
  25.  
  26.      private static <E extends Comparable<? super E>> void qsort(E[] array, int begin, int end) {
  27.          if (end > begin) {
  28.              int index = partition(array, begin, end);
  29.              qsort(array, begin, index - 1);
  30.              qsort(array, index + 1, end);
  31.          }
  32.      }
  33.  
  34.      public static <E extends Comparable<? super E>> void sort(E[] array) {
  35.          qsort(array, 0, array.length - 1);
  36.      }
  37.  
  38.      // Example uses
  39.      public static void main(String[] args) {
  40.          Integer[] l1 = { 5, 1024, 1, 88, 0, 1024 };
  41.          System.out.println("l1  start:" + Arrays.toString(l1));
  42.          QuickSort.sort(l1);
  43.          System.out.println("l1 sorted:" + Arrays.toString(l1));
  44.  
  45.          String[] l2 = { "gamma", "beta", "alpha", "zoolander" };
  46.          System.out.println("l2  start:" + Arrays.toString(l2));
  47.          QuickSort.sort(l2);
  48.          System.out.println("l2 sorted:" + Arrays.toString(l2));
  49.      }
  50.  }
  51.  
  52. Analogously to the Erlang solution above, a user-supplied {{Javadoc:SE|java/util|Comparator}} determines the partial ordering of array elements:
  53.  
  54.  import java.util.Comparator;
  55.  import java.util.Random;
  56.  
  57.  public class Quicksort {
  58.      public static final Random RND = new Random();    
  59.      private static void swap(Object[] array, int i, int j) {
  60.          Object tmp = array[i];
  61.          array[i] = array[j];
  62.          array[j] = tmp;
  63.      }
  64.      private static <E> int partition(E[] array, int begin, int end, Comparator<? super E> cmp) {
  65.          int index = begin + RND.nextInt(end - begin + 1);
  66.          Object pivot = array[index];
  67.          swap(array, index, end);      
  68.          for (int i = index = begin; i < end; ++ i) {
  69.              if (cmp.compare(array[i], pivot) <= 0) {
  70.                  swap(array, index++, i);
  71.              }
  72.          }
  73.          swap(array, index, end);      
  74.          return (index);
  75.      }
  76.      private static <E> void qsort(E[] array, int begin, int end, Comparator<? super E> cmp) {
  77.          if (end > begin) {
  78.              int index = partition(array, begin, end, cmp);
  79.              qsort(array, begin, index - 1, cmp);
  80.              qsort(array, index + 1,  end,  cmp);
  81.          }
  82.      }
  83.      public static <E> void sort(E[] array, Comparator<? super E> cmp) {
  84.          qsort(array, 0, array.length - 1, cmp);
  85.      }
  86.  }
  87.  
  88. //java/4529

Reply to "Fast sorting algorithm implemented in Java"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website