首页 技术 正文
技术 2022年11月17日
0 收藏 982 点赞 2,503 浏览 2013 个字

Java QuickSort

/**
* <html>
* <body>
* <P> Copyright 1994-2018 JasonInternational </p>
* <p> All rights reserved.</p>
* <p> Created on 2018年4月10日 </p>
* <p> Created by Jason</p>
* </body>
* </html>
*/
package cn.ucaner.algorithm.sorts;import java.util.Random;/**
* Quicksort is a sorting algorithm which, on average, makes O(n*log n) comparisons to sort
* n items. In the worst case, it makes O(n^2) comparisons, though this behavior is
* rare. Quicksort is often faster in practice than other algorithms.
* <p>
* Family: Divide and conquer.<br>
* Space: In-place.<br>
* Stable: False.<br>
* <p>
* Average case = O(n*log n)<br>
* Worst case = O(n^2)<br>
* Best case = O(n) [three-way partition and equal keys]<br>
* <p>
* @see <a href="https://en.wikipedia.org/wiki/Quick_sort" rel="external nofollow" >Quicksort (Wikipedia)</a>
* <br>
* @author Justin Wetherell <phishman3579@gmail.com>
*/
public class QuickSort<T extends Comparable<T>> { private static final Random RAND = new Random(); public static enum PIVOT_TYPE {
FIRST, MIDDLE, RANDOM
} public static PIVOT_TYPE type = PIVOT_TYPE.RANDOM; private QuickSort() { } public static <T extends Comparable<T>> T[] sort(PIVOT_TYPE pivotType, T[] unsorted) {
int pivot = 0;
if (pivotType == PIVOT_TYPE.MIDDLE) {
pivot = unsorted.length/2;
} else if (pivotType == PIVOT_TYPE.RANDOM) {
pivot = getRandom(unsorted.length);
}
sort(pivot, 0, unsorted.length - 1, unsorted);
return unsorted;
} private static <T extends Comparable<T>> void sort(int index, int start, int finish, T[] unsorted) {
int pivotIndex = start + index;
T pivot = unsorted[pivotIndex];
int s = start;
int f = finish;
while (s <= f) {
while (unsorted[s].compareTo(pivot) < 0)
s++;
while (unsorted[f].compareTo(pivot) > 0)
f--;
if (s <= f) {
swap(s, f, unsorted);
s++;
f--;
}
}
if (start < f) {
pivotIndex = getRandom((f - start) + 1);
sort(pivotIndex, start, f, unsorted);
}
if (s < finish) {
pivotIndex = getRandom((finish - s) + 1);
sort(pivotIndex, s, finish, unsorted);
}
} private static final int getRandom(int length) {
if (type == PIVOT_TYPE.RANDOM && length > 0)
return RAND.nextInt(length);
if (type == PIVOT_TYPE.FIRST && length > 0)
return 0;
return length / 2;
} private static <T extends Comparable<T>> void swap(int index1, int index2, T[] unsorted) {
T index2Element = unsorted[index1];
unsorted[index1] = unsorted[index2];
unsorted[index2] = index2Element;
}
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849