• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

Java实现的快速排序算法

JAVA相关 水墨上仙 1460次浏览

Java实现的快速排序算法

 import java.util.Arrays;
 import java.util.Random;
 
 public class QuickSort {
     public static final Random RND = new Random();
 
     private static void swap(Object[] array, int i, int j) {
         Object tmp = array[i];
         array[i] = array[j];
         array[j] = tmp;
     }
 
     private static <E extends Comparable<? super E>> int partition(E[] array, int begin, int end) {
         int index = begin + RND.nextInt(end - begin + 1);
         E pivot = array[index];
         swap(array, index, end);
         for (int i = index = begin; i < end; ++i) {
             if (array[i].compareTo(pivot) <= 0) {
                 swap(array, index++, i);
             }
         }
         swap(array, index, end);
         return (index);
     }
 
     private static <E extends Comparable<? super E>> void qsort(E[] array, int begin, int end) {
         if (end > begin) {
             int index = partition(array, begin, end);
             qsort(array, begin, index - 1);
             qsort(array, index + 1, end);
         }
     }
 
     public static <E extends Comparable<? super E>> void sort(E[] array) {
         qsort(array, 0, array.length - 1);
     }
 
     // Example uses
     public static void main(String[] args) {
         Integer[] l1 = { 5, 1024, 1, 88, 0, 1024 };
         System.out.println("l1  start:" + Arrays.toString(l1));
         QuickSort.sort(l1);
         System.out.println("l1 sorted:" + Arrays.toString(l1));
 
         String[] l2 = { "gamma", "beta", "alpha", "zoolander" };
         System.out.println("l2  start:" + Arrays.toString(l2));
         QuickSort.sort(l2);
         System.out.println("l2 sorted:" + Arrays.toString(l2));
     }
 }
Analogously to the Erlang solution above, a user-supplied {{Javadoc:SE|java/util|Comparator}} determines the partial ordering of array elements:
 import java.util.Comparator;
 import java.util.Random;
 
 public class Quicksort {
     public static final Random RND = new Random();	
     private static void swap(Object[] array, int i, int j) {
         Object tmp = array[i];
         array[i] = array[j];
         array[j] = tmp;
     }
     private static <E> int partition(E[] array, int begin, int end, Comparator<? super E> cmp) {
         int index = begin + RND.nextInt(end - begin + 1);
         Object pivot = array[index];
         swap(array, index, end);	
         for (int i = index = begin; i < end; ++ i) {
             if (cmp.compare(array[i], pivot) <= 0) {
                 swap(array, index++, i);
             }
         }
         swap(array, index, end);	
         return (index);
     }
     private static <E> void qsort(E[] array, int begin, int end, Comparator<? super E> cmp) {
         if (end > begin) {
             int index = partition(array, begin, end, cmp);
             qsort(array, begin, index - 1, cmp);
             qsort(array, index + 1,  end,  cmp);
         }
     }
     public static <E> void sort(E[] array, Comparator<? super E> cmp) {
         qsort(array, 0, array.length - 1, cmp);
     }
 }


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明Java实现的快速排序算法
喜欢 (0)
加载中……