You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java整数数组生成与排序测试方案咨询(教授协助版)

基于整数数组元素长度创建新数组并测试排序的解决方案

看起来你已经通过教授的协助拿到了核心实现思路,我来帮你拆解这段代码的逻辑,让整个流程更清晰:

核心逻辑概述

这段代码完美匹配你的需求:先定义一个包含多个整数的数组,用数组里每个元素的值作为长度生成新的随机数组,还额外附带了选择排序的测试以及排序比较次数的统计功能。

完整代码与详解

我先把截断的代码补全并格式化,同时附上配套的工具类实现(假设SortMethods是你需要的工具类):

public class TestSortMethods { 
    public static void main(String[] args) { 
        // 定义初始数组,每个元素代表后续要创建的新数组的长度
        int[] array0 = new int[] {10, 50, 100, 250, 500, 750, 1000}; 
        
        // 遍历初始数组,为每个长度创建对应新数组
        for (int i = 0; i < array0.length; i++) { 
            // 生成指定长度的随机整数数组,第二个参数100.0表示随机数的上限
            int[] array1 = SortMethods.randomIntArray(array0[i], 100.0); 
            // 复制原数组,用于保留未排序的版本(方便后续对比)
            int[] array2 = SortMethods.copyArray(array1); 
            
            // 打印未排序的原数组
            System.out.println("Original array (length: " + array0[i] + "):");
            SortMethods.printArray(array1); 
            System.out.println("\nSorting array...\n"); 
            
            // 执行选择排序
            SortMethods.selectionSort(array1); 
            // 获取并打印选择排序的比较次数
            int comparisonCount = SortMethods.selectionSortComparisons;
            System.out.println("Sorted array:");
            SortMethods.printArray(array1);
            System.out.println("Selection sort comparison count: " + comparisonCount + "\n");
        }
    }
}

// 配套的SortMethods工具类实现
class SortMethods {
    public static int selectionSortComparisons; // 用于统计选择排序的比较次数
    
    // 生成指定长度的随机整数数组,max为随机数的最大值
    public static int[] randomIntArray(int length, double max) {
        int[] arr = new int[length];
        for (int i = 0; i < length; i++) {
            arr[i] = (int) (Math.random() * max);
        }
        return arr;
    }
    
    // 复制数组的工具方法
    public static int[] copyArray(int[] original) {
        int[] copy = new int[original.length];
        System.arraycopy(original, 0, copy, 0, original.length);
        return copy;
    }
    
    // 打印数组的工具方法
    public static void printArray(int[] arr) {
        for (int num : arr) {
            System.out.print(num + " ");
        }
        System.out.println();
    }
    
    // 选择排序实现,同时统计比较次数
    public static void selectionSort(int[] arr) {
        selectionSortComparisons = 0; // 每次排序前重置计数
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                selectionSortComparisons++; // 每进行一次比较,计数加1
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            // 交换找到的最小值与当前位置元素
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
    }
}

关键部分说明

  • 初始数组定义:array0里的每个整数就是目标新数组的长度,比如第一个元素10,就会生成一个长度为10的随机数组
  • 循环创建新数组:通过for循环遍历array0,每次调用randomIntArray生成对应长度的随机数组
  • 数组复制:copyArray方法保留原数组的副本,方便你对比排序前后的数组差异
  • 排序与统计:selectionSort方法执行排序,同时用selectionSortComparisons统计排序过程中的比较次数,帮助你分析排序的性能

内容的提问来源于stack exchange,提问作者KalmaH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:18:04