Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursion Troubles
    primarykey
    data
    text
    <pre><code>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 &lt;Integer&gt; nums = new ArrayList&lt;Integer&gt;(); while (input.hasNextInt()) { nums.add(input.nextInt()); } System.out.println(nums); tournament(nums); } public static void tournament(ArrayList &lt;Integer&gt; 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 &lt; midPoint2; i++) { // while is bigger than mid point, increment by one if (list.get(i) &lt; list.get(midPoint)) { Collections.swap(list, i, midPoint); } midPoint++; } System.out.println(list); int newPoint = midPoint2/2; midPoint2 = newPoint; } tournament(list); } } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload