数据结构与算法---冒泡排序和快速排序练习

测试代码

public static void main(String[] args) {
        Integer[] s={10,12,9,7,23,15,99,31,2};
//        Integer[] s={23, 15, 99, 31};
        List list= Arrays.asList(s);
        List<Integer> targetList = new ArrayList<>();
        targetList.addAll(list);
        // 冒泡排序(从小到大排序)
//        first(targetList);

        // 快速排序
        second(targetList);
        System.out.println(targetList);


    }

冒泡排序

/**
     * 冒泡排序(从小到大排序)
     * @param targetList
     */
    private static void first(List<Integer> targetList) {
        // 起始比较的索引
        for (int i = 0; i < targetList.size(); i++){
            // 交换次数
            Integer changeTime = 0;
            // 需要比较的次数
            for(int j = 0;j < targetList.size()-i-1;j++){
                // 比较的两位元素
                Integer firstValue = targetList.get(j);
                Integer secondValue = targetList.get(j+1);
                // 比较的索引
                Integer firstIndex = j;
                Integer secondIndex = j+1;
                //如果第一个值比第二个大就交换位置
                if(firstValue > secondValue) {
                    // 不借助额外空间进行值交换
                    firstValue = firstValue ^ secondValue;
                    secondValue = firstValue ^ secondValue;
                    firstValue = firstValue ^ secondValue;
                    targetList.set(firstIndex,firstValue);
                    targetList.set(secondIndex,secondValue);
                    // 交换次数加一
                    changeTime++;
                }
            }
            // 如果一轮下来没有进行过交换,说明后面的已经排序好了
            // 不需要再进行循环了
            if(changeTime == 0){
                break;
            }
        }
    }

快速排序

private static void second(List<Integer> targetList) {
        // 如果序列的长度大于1则进行排序
        if(targetList.size() > 1) {
            // 目标索引,目标值,最小索引,最高索引
            Integer targetIndex = 0;
            Integer targetValue = targetList.get(0);
            Integer lowIndex = 0+1;
            Integer highIndex = targetList.size()-1;
            // 循环排序
            while (true) {
                // 取最高索引和最低索引的值
                Integer lowValue = targetList.get(lowIndex);
                Integer highValue = targetList.get(highIndex);
                // 先从高找到比目标值小的,并且最高索引要大于最小索引
                while(highValue > targetValue && highIndex > lowIndex){
                    highIndex--;
                    highValue = targetList.get(highIndex);
                }
                // 判断如果是因为找到值而退出上面的循环,就要将值替换到目标索引
                if(highValue <= targetValue){
                    targetList.set(targetIndex, highValue);
                    targetIndex = highIndex;
                }
                // 如果是因为最高索引小于最小索引而退出循环,说明找到了目标值的索引位置
                // 将目标值存入并结束循环
                if (highIndex <= lowIndex) {
                    targetList.set(targetIndex, targetValue);
                    break;
                }
                // 最高索引--
                highIndex--;

                // 再从最小索引开始找比目标值大的
                while(lowValue < targetValue && highIndex > lowIndex){
                    lowIndex++;
                    lowValue = targetList.get(lowIndex);
                }
                // 同上,因为找到值而退出,则将值赋给目标索引
                if(lowValue >= targetValue){
                    targetList.set(targetIndex, lowValue);
                    targetIndex = lowIndex;
                }
                // 同最高索引
                if (highIndex <= lowIndex) {
                    targetList.set(targetIndex, targetValue);
                    break;
                }
                // 最小索引++
                lowIndex++;

            }
            // 找到目标值所在索引后,该索引就将序列分成了两部分,左边的小于目标值,右边的大于目标值
            List<Integer> smallList = targetList.subList(0, targetIndex);
            List<Integer> bigList = targetList.subList(targetIndex+1, targetList.size());
            // 再将两个序列再次进行排序
            second(smallList);
            second(bigList);
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值