码迷,mamicode.com
首页 > 其他好文 > 详细

快速排序

时间:2014-08-23 18:55:51      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   for   ar   div   log   amp   new   

 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 }

 

快速排序

标签:style   blog   color   for   ar   div   log   amp   new   

原文地址:http://www.cnblogs.com/ryanwangblog/p/3931528.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!