import java.util.*; class MultiRadixPar{ int n; int [] a; final static int NUM_BIT = 7; // alle tall 6-11 .. finn ut hvilken verdi som er best int nThreads = Math.min( Runtime.getRuntime().availableProcessors(), 8); int [] radixMulti(int [] a) { long tt = System.nanoTime(); // 1-5 digit radixSort of : a[] int max = a[0], numBit = 2, numDigits, n =a.length; int [] bit ; // a) finn max verdi i a[] for (int i = 1 ; i < n ; i++) if (a[i] > max) max = a[i]; while (max >= (1L< 0) bit[i]++; } int[] t=a, b = new int [n]; for (int i =0; i < bit.length; i++) { radixSort( a,b,bit[i],sum ); // i-te siffer fra a[] til b[] sum += bit[i]; // swap arrays (pointers only) t = a; a = b; b = t; } if (bit.length%2 != 0 ) { // et odde antall sifre, kopier innhold tilbake til original a[] (nå b) System.arraycopy (a,0,b,0,a.length); } return a; } // end radixMulti class Worker implements Runnable { int[] a, b, count; int start, end, shift, mask; int id; Worker(int[] a, int[] b, int[] count, int start, int end, int shift, int mask) { this.a = a; this.b = b; this.count = count; //Arrays.copyOf(count, count.length); this.start = start; this.end = end; this.shift = shift; this.mask = mask; } public void run() { // d) move numbers in sorted order a to b for (int i = start; i < end - 1; i++) { b[count[(a[i]>>>shift) & mask]++] = a[i]; } } } /** Sort a[] on one digit ; number of bits = maskLen, shiftet up 'shift' bits */ void radixSort ( int [] a, int [] b, int maskLen, int shift){ int n = a.length; int mask = (1<>> shift) & mask]++; } // c) Add up in 'count' - accumulated values int j = 0; int acumVal = 0; for (int i = 0; i <= mask; i++) { j = count[i]; count[i] = acumVal; acumVal += j; } // Parallel Timer td = new Timer().start(); int d = n / nThreads; Thread[] threads = new Thread[nThreads]; Worker[] workers = new Worker[nThreads]; for (int i = 0; i < nThreads; ++i) { int start = d * i; int end = i == nThreads - 1 ? n : d * (i + 1); Worker w = workers[i] = new Worker(a, b, count, start, end, shift, mask); Thread t = threads[i] = new Thread(w); t.start(); } for (Thread t: threads) { try { t.join(); } catch (InterruptedException ex) {} } System.out.println("par D: "+td.end().prettyTime()); }// end radixSort void testSort(int [] a){ for (int i = 0; i< a.length-1;i++) { if (a[i] > a[i+1]){ System.out.println("SorteringsFEIL på plass: "+i +" a["+i+"]:"+a[i]+" > a["+(i+1)+"]:"+a[i+1]); return; } } }// end simple sorteingstest }// end SekvensiellRadix