前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java的快速排序

java的快速排序

作者头像
热心的社会主义接班人
发布2018-08-02 15:18:55
5910
发布2018-08-02 15:18:55
举报
文章被收录于专栏:cscs

写了2个形式的,原理差不多,都是找基数,递归到一个结束。但是细节和交换上有所不同。

相关code

代码语言:javascript
复制
package day20180728;

public class QuickSort{
    
    public static void quickSort(int[] arr,int lt,int rt)
    {
    //只有一个元素的时候,递归出口
        if(lt>=rt)
            return;
        
       int temp=arr[lt];
       int st=lt,end=rt;
       
       while(st<end)
       {
           while(st<end&&temp<=arr[end])
           {
               end--;
           }
           
           while(st<end&&temp>=arr[st])
           {
               st++;
           }
           
           if(st<end)
           {
           int tag=0;
           tag=arr[end];
           arr[end]=arr[st];
           arr[st]=tag;
           }
           
        System.out.println("每一次左右轮换的结果为");
           display(arr);
           System.out.println();
           System.out.println("#########");
       }
        arr[lt]=arr[st];
        arr[st]=temp;
        
        System.out.println("基数为:"+st);
        
        System.out.println("基数定位的结果为:");
        System.out.println("------****-------");
        display(arr);
        System.out.println("++++++++++");
        
       //递归基数的左边部分。
        quickSort(arr,lt,st-1);    
        quickSort(arr,st+1,rt);

    }
    
    
    public static void display(int[] arr)
    {
        
        
        for(int elem:arr)
            System.out.print(elem+" ");
    }
    
    
    
    public static void main(String[] args)
    {
        
        int[] arr= {11,6,8,22,33,78,65,0};
        
        quickSort(arr,0,arr.length-1);
        
    
        
        display(arr);
        

            
    

        
    }
}

结果

代码语言:javascript
复制
每一次左右轮换的结果为
11 6 8 0 33 78 65 22 
#########
每一次左右轮换的结果为
11 6 8 0 33 78 65 22 
#########
基数为:3
基数定位的结果为:
------****-------
0 6 8 11 33 78 65 22 ++++++++++
每一次左右轮换的结果为
0 6 8 11 33 78 65 22 
#########
基数为:0
基数定位的结果为:
------****-------
0 6 8 11 33 78 65 22 ++++++++++
每一次左右轮换的结果为
0 6 8 11 33 78 65 22 
#########
基数为:1
基数定位的结果为:
------****-------
0 6 8 11 33 78 65 22 ++++++++++
每一次左右轮换的结果为
0 6 8 11 33 22 65 78 
#########
每一次左右轮换的结果为
0 6 8 11 33 22 65 78 
#########
基数为:5
基数定位的结果为:
------****-------
0 6 8 11 22 33 65 78 ++++++++++
每一次左右轮换的结果为
0 6 8 11 22 33 65 78 
#########
基数为:6
基数定位的结果为:
------****-------
0 6 8 11 22 33 65 78 ++++++++++
0 6 8 11 22 33 65 78 

另外一个版本 code如下

代码语言:javascript
复制
package day20180728;

public class qSort {
    
    
    public static void ksort(int[] arr,int left,int right)
    {
        if(left>=right)
            return ;
        int tag=arr[left];
        int i=left,j=right;
        
        while(i<j)
        {
            
            while(i<j&&arr[i]<=arr[j])
            {
                j--;
            }
            
            if(i<j)
            {
                int temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
                i++;
            }
            
            while(i<j&&arr[j]>=arr[i])
            {
                i++;
            }
            
            if(i<j)
            {
                int temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
                j--;
            }
                    
        }
        
        System.out.println("基数i为:"+i);
        
        System.out.println("基数定位的结果为:");
        System.out.println("------////-------");
        display(arr);
       
         System.out.println("/////////////");
           //递归基数的左边部分。
         ksort(arr,left,i-1);    
            ksort(arr,i+1,right);
    }
    
    
    public static void display(int[] arr)
    {
        
        
        for(int elem:arr)
            System.out.print(elem+" ");
    }
    

    public static void main(String[] args) {
    
        int[] arr= {11,66,8,22,33,78,65,0};
        ksort(arr,0,arr.length-1);
        
        display(arr);
        
        
    }

}

结果

代码语言:javascript
复制
基数i为:2
基数定位的结果为:
------////-------
0 8 11 22 33 78 65 66 /////////////
基数i为:0
基数定位的结果为:
------////-------
0 8 11 22 33 78 65 66 /////////////
基数i为:3
基数定位的结果为:
------////-------
0 8 11 22 33 78 65 66 /////////////
基数i为:4
基数定位的结果为:
------////-------
0 8 11 22 33 78 65 66 /////////////
基数i为:7
基数定位的结果为:
------////-------
0 8 11 22 33 66 65 78 /////////////
基数i为:6
基数定位的结果为:
------////-------
0 8 11 22 33 65 66 78 /////////////
0 8 11 22 33 65 66 78 

快速排序设计到了递归,有点不好理解,相关东西可以网上多查看一下

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.07.30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 相关code
    • 结果
      • 结果
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档