标签:
package com.javaTest300;
public class Test039 {
public static void main(String[] args) {// 快速排序
int a[]={2,3,8,10,5,1,8,78,35,12};
System.out.println("----排序前-----");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
System.out.println();
System.out.println("----排序后-----");
Quick_Sort(a, 0, a.length-1);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
System.out.println();
}
public static void Quick_Sort(int arr[],int start,int end) {
if(start<end){ //当然这里省略了(arr==nullend-start+1<2)的情况,即传入的数组为空以及数组中只有一个元素的情况
int privote=Partition(arr,start,end);
Quick_Sort(arr, start, privote-1);
Quick_Sort(arr, privote+1, end);
}
}
public static int Partition(int arr[],int low,int high){
int tmp=arr[low];
while(low<high){
while(low<high&&tmp<=arr[high]){
high--;
}
arr[low]=arr[high];
while(low<high&&arr[low]<=tmp){
low++;
}
arr[high]=arr[low];
}
arr[low]=tmp; return low;
}
}
标签:
原文地址:http://www.cnblogs.com/ysw-go/p/4878742.html