title: Multithreaded bubblesort using a threshold
body: <p>So I got this assignment where I had to split an array into two, split those split arrays in 2, etc... up to a certain threshold.</p>
  
  <p>So if my threshold was lets say 1000 it would be like:
  10k>2x 5k>4x 2500>8x 1250>16x 625 (numbers in array).</p>
  
  <p>Then I'd have to sort those arrays and merge them:
  16x 625 (merged)>8x 1250 (merged)>4x 2500 (merged)>2x 5000 (merged)>1x 10k. 
  All of them must remain sorted.</p>
  
  <p>So I think I got pretty far but it seems like it does not work the way I want it to. I tried multiple things to figure what is going wrong but I haven#t figured it out which is why I am here.</p>
  
  <p>Can someone help me understand what I am doing wrong and give me a solution?</p>
  
  <p><strong>Bubblesort.java:</strong></p>
  
  <pre><code>package PartThree;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class Bubblesort implements Runnable {
  public List&lt;Integer&gt; arrayList;
  private int threshold;
  private int middle;
  
  public Bubblesort(List&lt;Integer&gt; arrayList, int threshold) {
      this.arrayList = arrayList;
      this.threshold = threshold;
      this.middle = arrayList.size() / 2;
      System.out.println(arrayList.size() + " ha");
  }
  
  @Override
  public void run() {
      if (arrayList.size() &lt;= treshold) {
          sort();
      }else if(threshold&lt;arrayList.size()){
          Bubblesort rLeft = new Bubblesort(arrayList.subList(0, middle), threshold);
          Bubblesort rRight = new Bubblesort(arrayList.subList(middle, arrayList.size()), threshold);
  
  
          Thread tLeft = new Thread(rLeft);
          Thread tRight = new Thread((rRight));
          tRight.start();
          tLeft.start();
          try {
              tLeft.join();
              tRight.join();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
  
          merge(rLeft.arrayList, rRight.arrayList);
          System.out.println(arrayList.size() + ":size");
      }
  }
  
  private void merge(List&lt;Integer&gt; left, List&lt;Integer&gt; right) {
      int i = 0, j = 0, k = 0;
      List&lt;Integer&gt; temp = new ArrayList&lt;&gt;();
      while (i &lt; left.size() &amp;&amp; j &lt; right.size()) {
          if (left.get(i) &lt; right.get(j)) {
              temp.add(k, left.get(i));
              i++;
          } else {
              temp.add(k, right.get(j));
              j++;
          }
          k++;
      }
      while (i &lt; left.size()) {
          temp.add(k, left.get(i));
          i++;
          k++;
      }
      while (j &lt; right.size()) {
          temp.add(k, right.get(j));
          j++;
          k++;
      }
      arrayList = temp;
      temp.clear();
  }
  
  private void sort() {
      int i, j, tijdelijk;
      for (j = 0; j &lt; arrayList.size(); j++) {
          for (i = 1; i &lt; arrayList.size() - j; i++) {
              if (arrayList.get(i - 1) &gt; arrayList.get(i)) {
                  tijdelijk = arrayList.get(i);
                  arrayList.set(i, arrayList.get(i - 1));
                  arrayList.set(i - 1, tijdelijk);
              }
          }
      }
  }
  }
  </code></pre>
  
  <p><strong>Implementation:</strong></p>
  
  <pre><code>package PartThree;
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Random;
  
  public class API {
  private Random random = new Random();
  private List&lt;Integer&gt; array_unsorted_main = new ArrayList&lt;&gt;();
  private int maxSize;
  
  private Thread t1;
  
  private void run(){
      this.maxSize = 10000;
  
      for (int i = 0; i &lt; maxSize; i++) {
          array_unsorted_main.add(random.nextInt(10000));
      }
      t1 = new Thread(new Bubblesort(array_unsorted_main,1000));
      t1.start();
      try {
          t1.join();
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  
  }
  
      public static void main(String args[]){
          new PartThree.API().run();
      }
  }
  </code></pre>
  
  <p>Please help me out, thank you for your time.</p>