现在的位置: 首页 > 综合 > 正文

用java实现的快速排序

2018年05月28日 ⁄ 综合 ⁄ 共 661字 ⁄ 字号 评论关闭

package com.phj.math;

public class QuickSort {

public static void main(String[] args) {
Quick qs = new Quick();
int data[] = { 44, 22, 2, 32, 54, 22, 88, 77, 99, 11 };
qs.data = data;
qs.sort(0, qs.data.length - 1);
qs.display();

}

}

class Quick {
public int[] data = null;

public int partition(int array[], int low, int high) {
int key = array[low];
while (low < high) {
while (low < high && array[high] >= key)
high--;
array[low] = array[high];
while (low < high && array[low] <= key)
low++;
array[high] = array[low];
}
array[low] = key;
return low;
}

public void sort(int low, int hight) {
if (low < hight) {
int result = partition(data, low, hight);
sort(low, result - 1);
sort(result + 1, hight);
}
}

public void display() {
for (int i = 0; i < data.length; i++) {
System.out.print(data[i] + " ");
}
}

}

抱歉!评论已关闭.