Home > Enterprise >  How to reduce the number of loops if there is no change?
How to reduce the number of loops if there is no change?

Time:08-15

This code is radix sort in Java.

Now I can sort. But I want to reduce its functionality if there is no change in the array, let it stop the loop and show the value.

Where do I have to fix it? Please guide me, thanks in advance.

public class RadixSort {

    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

CodePudding user response:

I'm going to resist typing out some code for you and instead go over the concepts since this looks like homework.

If I'm understanding you correctly, your problem boils down to: "I want to check if two arrays are equivalent and if they are, break out of a loop". Lets tackle the latter part first. In Java, you can use the keyword"

break;

to break out of a loop.

A guide for checking if two arrays are equivalent in java can be found here: https://www.geeksforgeeks.org/compare-two-arrays-java/

CodePudding user response:

Sorry if this doesnt answer your question. Im just gonna suggest a faster way to find the digits of each element. Take the log base 10 of the element and add 1.
Like this : int digits = (int) Math.log10(i) 1;

  •  Tags:  
  • java
  • Related