java - Parallel sort not finding the top numbers -


i'm writing program supposed find 25 top numbers in large array using threads. algorithm seems work fine, when comparing result arrays.sort-ed version of original array, seems top 25-list misses of numbers. hate posting code in question, i'm stuck on this, , has been couple of hours now. i'd love figuring out what's wrong. here classes:

main.java

import java.util.arrays; import java.util.random;  public class main {     public static void main(string[] args) {         final int num_thrs = 4;          int[] numbers = new int[500];         random generator = new random(500);          for(int = 0; < numbers.length; i++) {             numbers[i] = math.abs(generator.nextint());         }          thread[] thrs = new thread[num_thrs];         numberthread[] nthrs = new numberthread[num_thrs];          long starttime = system.currenttimemillis();          for(int = 0; < thrs.length; i++) {             int start = getstart(i, thrs.length, numbers.length);             int stop = getstop(i, thrs.length, numbers.length);              nthrs[i] = new numberthread(numbers, start, stop);             thrs[i] = new thread(nthrs[i]);             thrs[i].start();         }          (int = 0; < thrs.length; i++) {             try {                 thrs[i].join();             } catch (interruptedexception e) {                 e.printstacktrace();             }         }          int[] top = new int[25];         int[] indices = new int[num_thrs];          (int = 0; < indices.length; i++) {             indices[i] = 24;         }           for(int = 0; < top.length; i++) {             top[i] = getmax(nthrs, indices);         }          (int = 0; < top.length; i++) {             system.out.println(top[i]);         }         }      public static int getmax(numberthread[] thrs, int[] indices) {         int maxnum = 0;         int maxidx = 0;         for(int = 0; < indices.length; i++) {             if(indices[i] >= 0) {                 if(thrs[i].topnums[indices[i]] > maxnum) {                     maxnum = thrs[i].topnums[indices[i]];                     maxidx = i;                 }             }         }         system.out.println("iterate");         indices[maxidx] = indices[maxidx]-1;         return maxnum;     }      public static int getstart(int i, int total, int len) {         return i*len/total;     }      public static int getstop(int i, int total, int len) {         if(i != total-1) {             return (i+1)*len/total;         }          return len-1;     } } 

numberthread.java

public class numberthread implements runnable {      int start, stop;     int[] numbers;     int[] topnums;      public numberthread(int[] numbers, int start, int stop) {         this.numbers = numbers;         this.start = start;         this.stop = stop;         this.topnums = new int[25];          system.out.println(start + " " + stop);     }      @override     public void run() {         (int = start; <= stop; i++) {             inner: (int j = topnums.length-1; j > 0; j--) {                 if(numbers[i] > topnums[j]) {                     topnums[j] = numbers[i];                     break inner;                 }             }         }     } } 

the numbers printed after main not same top numbers when arrays.sort numbers-array , print top 25. numbers seem missing.

thanks lot in advance.

i think numberthread classes run method isn't doing should do. needs find 25 largest numbers in partition assign it, example if array searching sorted 25 largest numbers in 1 partition its's doing overwriting first number finds that's smaller current number end less 25 numbers , might not largest.

for example consider sequence 98 99 1 2 3... 98 written topnums[19] overwritten 99.

i'm not sure getmax function, seems trying merge different topnums arrays together; arrays aren't sorted don't see how can work.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -