首页 > 编程语言 > 详细

快速排序

时间:2017-07-17 19:43:41      阅读:235      评论:0      收藏:0      [点我收藏+]

package com.charles.algorithm;

public class QuickSort {

public static void main(String[] args) {

int[] data = new int[] { 1, 4, 7, 9, 0 };
quickSort(data, data.length - 1, 0);
for (int index : data) {
System.out.print(index + " ");
}
}

//make the first item to be index
public static void quickSort(int[] data, int high, int low) {
if (low >= high) {
return;
}
int h = high, l = low;
int index = data[low];
while (low < high) {
while (low < high && data[high] >= index) {
high--;
}
data[low] = data[high];
while (low < high && data[low] <= index) {
low++;
}
data[high] = data[low];
}
data[low] = index;
quickSort(data, h, low + 1);
quickSort(data, low - 1, l);
}
//make the middle item to be index
public static void quickSort2(int[] data, int high, int low) {
if (low >= high) {
return;
}
int h = high, l = low;
int middle = (high + low) / 2;
int index = data[middle];
while (low < high) {
while (low < high && data[low] <= index) {
low++;
}
data[middle] = data[high];
data[high] = data[low];
while (high > low && data[high] >= index) {
high--;
}
data[low] = data[high];
}
data[low] = index;
quickSort2(data, h, low + 1);
quickSort2(data, low - 1, l);
}
}

快速排序

原文:http://www.cnblogs.com/julygift/p/7196963.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!