|
-
November 26th, 2016, 05:33 PM
#7
Re: Parallel Counter Sort
So mainly been cleaning up my code and testing. When I now run this, I get this as the result.
For array 1
261 155 13 31 203 298 13 122 237 173
Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: 286
at ParCountSor.Thread1.run(Thread1.java:46)
Sorted 1 time(s)
That 286 is the range variable that is the length of the counts array. Completely confused there. Does it think that I want the value to be 286? Am I set the size of the counts array wrong?
Thread1 (CountSort class hasn't changed)
Code:
package ParCountSor;
import java.util.Arrays;
public class Thread1 extends Thread{
private int[] counts;
public int[] cArray;//half of array to search
public Thread1(int[] ranarray) {
//constructor for firstHalf and target
this.cArray = ranarray;//copy array
}
public int findmax(){
int max = cArray[0];
for(int i = 0; i < cArray.length; i++){
if (cArray[i] > max){
max = cArray[i];
}
}
return max;
}
public int findmin(){
int min = cArray[0];
for(int i = 0; i < cArray.length; i++){
if (cArray[i] < min){
min = cArray[i];
}
}
return min;
}
public void run(){
int maxVal = findmax();//max value
int minVal = findmin();//min value
int range = maxVal - minVal +1;//find the range
counts = new int[range];//counts array
for(int i =0; i < cArray.length; i++){
counts[cArray[i]-minVal]++;
}
int j = 0;
for(int i = 0; i < counts.length; i++){
while(counts[i] > 0){
counts[j++] = i + minVal;
counts[i]--;
}
}
//print sorted array
System.out.println("Sorted Array: " + Arrays.toString(counts));
System.out.println("");
}//end run method
public int[] getCounts(){
return this.counts;
}//end getResult
}//end Thread1 class
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|