A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 段玉超 中级黑马   /  2012-3-4 12:52  /  1277 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class  QuckSourtDemo
{
        public static void main(String[] args)
        {
               
                int[] list = {19,2,6,8,24,2,37,3,18};
                for(int a : list){
                        System.out.printf("%5d",a);
                }
                partistion(list,0,list.length-1);
                quckSort(list,0,list.length-1);
                System.out.println("\n========================!\n");
                for(int a : list){
                        System.out.printf("%5d",a);
                }

        }
       
        public static void quckSort(int[] list ,int first,int last){
                if(last > first){
                        int pivotIndex = partistion(list,first,last);
                        quckSort(list,first,pivotIndex-1);
                        quckSort(list,pivotIndex+1,last);
                }
        }

        public static int partistion(int[] list,int first ,int last){
                int pivot = list[first];
                int low = first +1;
                int high = last;


                while (high>low)
                {
                        while(low<=high && list[low]<=pivot){
                                low++;
                        }
                        //System.out.print(low);
                        while(low<=high && list[high] > pivot){
                                high--;
                        }
                        //System.out.print(high);
                        if(high > low){
                                int temp = list[high];
                                list[high] = list[low];
                                list[low]  = temp;
                        }
                }

                        while(high > first && list[high] >=pivot)
                        {
                                high--;
                        }

                        if(pivot > list[high]){
                                list[first] = list[high];
                                list[high] = pivot;
                                return high;
                        }else{
                                return first;
                        }
               
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马