一种排序算法

假定控制台输入一段数字序列,对其进行排序,首先把这个序列的每个数字装进一个被定义全局变量的数组SI,定义一个List<Integer>型的list,再把数组SI里的每个值通过for循环添加到list。定义一个boolean型函数isOrdinal(int[] sz),SI作为参数该函数用来判断序列是否有序,定义一个函数Digui(int[] sz, List<integer> list),SI和list作为形参,该函数用来选中序列中的一个数作为枢轴,把小于枢轴的数放到枢轴的左边,把大于枢轴的数放到枢轴的右边,等于枢轴的数放在它们之间。通过while循环判断每次使用Digui()后,序列是否有序,所以Difui()放在while内部。

选取枢轴的方法采用随机数,每次使用Digui()都会产生一个0~数组长度减一的数字,数字即为原序列数组SI的下标,下标对应的值即为枢轴,这种方法虽然最后也能排序成功,但是选取随机数的过程很可能产生重复,影响效率,所以经过改进,每次从0~list.size()-1的范围内选取随机数,得到枢轴,然后将list中对应枢轴的元素删除,进而下次选取枢轴时,就不会产生重复了。


代码块

package sort;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class QKSort {
    public static String[] shuzuStr;
    public static int[] SI;
    public static int count = 0;
    //这个函数判断数组是否从小到大
    public static boolean isOrdinal(int[] sz){
        int countT = 0;
        if(sz.length >= 2)
            for(int m = 1; m < sz.length; m++)
                if(sz[m-1] <= sz[m])
                    countT++;
        if(countT == sz.length-1)
            return true;
        else
            return false;
    }
    //一个函数,用来选取枢轴,并把数字放到其左右两边,左边小,又变大
    public static void Digui(int[] sz, List<Integer> list){
        count++;
        Random rand = new Random();
        int randomNum = rand.nextInt(list.size()-1);

        int oneNum = list.get(randomNum);
        if(list.size() > 0)
            list.remove(randomNum);
        System.out.print("第"+count+"次选取枢轴:"+oneNum+","+"排序后是:");
        int countX = 0;
        int countE = 0;
        List<Integer> listX = new ArrayList<Integer>();
        List<Integer> listD = new ArrayList<Integer>();
        for(int m = 0; m < sz.length; m++){
            if(sz[m] < oneNum){
                countX++;
                listX.add(sz[m]);
            }else if(sz[m] > oneNum)
                listD.add(sz[m]);
            else if(sz[m] == oneNum)
                countE++;
        }
        for(int m = 0; m < countX; m++)
            sz[m] = listX.get(m);
        for(int m = countX+countE; m < sz.length; m++)
            sz[m] = listD.get(m-(countX+countE));
        for(int m = countX; m < countX+countE; m++)
            sz[m] = oneNum;
        //输出
        for(int m = 0; m < sz.length; m++)
            System.out.print(sz[m]+",");
        System.out.println();
    }

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();

        Scanner Scanner = new Scanner(System.in);
        System.out.println("输入多个正整数(以英文逗号分隔):");
        String Input = Scanner.next();
        shuzuStr = Input.split(",");

        SI = new int[shuzuStr.length];
        for(int m = 0; m < shuzuStr.length; m++)
            SI[m] = Integer.parseInt(shuzuStr[m]);
        for(int m = 0; m < SI.length; m++)
            list.add(SI[m]);
        if(shuzuStr.length == 1)
            System.out.println(SI[0]);
        else if(isOrdinal(SI))
            for(int m = 0; m < SI.length; m++)
                System.out.print(SI[m]+",");
        else
            while(!isOrdinal(SI))
                Digui(SI, list);

        Scanner.close();
    }
}

运行结果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值