似水如衡

财上平如水 人中直似衡

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
 1 package com.learning.algorithm;
 2 
 3 public class QuickSort {
 4     
 5     public int getMiddle(int[] arrValues, int low, int high){
 6         int tempValue = arrValues[low];
 7         while(low<high){
 8             while(low<high && tempValue<=arrValues[high]){
 9                 high--;
10             }
11             arrValues[low]=arrValues[high];
12             
13             while(low<high && tempValue>=arrValues[low]){
14                 low++;
15             }
16             arrValues[high]=arrValues[low];
17             
18             arrValues[low] = tempValue;
19             
20         }
21         return low;
22     }
23     
24     public void quickSort(int[] arrValues, int low, int high){
25         if(low<high){
26             int middle = getMiddle(arrValues,low,high);
27             quickSort(arrValues,low,middle-1);
28             quickSort(arrValues,middle+1,high);
29         }
30         
31     }
32     
33     public void quick(int[] arrValues){
34         if(arrValues.length>0){
35             quickSort(arrValues,0,arrValues.length-1);
36         }
37     }
38 
39     /**
40      * @param args
41      */
42     public static void main(String[] args) {
43         int[] arrValues = {89,39,56,93,2,58,33,43,51,33,67};
44         QuickSort qs = new QuickSort();
45         qs.quick(arrValues);
46         for(int i=0;i<arrValues.length;i++){
47             System.out.print(arrValues[i]);
48             System.out.print(",");
49         }
50         
51 
52     }
53 
54 }

 

posted on 2014-08-23 17:31  似水如衡  阅读(134)  评论(0)    收藏  举报