排序有哪几种方法?请列举。并用 JAVA 实现一个快速排序

 我来答
堵寄灵kO
2014-05-15 · TA获得超过2370个赞
知道小有建树答主
回答量:588
采纳率:100%
帮助的人:463万
展开全部
  插入排序,选择排序,冒泡排序,快速排序等
  快速排序
public class QuickSort {
public static void main(String[] args) {
int[] array = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3 };
System.out.println("Before sort:");
ArrayUtils.printArray(array);
quickSort(array);
System.out.println("After sort:");
ArrayUtils.printArray(array);
}
public static void quickSort(int[] array) {
subQuickSort(array, 0, array.length - 1);
}
private static void subQuickSort(int[] array, int start, int end) {
if (array == null || (end - start + 1) < 2) {
return;
}
int part = partition(array, start, end);
if (part == start) {
subQuickSort(array, part + 1, end);
} else if (part == end) {
subQuickSort(array, start, part - 1);
} else {
subQuickSort(array, start, part - 1);
subQuickSort(array, part + 1, end);
}
}
private static int partition(int[] array, int start, int end) {
int value = array[end];
int index = start - 1;
for (int i = start; i < end; i++) {
if (array[i] < value) {
index++;
if (index != i) {
ArrayUtils.exchangeElements(array, index, i);
}
}
}
if ((index + 1) != end) {
ArrayUtils.exchangeElements(array, index + 1, end);
}
return index + 1;
}
}
百度网友9acb421
2014-05-15 · TA获得超过243个赞
知道小有建树答主
回答量:327
采纳率:0%
帮助的人:243万
展开全部
http://www.oschina.net/code/snippet_120166_6102

我觉得上边的这个总结的不错哟,可以参考一下啦.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式