您的位置:首页 > 其它

Sort--快速排序

2016-05-11 00:00 351 查看
快速排序

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 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: