|
-
November 25th, 2016, 03:30 PM
#3
Re: Parallel Counter Sort
So, working through commented out code, a section, so I know where it was changing the first value, but still doesn't want to sort the data. So question is what is wrong with my sort?
Code:
//sort original array
int j = 0;
for(int i = 0; i < counts.length; i++){
while(j < counts[i]){
cArray[j++] = i + minVal;
counts[i]--;
}//end while
Rest of my code Thread1 class code (the CountSort hasn't changed since above)
Code:
package ParCountSor;
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
//make count of how many times it appears in the array
/*for(int i = 0; i < cArray.length; i++){
counts[cArray[i]- min]++;
}//end for*/
for(int i =0; i < cArray.length; i++){
counts[cArray[i] - minVal]++;
}
/*//make sure of positions in final array
for(int i = 1; i < counts.length; i++){
counts[i] += counts[i-1];
}//end for*/
//sort original array
int j = 0;
for(int i = 0; i < counts.length; i++){
while(j < counts[i]){
cArray[j++] = i + minVal;
counts[i]--;
}//end while
}//end for
//print sorted array
System.out.println("Sorted Array: ");
for(int i = 0; i < cArray.length; i++){
System.out.print(cArray[i]+" ");
}//end for
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
|