import java.util.Arrays;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
import java.util.List;
public class TennisTournament {
public static void main (String [] args) {
Scanner input = new Scanner(System.in);
ArrayList <Integer> nums = new ArrayList<Integer>();
while (input.hasNextInt()) {
nums.add(input.nextInt());
}
System.out.println(nums);
tournament(nums);
}
public static void tournament(ArrayList <Integer> list) {
int midPoint = list.size()/2; // returns the index number of half of the lists size
int midPoint2 = list.size()/2;
if (list.size() != 1 || midPoint != 1) {
for (int i = 0; i < midPoint2; i++) { // while is bigger than mid point, increment by one
if (list.get(i) < list.get(midPoint)) {
Collections.swap(list, i, midPoint);
}
midPoint++;
}
System.out.println(list);
int newPoint = midPoint2/2;
midPoint2 = newPoint;
}
tournament(list);
}
}
Okay, so I’m quite new to idea of recursion and right now, all I’m getting is an infinite loop. So in the first method, I wanted to analyse to the array by dividing it in half, and and compare the first element of the first half of the array with the first element of the second half of the array, and if the second element was bigger, then swap And that’s all working fine, it’s doing exactly what I want it to do.
[3, 5, 8, 2, 1, 7, 6, 4]
[3, 7, 8, 4, 1, 5, 6, 2]
The next step I want to take is I just want to half the elements i’m working on. So in the first instance, I wanted to half the number of elements i’m working, so instead of 0-list.size, i want it to work on 0-list.size()/2, so it will only swap the first four numbers, and then do it again, on 2 numbers until the mid point becomes one.
Just a little insight on how I could achieve that would be great.
And no, not homework.
I guess you need to stop splitting, when the list size is smaller than 2.