zoukankan      html  css  js  c++  java
  • 4.12快速分类

    package test;
    
    import java.util.Scanner;
    
    public class T12Partition {
        public static int[] a;
        public int partition(int m,int p){
            int v=a[m];
            System.out.println("基准元素为:"+v);
            int i=m+1;
            while(i<=p){
                while(i<=p&&a[i]<v){
                    i++;
                }
                while(i<=p&&a[p]>v){
                    p--;
                }
                if(i<p){
                    int temp=a[i];
                    a[i]=a[p];
                    a[p]=temp;
                }else{
                    break;
                }
            }
            System.out.println(a[p]+"即将回到"+m+"位置");
            System.out.println(v+"即将回到"+p+"位置");
            a[m]=a[p];
            a[p]=v;
            return p;
        }
        public void quicksort(int p,int q){
            if(p<q){
                int j=partition(p,q);
                System.out.println("左边:"+p+" "+(j-1));
                quicksort(p,j-1);
                for(int i=0;i<9;i++){
                    System.out.print(a[i]+" ");
                }
                System.out.println();
                System.out.println("右边:"+(j+1)+" "+q);
                quicksort(j+1,q);
                for(int i=0;i<9;i++){
                    System.out.print(a[i]+" ");
                }
                System.out.println();
            }
        }
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.print("输入数组长度n:");
            int n=sc.nextInt(); 
            a=new int[n];
            System.out.println("输入数组元素:");
            for(int i=0;i<n;i++){
                a[i]=sc.nextInt();
            }
            new T12Partition().quicksort(0,n-1);
        }
    }

    输入数组长度n:9

    输入数组元素:
    65 70 75 80 85 60 55 50 45
    ]基准元素为:65
    60即将回到0位置
    65即将回到4位置
    左边:0 3
    基准元素为:60
    55即将回到0位置
    60即将回到3位置
    左边:0 2
    基准元素为:55
    50即将回到0位置
    55即将回到2位置
    左边:0 1
    基准元素为:50
    45即将回到0位置
    50即将回到1位置
    左边:0 0
    45 50 55 60 65 85 80 75 70
    右边:2 1
    45 50 55 60 65 85 80 75 70
    45 50 55 60 65 85 80 75 70
    右边:3 2
    45 50 55 60 65 85 80 75 70
    45 50 55 60 65 85 80 75 70
    右边:4 3
    45 50 55 60 65 85 80 75 70
    45 50 55 60 65 85 80 75 70
    右边:5 8
    基准元素为:85
    70即将回到5位置
    85即将回到8位置
    左边:5 7
    基准元素为:70
    70即将回到5位置
    70即将回到5位置
    左边:5 4
    45 50 55 60 65 70 80 75 85
    右边:6 7
    基准元素为:80
    75即将回到6位置
    80即将回到7位置
    左边:6 6
    45 50 55 60 65 70 75 80 85
    右边:8 7
    45 50 55 60 65 70 75 80 85
    45 50 55 60 65 70 75 80 85
    45 50 55 60 65 70 75 80 85
    右边:9 8
    45 50 55 60 65 70 75 80 85
    45 50 55 60 65 70 75 80 85

    在平凡中坚持前行,总有一天,会遇见优秀的自己
  • 相关阅读:
    android学习十四(android的接收短信)
    C/C++知识要点4——printf函数以及cout的计算顺序
    HDU 5355 Cake(2015多校第六场,搜索 + 剪枝)
    微信错误提示code= -4/微信发送被拒绝
    struts2的validate在使用过程中的一个问题
    28.字符串的排列
    Redis入门经典——The Little Redis Book (翻译)
    POJ 3155 Hard Life(最大密度子图)
    BZOJ 1798 AHOI2009 Seq 维护序列 线段树
    RT-Thread开篇
  • 原文地址:https://www.cnblogs.com/mao-19/p/5419338.html
Copyright © 2011-2022 走看看