码迷,mamicode.com
首页 > 编程语言 > 详细

快速排序

时间:2017-07-17 20:02:48      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:char   ring   quick   sys   system   print   algo   public   ack   

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);
}
}

快速排序

标签:char   ring   quick   sys   system   print   algo   public   ack   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!