Java业务逻辑-1(冒泡排序)

需求

 定义一个方法,在键盘上接收10个整数,然后对10个整数进行按照从小到大的顺序放入一个数组中返回

自研算法-源代码 

直接上源代码+注释。(补充:最后有附录,用于理解代码执行流程)

package com.itheima.improve;

import java.util.Random;
import java.util.Scanner;

/**
 * 1、定义一个方法,在键盘上接收10个整数,然后对10个整数进行按照从小到大的顺序放入一个数组中返回
 */
public class d1 {
    static Scanner sc;
    static Random r;

    static {
        sc = new Scanner(System.in);
        r = new Random();
    }

    public static void main(String[] args) {
        long ctime = System.nanoTime();
        int[] arr = input();
        sort(arr);
        long etime = System.nanoTime();
        //运行程序花费多少秒
        System.out.println();
        System.out.println("本次执行花费时间为:" + (etime - ctime) / 1000000000.0);

    }

    public static void sort(int[] a) {
        //定义一个空数组用来接收排序后的数据
        int[] arr = new int[a.length];
        int min = a[0];
        int count0 = 0;
        int j = 0;
        //1.先找0
        for (int i = 0; i < a.length; i++) {
            if (a[i] == 0) {
                count0++;//记录0的个数
            }
        }
        //2.再找负整数
        int iwhile = 0;//用于确保查询并添加完所有负数
        int temp = -1;//用于记录最小值所在索引
        boolean flag = false;
        while (true) {
            for (int i = 0; i < a.length; i++) {
                if (a[i] < 0 && a[i] <= min) {//是否是负整数并且判断是否是最小值
                    min = a[i];//不是最小值则赋值最小值
                    temp = i;//存储最小值索引
                }
                if (i == a.length - 1) {//’上方循环一遍前不执行此处代码‘/遍历a.length次的目的是确保遍历完所有负数
                    if (temp > -1) {//判断是否已找到最小值 (>-1说明有新的最小值赋值了).44
                        arr[j] = min;//将最小值从左至右添加至数组
                        a[temp] = 0;//将最小值所在索引置的值设置为0,原数组中0的个数已确定:count0已记录
                        j++;//准备为新数组的下一个位置赋值
                        temp = -1;//重置temp
                    }
                    if (a[0] > 0 && min == a[0]) {//因为每次遍历完一次后都会给min赋初始值,若初始值>0并且min=a[0]说明数组内已无负数,跳出循环
                        flag = true;//信号标志法,可提高性能
                        break;//遍历
                    }
                    iwhile++;//记录循环次数,若全是负数则需要循环a.length次。
                    min = a[0];//重置min
                }
            }//’上方循环a.length遍前不执行此处代码‘
            //判断是否遍历完所有负数
            if (iwhile == a.length - 1 || flag) {
                break;
            }
        }
        //将0添加至数组
        for (int k = j; k < j + count0; k++) {//count0记录了原数组中的0的个数,j记录了新数组arr的下一个空位置的索引
            arr[k] = 0;
        }
        //最后找正整数
        min = a[0];//此时重新使用其对大于0的数进行倒序排列
        j = a.length - 1;//此时用来定义倒序添加的索引
        iwhile = 0;//用于确保查询并添加完所有正数
        flag = false;
        while (true) {
            for (int i = 0; i < a.length; i++) {
                if (i != a.length - 1 && a[i] == 0) {//如果该元素不是数组的最后一个元素且该元素为0,则跳过;
                    // 为什么要添加i!=a.length-1是因为如果最后一个元素是0,那么它跳过的话会导致无法执行跳出陷入死循环
                    continue;
                }
                if (a[i] > 0 && a[i] >= min) {//判断是否是正整数并且判断是否是最大值
                    min = a[i];//不是最大值则赋值最大值
                    temp = i;//存储最大值索引
                }
                if (i == a.length - 1) {//’上方循环一遍前不执行此处代码‘/遍历a.length次的目的是确保遍历完所有正数
                    if (temp > -1) {//判断是否已找到最大值
                        arr[j] = min;//将最大值从右至左添加至数组  j=a.length-1
                        a[temp] = 0;//将最大值所在索引置的值设置为0,0在数组中已属于无效的值
                        j--;//j=a.length-1  从右递减
                        temp = -1;//重置temp
                    }
                    if (a[0] == 0 && min == 0) {//因为每次遍历完一次后都会给min赋初始值,若初始值==0并且min=0说明数组内已无正数,跳出循环
                        flag = true;//信号标志法,可提高性能
                        break;//跳出for循环
                    }
                    iwhile++;//记录循环次数,若全是正数则需要循环a.length次。
                    min = a[0];//重置min
                }
            }
            if (iwhile == a.length - 1 || flag) {//判断是否遍历完所有正数
                break;//跳出while循环
            }
        }
        System.out.print("排序后的数组为:[");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + " ");
        }
        System.out.print("]");
    }

    public static int[] input() {
        System.out.println("请输入您要排序的整数的个数:");
        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < arr.length; i++) {
//            System.out.println("请输入第" + (i + 1) + "个整数:");
//            arr[i] = sc.nextInt();
            arr[i] = r.nextInt(50) - 25;
        }
        return arr;
    }
}

