java小白探索,螺栓螺母匹配,冒泡实现和快速排序实现

丁丁的算法作业,学大佬发博客,各位大佬教教我算法入门求求了。昨天大佬指出冒泡哪里有错误已经修改了,在源代码上加上了键入。

package com.company;

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

public class Main {
    public static void sort1(int[] a3, int[] a4) {     //sort1 第一种思路采用冒泡排序的思路进行匹配
        for (int i = 0; i < a4.length; i++) {               //从螺丝中选择一个螺丝
            int j = i;
            for (j = i; j < a3.length; j++) {             //在螺母总寻找匹配螺丝
                if (a3[j] == a4[i]) {                 //如果找到匹配螺母则将螺母交换到对应位置
                    int temp = a3[j];
                    a3[j] = a3[i];
                    a3[i] = temp;
                }
            }
        }
    }


    public static void sort(int[] a1, int[] a2, int start1, int start2, int end1, int end2) {    //sort基于快速排序的排序
        if (start1 < end1 && start2 < end2) {        //递归终止条件
            int stard1 = a1[start1];//挑选第一个螺钉为基准数
            for (int i = 0; i < a2.length; i++) {   //  在寻找在螺母中对应的基准数并交换到基准螺钉的对应位置
                if (a2[i] == stard1) {
                    int temp = a2[i];
                    a2[i] = a2[start2];
                    a2[start2] = temp;

                }
            }
            int stard2 = a2[start2];//寻找螺母中的基准数
            int high1 = start1, low1 = end1, high2 = start2, low2 = end2;//双边循环,high指向数组左边,low指向数组右边
            while (high1 < low1)/*当左右指针重合时停止循环*/ {
                while (high1 < low1 && a1[low1] > stard2)/*将小于基准数的螺钉移到基准数的左边*/ {
                    low1--;//如果大于基准数则不需要移动low向左移
                }
                a1[high1] = a1[low1];

                while (high1 < low1 && a1[high1] < stard2)/*将大于基准数的螺钉移到右边*/ {
                    high1++;//如果小于基准数则不需要移动则high右移
                }
                a1[low1] = a1[high1];//螺钉的交换


            }
            while (high2 < low2) {//
                while (high2 < low2 && a2[low2] > stard1)/*将小于基准数的移到螺母的左边*/ {
                    low2--;
                }
                a2[high2] = a2[low2];


                while (high2 < low2 && a2[high2] < stard1) {
                    high2++;
                }
                a2[low2] = a2[high2];

            }
            a1[low1] = stard1;//将基准数放回数组,此时low和high重合指向同一位置
            a2[low2] = stard2;

            sort(a1, a2, start1, start2, high1, high2);//递归左边
            sort(a1, a2, low1 + 1, low2 + 1, end1, end2);//递归右边

        }
    }

    public static int[] getRandomArrayByIndex(int num, int scope) {//生成随机数方法
        //1.获取scope范围内的所有数值,并存到数组中
        int[] randomArray = new int[scope];
        for (int i = 0; i < randomArray.length; i++) {
            randomArray[i] = i;
        }

        //2.从数组random中取数据,取过后的数改为-1
        int[] numArray = new int[num];//存储num个随机数
        int i = 0;
        while (i < numArray.length) {
            int index = (int) (Math.random() * scope);
            if (randomArray[index] != -1) {//已出现的数不放入numArray中,因为题目要求没有同样大小的螺母或螺丝
                numArray[i] = randomArray[index];
                randomArray[index] = -1;
                i++;
            }
        }

        return numArray;//numArrays作为方法返回值
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入数据量");
        int num = sc.nextInt();
        System.out.println("请输入数据范围");
        int scope = sc.nextInt();
        int a1[] = getRandomArrayByIndex(num, scope);//a1,a2都为值是1-9的随机数组
        int a2[] = getRandomArrayByIndex(num, scope);
        int a3[]= a1.clone();
        int a4[]=a2.clone();

        System.out.println("a1排序前" + Arrays.toString(a1));
        System.out.println("a2排序前" + Arrays.toString(a2));
        System.out.println("a3排序前" + Arrays.toString(a3));
        System.out.println("a4排序前" + Arrays.toString(a4));
        long start, end;
        start = System.currentTimeMillis();
        sort(a1, a2, 0, 0, a1.length - 1, a2.length - 1);
        end = System.currentTimeMillis();
        System.out.println(a1.length + "的数据量sort排序时间:" + (end - start) + "(ms)");
        start = System.currentTimeMillis();
        sort1(a3, a4);
        end = System.currentTimeMillis();
        System.out.println(a3.length + "的数据量sort1排序时间:" + (end - start) + "(ms)");
       System.out.println("a1排序后" + Arrays.toString(a1));
        System.out.println("a2排序后" + Arrays.toString(a2));
        System.out.println("a3排序后" + Arrays.toString(a3));
        System.out.println("a4排序后" + Arrays.toString(a4));

    }
}
  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值