快速排序(java 图示+代码)

对无序数据进行排序
时间复杂度 n*logn
100 50 200 30 10 40 43 300 150 130 110 220 56 32 11 111
以100做基准,比100小的放左边,比100大的放右边
第一轮:50,30,10,40,43,56,32,11,#100#,200,300,150,130,110,220,111
第二轮:30,10,40,43,32,11,#50#,56,| #100# |,150,130,110,111,#200#,300,220
第三轮:10,11,#30#,40,43,32,| #50# |,56,| #100# |,130,110,111,#150#,| #200# |,220,#300#

第一轮

0
按照这样的规则一次排序,直到两个基准重合,第一轮操作会将100排好位置
0

第二轮

0
第二轮结束,50、110的位置已经找到
0
重复上述步骤,最终得到有序结果。

代码示例

public static void main(String[] args) {
    int[] arr={100,50,200,30,10,40,43,300,150,130,110,220,56,32,11,111};
    sort(arr,0,arr.length-1);
    System.out.println(Arrays.toString(arr));
}

 public static void sort(int[] arr,int flag1,int flag2){
    int left=flag1;
    int right=flag2;
     while(flag1!=flag2){
         if(arr[flag1]>arr[flag1+1]){
             int temp=arr[flag1];
             arr[flag1]=arr[flag1+1];
             arr[flag1+1]=temp;
             flag1++;
         }else{
             int temp=arr[flag1+1];
             arr[flag1+1]=arr[flag2];
             arr[flag2]=temp;
             flag2--;
         }
     }
     //当左侧数据超过两个,再次排序
     if(flag1-1-left>0){
         sort(arr,left,flag1-1);
     }
     //当右侧数据超过两个,再次排序
     if(right-(flag1+1)>0){
         sort(arr,flag1+1,right);
     }
 }

小插曲

public class Node {
    public int value;
}
public static void main(String[] args) {
    int a=10;
    Node b=new Node();
    b.value=20;
    Node c=new Node();
    c.value=30;
    change(a,b,c);
    
    /**输出什么???**/
    System.out.println(a);  //10
    System.out.println(b.value); //20
    System.out.println(c.value); //55
}

public static void change(int p1,Node p2,Node p3){
    p1=33;
    p2=new Node();
    p2.value=44;
    p3.value=55;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值