【问题标题】:How to reduce the number of loops if there is no change?如果没有变化,如何减少循环次数?
【发布时间】:2022-08-15 13:31:46
【问题描述】:

这段代码是 Java 中的 RedixSort

现在我可以排序了。但是如果没有变化,我想减少它的功能大批,让它停止环形显示价值.

我必须在哪里修复它?请指导我,提前谢谢。

公共类基数排序{

void countingSort(int inputArray[], int size, int place) {
    //find largest element in input array at \'place\'(unit,ten\'s etc)
    int k = ((inputArray[0] / place) % 10);
    for (int i = 1; i < size; i++) {
        if (k < ((inputArray[i] / place) % 10)) {
            k = ((inputArray[i] / place) % 10);
        }
    }
    //initialize the count array of size (k+1) with all elements as 0.
    int count[] = new int[k + 1];
    for (int i = 0; i <= k; i++) {
        count[i] = 0;
    }
    //Count the occurrence of each element of input array based on place value
    //store the count at place value in count array.
    for (int i = 0; i < size; i++) {
        count[((inputArray[i] / place) % 10)]++;
    }
    //find cumulative(increased) sum in count array
    for (int i = 1; i < (k + 1); i++) {
        count[i] += count[i - 1];
    }
    //Store the elements from input array to output array using count array.
    int outputArray[] = new int[size];
    for (int j = (size - 1); j >= 0; j--) {
        outputArray[count[((inputArray[j] / place) % 10)] - 1] = inputArray[j];
        count[(inputArray[j] / place) % 10]--;//decrease count by one.
    }
    for (int i = 0; i < size; i++) {
        inputArray[i] = outputArray[i];//copying output array to input array.
    }
    System.out.println(Arrays.toString(inputArray));
}

void radixSort(int inputArray[], int size) {
    //find max element of inputArray
    int max = inputArray[0];
    for (int i = 1; i < size; i++) {
        if (max < inputArray[i]) {
            max = inputArray[i];
        }
    }
    //find number of digits in max element
    int d = 0;
    while (max > 0) {
        d++;
        max /= 10;
    }
    //Use counting cort d no of times
    int place = 1;//unit place
    for (int i = 0; i < d; i++) {
        System.out.print(\"iteration no = \"+(i+1)+\" \");
        countingSort(inputArray, size, place);
        place *= 10;//ten\'s , hundred\'s place etc
    }
}

1

    标签: java


    【解决方案1】:
     public static void main(String[] args) {
        Scanner scan = new Scanner( System.in );
        RadixSort radixSort = new RadixSort();
        int n, i;
        System.out.print("Enter number of integer elements :");
        n = scan.nextInt();
        int inputArray[] = new int[ n ];
        System.out.print("\nEnter "+ n +" integer elements :");
    
        for (i = 0; i < n; i++)
        inputArray[i] = scan.nextInt();
    
        System.out.println("\n===============Array before sorting=================");
        System.out.println(Arrays.toString(inputArray));
        System.out.println("\n===============Array after  sorting=================");
        radixSort.radixSort(inputArray, n);
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 2023-02-21
      • 1970-01-01
      • 2011-10-29
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      相关资源
      最近更新 更多