JJay
JJay

Reputation: 21

Java program skips over for loop

The point of this program is to break down a large array into four separate arrays using recursion until each array contains less than or equal to 1000 integers. When the happens, the arrays will be sorted and then added to an ArrayList.

My problem is that after declaring the four sub-arrays, my program entirely skips over the set of for loops and goes right to the recursive call, which results in my arrays being filled with nothing but zeros. I have no idea why this happens, so any insight would be greatly appreciated.

 public static void quadSort(int[] array, int startIndex, int endIndex) {
            // Insertion sort
            if(endIndex - startIndex <= 1000) {
                for(int i = 0; i <= endIndex; i++) {
                    int x = array[i];
                    int j = i;
                    while(j > 0 && array[j - 1] > x) {
                        array[j] = array[j - 1];
                        j = j - 1;
                    }
                    array[j] = x;
                }

                for(int i = 0; i < array.length; i++) {
                    list.add(array[i]);
                }
            }

            else {

                // Split array into four separate arrays
                int[] a = new int[(endIndex + 1) / 4];
                int[] b = new int[(endIndex + 1) / 4];
                int[] c = new int[(endIndex + 1) / 4];
                int[] d = new int[(endIndex + 1) / 4];

                // Fill separate arrays with initial array's values
                for(int i = 0; i < (endIndex + 1) * (1/4); i++) {

                    for(int k = startIndex; k < (endIndex + 1) * (1/4); k++) {
                        a[i] = array[k];
                    }

                    for(int l = (endIndex + 1) * (1/4); l < (endIndex + 1) * (1/2); l++) {
                        b[i] = array[l];
                    }

                    for(int m = (endIndex + 1) * (1/2); m < (endIndex + 1) * (3/4); m++) {
                        c[i] = array[m];
                    }

                    for(int n = (endIndex + 1) * (1/4); n < endIndex + 1; n++) {
                        d[i] = array[n];
                    }
                }

                quadSort(a,0,a.length - 1);
                quadSort(b,0,b.length - 1);
                quadSort(c,0,c.length - 1);
                quadSort(d,0,d.length - 1);
            }
        }

Upvotes: 1

Views: 1265

Answers (3)

Pokechu22
Pokechu22

Reputation: 5046

This is a case of integer arithmetic.

If you run this:

 System.out.println(1/4);

You get 0.

On the other hand, this:

System.out.println(1.0f/4.0f);

gives 0.25.


Using that, you can change your loop to be like this:

for(int i = 0; i < (endIndex + 1) * (1.0f/4.0f); i++)

Alternatively, you can just skip the division and use a precalculated value instead:

for(int i = 0; i < (endIndex + 1) * .25f; i++)

Upvotes: 1

Spaceburger
Spaceburger

Reputation: 53

for(int i = 0; i < (endIndex + 1) * (1/4); i++) 

will iterate exactly 0 times, due to the integer division.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726529

The problem is that 1/4 evaluates to zero (it is an integer division, so the fraction is discarded). You can fix this by multiplying both sides of inequity by 4, like this:

for(int i = 0 ; 4*i < (endIndex + 1) ; i++)
    ...

Now the condition is logically equivalent*, but it is evaluated without using division.

* Assuming there's no overflow

Upvotes: 3

Related Questions