java数组排序练习

冒泡排序

public class BubbleSort {
    public static void main(String[] args) {
        int[] array = {0, 9, 5, 3, 4, 8, 2, 1, 7, 6};

        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - i - 1; j++) {// array.length - i - 1 提高效率
                if (array[j] > array[j + 1]) {

                    //交换位置方法1:
//                    int temp = array[j];
//                    array[j] = array[j + 1];
//                    array[j + 1] = temp;

                    //交换位置方法2:
                    array[j] = array[j] ^ array[j + 1];
                    array[j + 1] = array[j] ^ array[j + 1];
                    array[j] = array[j] ^ array[j + 1];

                }
            }
        }
        System.out.println(Arrays.toString(array));
    }
}

插入排序

public class InsertSort {
    public static void main(String[] args) {
        int[] array = {0, 9, 5, 3, 4, 8, 2, 1, 7, 6};

        for (int i = 0; i < array.length; i++) {
            for (int j = i; j > 0; j--) {
                if (array[j] < array[j - 1]) {
                    array[j] = array[j] ^ array[j - 1];
                    array[j - 1] = array[j] ^ array[j - 1];
                    array[j] = array[j] ^ array[j - 1];
                }
            }
        }

        System.out.println(Arrays.toString(array));

    }
}

快速排序

public class QuickSort {
    public static void main(String[] args) {
        int[] array = {0, 9, 5, 3, 4, 8, 2, 1, 7, 6};

        quickSort(array, 0, array.length - 1);

        System.out.println(Arrays.toString(array));
    }

    private static void quickSort(int[] array, int left, int right) {
        if (left > right) return;
        int i = left, j = right, basic = array[i];

        while (i < j) {
            while (i < j && array[j] > basic) j--;
            if (i < j) {
                array[i] = array[j];
                i++;
            }
            while (i < j && array[i] < basic) i++;
            if (i < j) {
                array[j] = array[i];
                j--;
            }
        }

        array[i] = basic;
        quickSort(array, left, i - 1);
        quickSort(array, i + 1, right);
    }


}

选择排序

public class SelectSort {
    public static void main(String[] args) {
        int[] array = {0, 9, 5, 3, 4, 8, 2, 1, 7, 6};

        for (int i = 0; i < array.length; i++) {
            int index = i;
            for (int j = i; j < array.length; j++) {
                if (array[j] < array[index])
                    index = j;
            }
            int temp = array[i];
            array[i] = array[index];
            array[index] = temp;
        }

        System.out.println(Arrays.toString(array));

    }
}

二分查找法

public class DichotomySelect {
    public static void main(String[] args) {
        int[] array = {1, 3, 6, 7, 5, 4, 2, 9, 8};
        Arrays.sort(array);//二分查找前提数组有序且是升序
        System.out.println(dichotomySelect(array, 1));
    }

    //实现方法体
    static int dichotomySelect(int[] array, int key) {
        int start = 0;
        int end = array.length - 1;
        int counter = (end + start) / 2;

        while (start <= end) {
            if (array[counter] == key) return counter;
            else if (array[counter] > key) {
                end = counter - 1;
            } else {
                start = counter + 1;
            }
            counter = (end + start) / 2;
        }
        return -1;
    }

}

31选7(集合实现)

public class HashSetLottery {
    public static void main(String[] args) {
        HashSet<Integer> hashSet = new HashSet<>();
        Random random = new Random();
        while (hashSet.size() < 7) {//生成不重复的31位数字
            int i = random.nextInt(31) + 1;
            hashSet.add(i);
        }

        //获取存储用户号码
        HashSet<Integer> hashSet01 = new HashSet<>();
        Scanner scanner = new Scanner(System.in);
        int temp = 1;
        while (temp < 8) {
            System.out.println("请输入第" + temp++ + "号码");
            int i = scanner.nextInt();
            if (i < 0 || i >31){
                System.out.println("------------数值输入不合理-----------");
                temp--;
                continue;
            }

            hashSet01.add(i);

            if (temp-1 - hashSet01.size() != 0) {
                System.out.println("------------存在重复号码-----------");
                temp--;

            }
        }

        hashSet01.addAll(hashSet);//将中彩票的号码添加如,中奖号码集合
        int i = hashSet01.size() - hashSet.size();
        switch (i) {
            case 0:
                System.out.println("喜中一等奖,2000000元");
                System.out.println("本期开奖号码:" + hashSet);
                break;
            case 1:
                System.out.println("喜中二等奖,200000元");
                System.out.println("本期开奖号码:" + hashSet);
                break;
            case 2:
                System.out.println("喜中三等奖,20000元");
                System.out.println("本期开奖号码:" + hashSet);
                break;
            case 3:
                System.out.println("喜中四等奖,2000元");
                System.out.println("本期开奖号码:" + hashSet);
                break;
            case 4:
                System.out.println("喜中五等奖,200元");
                System.out.println("本期开奖号码:" + hashSet);
                break;
            default:
                System.out.println("您为建设祖国,出了一份力");
                System.out.println("本期开奖号码:" + hashSet);
        }

    }
}

31选7(数组实现)

public class Lottery31and7 {
    public static void main(String[] args) {
        int[] winner = new int[7];
        int index = 0;
        while (index < winner.length) {
            int random = (int) (Math.random() * 31 + 1);//随机数生成
            boolean repetition = isRepetition(winner, random);
            if (repetition) {
                winner[index++] = random;
            }
        }


        int correct = isCorrect(winner);
        switch (correct) {
            case 7:
                System.out.println("喜中一等奖,2000000元");
                System.out.println("本期开奖号码:" + Arrays.toString(winner));
                break;
            case 6:
                System.out.println("喜中二等奖,200000元");
                System.out.println("本期开奖号码:" + Arrays.toString(winner));
                break;
            case 5:
                System.out.println("喜中三等奖,20000元");
                System.out.println("本期开奖号码:" + Arrays.toString(winner));
                break;
            case 4:
                System.out.println("喜中四等奖,2000元");
                System.out.println("本期开奖号码:" + Arrays.toString(winner));
                break;
            case 3:
                System.out.println("喜中五等奖,200元");
                System.out.println("本期开奖号码:" + Arrays.toString(winner));
                break;
            default:
                System.out.println("您为建设祖国,出了一份力");
                System.out.println("本期开奖号码:" + Arrays.toString(winner));
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕清海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值