简便解法

冒泡排序

package com.itheima.improve;

import java.util.Random;
import java.util.Scanner;

import static com.itheima.improve.d10.input;

/**
 * 1、定义一个方法,在键盘上接收10个整数,然后对10个整数进行按照从小到大的顺序放入一个数组中返回
 */
public class d11 {
    static Scanner sc;
    static Random r;

    static {
        sc = new Scanner(System.in);
        r = new Random();
    }

    public static void main(String[] args) {
        long ctime = System.nanoTime();
        int[] arr = input();
        sort(arr);
        long etime = System.nanoTime();
        //运行程序花费多少秒
        System.out.println();
        System.out.println("本次执行花费时间为:" + (etime - ctime) / 1000000000.0);
    }

    private static void sort(int[] arr) {
        for (int j = 0; j < arr.length - 1; j++) {
            for (int i = 0; i < arr.length - 1 - j; i++) {
                if (arr[i] >= arr[i + 1]) {
                    int temp;
                    temp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = temp;
                }
            }
        }
        System.out.print("排序后:[");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + " ");
        }
        System.out.print("]");
    }
}

附录

此处以一个数组{9,-1,0,3,2,-3,0}为例,依次执行sort方法的步骤简略。

//判负
// 9 -1 0 3 2 -3 0 count0 = 2

//min = -3  temp = 5 j=0  iwhile=0
//-3 0 0 0 0 0 0  arr
// 9 -1 0 3 2 0 0 a
// j =1 temp =-1  iwhile =1 min = 9

//min = -1 temp = 1  j=1  iwhile=1
//-3 -1 0 0 0 0 0 arr
//  9 0 0 3 2 0 0 a
//j =2 temp = -1 iwhile =2 min = 9

//min =9 temp = -1  iwhile =3 min=9
//min =9 temp = -1  iwhile =4 min=9
//min =9 temp = -1  iwhile =5 min=9
//min =9 temp = -1  iwhile =6 min=9
//
//判0
//k∈[2,4)
//k=2
//-3 -1 0 0 0 0 0 arr
//k=3
//-3 -1 0 0 0 0 0 arr
//
//判正
//9 0 0 3 2 0 0       a
//max = 9 temp = 0 j=a.length  iwhile=0
//-3 -1 0 0 0 0 9 arr
//  0 0 0 3 2 0 0 a
//j = a.length-1-1 temp = -1 iwhile =1 min=0
//max = 3 temp = 3 j = a.length-1-1
//-3 -1 0 0 0 3 9 arr
//  0 0 0 0 2 0 0 a
//j = a.length-1-1-1 temp = -1 iwhile =2 min=0
//max = 2 temp = 4 j = a.length-1-1-1
//-3 -1 0 0 2 3 9 arr
//  0 0 0 0 0 0 0 a
//j = a.length-1-1-1-1  temp = -1 iwhile =3 min=0
//j = a.length-1-1-1-1  temp = -1 iwhile =4 min=0
//j = a.length-1-1-1-1  temp = -1 iwhile =5 min=0
//j = a.length-1-1-1-1  temp = -1 iwhile =6 min=0
//

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值