java - BubbleSort thread doesn't work -


i want make thread sort array bubblesort method, have problems.

this bubblesort class:

package thread; import java.util.arrays;  public class bubblesort implements runnable {  private int[] array; private long start, end;  public bubblesort(int[] array){     this.array=array; }  public void sort(){     int j;     boolean flag = true;       int temp;      while (flag) {         flag= false;         for( j=0;j<array.length-1;j++ ){             if (array[j]>array[j+1]){                 temp = array[ j ];                             array[j] = array[ j+1 ];                 array[j+1] = temp;                 flag=true;                         }         }      }  }  @override public void run() {    start = system.currenttimemillis();    this.sort();    end = system.currenttimemillis();    system.out.println(end-start); }  public long gettime(){     return end - start; }  @override public string tostring(){     return arrays.tostring(array); } 

}

and main class:

package multisort;  import thread.bubblesort;      public class multisort {      public static void main(string[] args) {         int[] x = {12,34,53,1,23,532,102,31,12,0,344,123,5422,12341,22,3410,123,342,233,12342,234432,12334};         bubblesort bs = new bubblesort(x);         thread bsthread = new thread(bs);         bsthread.start();         system.out.println(bs+"\n"+bs.gettime());         /*         bs.sort();         system.out.println(bs); works         */     }  } 

the problem array not sorted if call sort method in run method. can me response?

you don't wait thread finish sorting. stomp on array regardless of other thread doing!

this telling daughter can use car , opening hood , taking out bits of motor.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -