public class QuickSort {
public static void main(String[] args){
int length = 10;
int[] a = new int[length];
QuickSort qs = new QuickSort();
qs.quicksort(a, 0, length-1);
for(int i=0; i<length; i++) {
System.out.println(a[i]);
}
}
public void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public int partition(int[] a, int low, int high) {
int pivot = a[low];
while(low<high) {
while(low<high && a[high]>=pivot) --high;
swap(a, low, high);
while(low<high && a[low]<=pivot) ++low;
swap(a, low, high);
}
return low;
}
public void quicksort(int a[], int low, int high) {
if(low<high){
int pivotloc = partiton(a, low, high);
quicksort(a, low, pivotloc-1);
quicksort(a, pivotloc+1, high);
}
}
}
快速排序 Java
本文介绍了一个简单的快速排序算法实现过程。通过定义交换元素位置的方法和分区操作,实现了递归快速排序,最后展示了排序后的数组输出。

被折叠的 条评论
为什么被折叠?



