Sort--快速排序

时间:2023-03-09 14:51:11
Sort--快速排序

快速排序

 1 public class QuickSort{
2
3 public static int Partition(int[] a,int low,int high){
4 int pivotkey=a[low];
5 while(low<high){
6 while(pivotkey<=a[high]&&low<high) high--;
7 a[low++]=a[high];
8 while(pivotkey>=a[low]&&low<high) low++;
9 a[high--]=a[low];
10 }
11 a[low]=pivotkey;
12 return low;
13 }
14
15 public static void quicksort(int[] a,int low,int high){
16 int pivotkey;
17 if(low<high){
18 pivotkey = Partition(a,low,high);
19 quicksort(a,low,pivotkey-1);
20 quicksort(a,pivotkey+1,high);
21 }
22 }
23 public static void main(String[] args){
24 int[] a={49,38,65,97,76,13,27};
25 quicksort(a,0,a.length-1);
26 for(int i=0;i<a.length;i++){
27 System.out.println(a[i]);
28 }
29 }
30 }