排序

package com.foundation.sort;
/***
 * 注:为什么今天会想到重新来写一下排序 还得研究执行的过程
 * (今天下午华为的技术员进行复试,喊哥给他手写排序,
 *     写了就算了,还让我给他说名执行过程,
 * 说明过程写了就算,还得让你给他说出来,
 * 在第几次的循环结果出来,我只能说,真的很挑剔)
 * */
public class SortTest {
    public static void main(String[] args) {
        int[] arrays={56,67,2,45,32,1};
        //chooseSort(arrays);
        bubbleSort(arrays);
        print(arrays);
    }
    /***选择排序*/
    public static void chooseSort(int[] arrays) {
        for (int i = 0; i < arrays.length; i++) {
            //注释中的是用来观察选择排序,每一次排序结果的顺序    
            //System.out.print("第"+i+"次的排序结果:");
                //print(arrays);
                //System.out.println();
            for (int j = i+1; j < arrays.length ; j++) {
                if (arrays[i] > arrays[j]) {
                    int temp = arrays[i];
                    arrays[i] = arrays[j];
                    arrays[j] = temp;
                }
            }
        }
        /*
         *  第0次的排序结果:56,67,2,45,32,1,
            第1次的排序结果:1,67,56,45,32,2,
            第2次的排序结果:1,2,67,56,45,32,
            第3次的排序结果:1,2,32,67,56,45,
            第4次的排序结果:1,2,32,45,67,56,
            第5次的排序结果:1,2,32,45,56,67,
            result:=1,2,32,45,56,67,
            
        */
        
    }
    /**冒泡排序*/
    public static void bubbleSort(int[] arrays){
        for (int i = 0; i < arrays.length; i++) {
            //注释中的是用来观察选择排序,每一次排序结果的顺序    
            System.out.print("第"+i+"次的排序结果:");
                print(arrays);
                System.out.println();
            for (int j = 0; j < arrays.length-1; j++) {
                if(arrays[j]>arrays[j+1]){
                    int temp=arrays[j];
                    arrays[j]=arrays[j+1];
                    arrays[j+1]=temp;
                }
            }
        }
        /*
         * 第0次的排序结果:56,67,2,45,32,1,
            第1次的排序结果:56,2,45,32,1,67,
            第2次的排序结果:2,45,32,1,56,67,
            第3次的排序结果:2,32,1,45,56,67,
            第4次的排序结果:2,1,32,45,56,67,
            第5次的排序结果:1,2,32,45,56,67,
            result:=1,2,32,45,56,67,
         *
         * */
    }
    
    public static void print(int[] arrays){
        for (int i : arrays) {
            System.out.print(i+",");
        }
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值