用java实现的快速排序

来源:互联网 发布:北大光华知乎 编辑:程序博客网 时间:2024/05/23 13:56
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] + " ");
}
}


}




原创粉丝点击