So I have this method which will take an ArrayList of Integers, I compare between 2 sub arrays and preform some swapping of numbers.
I am comparing the first elements of each sub array which is just the array halved, then swapping the elements if the bigger number isn't in the left subList. after that I want to recursively do the same thing but to the first half.
so if I type 3 5 8 2 1 7 6 4 I want to see this;
[3, 5, 8, 2, 1, 7, 6, 4]
[3, 7, 8, 4, 1, 5, 6, 2]
[8, 7, 3, 4, 1, 5, 6, 2]
[8, 7, 3, 4, 1, 5, 6, 2]
What I want to be able to do is once I have done what I need to, to the base array, I need to work on the first half of the same array recursively. However when I compile, I get an error saying that I have incompatible types. So I'm assuming the subList function isn't providing me a ArrayList.
Any pointers on gettign this to work correctly?
public static void tournament(ArrayList<Integer> players){
int mid = players.size()/2;
for(int i=0; i < mid; i++){
if(players.subList(0,mid).get(i) < players.subList(mid,players.size()).get(i)){
int temp = players.subList(0,mid).get(i);
players.subList(0,mid).set(i , players.subList(mid,players.size()).get(i));
players.subList(mid,players.size()).set(i,temp);
}// end if
}// end for
System.out.println(players);
if(players.size() > 2){
tournament(players);
}// end if
}// end tournament
players.subList(0,mid).get(i)is equivalent toplayers.get(i); andplayers.subList(mid,players.size()).set(i,temp)is equivalent toplayers.set(mid + i, temp). No? In fact, I don't see a single place where your use ofsubListis accomplishing anything. Also, your recursive call totournament(players)doesn't really make sense, since that will recurse indefinitely (until you get aStackOverflowException).