Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So I got this school assignment:

Write a method to take two integer arrays and sort x[] into y[] using foreach loop.

x[] is size 10 and has all elements initialized with Math.random(), and y[] is of size >10 and filled with zeroes. The method should sort the x[] array into y[] and return y[].

Since I'm only left with foreach I can't figure a way to refer to other elements in the array in order to compare them. Any ideas?

share|improve this question
    
What do you mean by ”Since I'm only left with foreach I can't figure a way to refer to other elements in the array in order to compare them.” ? –  Raul Rene Feb 20 '13 at 23:02
    
Java has a sort of foreach statement for (E elem : collection) {} but are you SURE you have to use it? –  gd1 Feb 20 '13 at 23:03
    
@RaulGogo: I was looking for a way to do something like I would with a classic for loop: 'if(x[i] < x[i+1]) //swap the elements' but I would need a counter for that, which I do not have in the foreach. –  Viktor Hronec Feb 20 '13 at 23:06
    
Math.random() returns a double and you're sorting Integers - how do you plan to convert double to int ? –  alfasin Feb 20 '13 at 23:06
    
You can cheat by using a counter you manually increment, and perform a simple insertion sort. –  gd1 Feb 20 '13 at 23:07

2 Answers 2

This is my 100% serious answer.

public static void completelyLegitSort(int[] x,int[] y){
    for(int n:x){
        System.arraycopy(x, 0, y, 0, x.length);
        java.util.Arrays.sort(y);
        break;
    }
}
share|improve this answer
    
+1 You are a genius! But be polite, you aren't allowed to change the array x! :D –  gd1 Feb 20 '13 at 23:56
1  
@gd1 You are right, I fixed it –  Zane Feb 21 '13 at 0:10
    
Why do you call java.util.Arrays.sort(y); inside the for-loop (x.length times) instead of one call right after the loop ? –  alfasin Feb 21 '13 at 19:27
    
@alfasin : the loop breaks after the first iteration –  gd1 Feb 21 '13 at 20:22
    
@gd1 now that mention it - you're right! so why do we need a loop ??? –  alfasin Feb 21 '13 at 20:56
int[] crazySort(int[] x, int[] y) {

    System.arraycopy(x, 0, y, 0, x.length);

    int i = 1; // cheating
    for (int n : y) { // <- this is your foreach, you will never use the n variable :D
        if (i == x.length) break; // cheating again
        for (int j = i-1; j >= 0; j--) { // finally doing something reasonable
            if (y[j+1] < y[j]) {
                int temp = y[j+1];
                y[j+1] = y[j];
                y[j] = temp;
            }
        }
        i++;
    }

    return y;

}

Hope the teacher finds it funny, at least as funny as the exercise statement. This solution uses the java "foreach" loop, and the exercise statement doesn't tell you to use only it.

share|improve this answer
    
int[] x = new int[]{15,10,7,1,7,6,27,8,2,17}; int[] y = new int[10]; int index; int count; for (int i1 : x) { index = 0; count = 0; for (int i2 : x) { if(i2 < i1) { index++; } else if (i2 == i1) { count++; } } while (count != 0) { count--; y[index] = i1; index++; } } Sorry for the formatting. A friend of mine finally figured it out. –  Viktor Hronec Feb 20 '13 at 23:21
    
The solution of your friend seems 'hacky' enough to get a good mark. :P Change the name of the variables, put a couple of useless counters and you're done. –  gd1 Feb 20 '13 at 23:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.