|
-
November 24th, 2016, 05:44 PM
#1
Parallel Counter Sort
First of all, let me explain that counter sort is an optimized version of counting sort. Counter sort takes the highest and lowest values and then makes an array from the lowest to the highest and counts the number of times that the index of the counter array in in the sorting array. Then it creates a new array by placing the index from the counter array back in sorted order.
Right now when I run the current code I have I get an exception:
Exception in thread "Thread-1" java.lang.ArrayIndexOutOfBoundsException: 10
at ParCountSor.Thread1.run(Thread1.java:48)
So, somehow I am reach outside of my array or not stopping. I'm looking through it and could use some fresh eyes to help me figure out where I am going wrong with my sort and how I'm going through the array.
Code:
package ParCountSor;
import java.util.Random;
import java.util.Scanner;
public class CountSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i, j = 0;
int counter = 0;//test for how many times it runs.
int[] data;
Random ranGen = new Random();
//Accept in the Command Line Parameter
//If the parameter is correct.
if(args.length == 2){
int testnum = Integer.parseInt(args[1]);//how many times it will run
for (i = 0; i != testnum; i++){
int[] ranarray = new int [10];//random array to be sorted
int arraynumb = i;//just to tell what array is being sorted
arraynumb++;//make the number more real
System.out.println("For array " + arraynumb);
for (j = 0; j < ranarray.length; j++){
int ran = ranGen.nextInt(301); //Randomly generates number (0 - 19)
System.out.print(" " + ran);
ranarray[j] = ran; //Random number added
data = ranarray;
}//for
System.out.println("");
Thread1 t1 = new Thread1(ranarray);//pass over to the thread
t1.start();//start the thread
try {
t1.join();//bring back the values from thread 1
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
counter++; //Counter + 1
}//for
System.out.println("\nSorted " + counter + " time(s)");
//Print out how many successful times it sorted
}else if(args.length > 2 ) {
System.out.println("Too many arguments supplied.");
}
//else too few arguments
else{
System.out.println("One argument expected.");
}
}
}
Thread1 class
Code:
package ParCountSor;
public class Thread1 extends Thread{
private static int[] result;
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 max = findmax();
int min = findmin();
int range = max - min +1;
int [] counts = new int[range];
result = new int [cArray.length];
for(int i = 0; i < cArray.length; i++){
//counts[cArray[i]] = counts[cArray[i]]++;
counts[cArray[i]- min]++;
}
for(int i = 1; i < counts.length; i++){
counts[i] = counts[i] + counts[i-1];
}
for(int i = cArray.length - 1; i >= 0; i--){
//counts[cArray[i]]++;
result[counts[cArray[i] - min]--] = cArray[i];
}
}//end run method
/*public void setResult(int result){
this.result = result;
}*/
public int[] getResult(){
return this.result;
}
}//end Thread1 class
Also posted:
http://www.dreamincode.net/forums/to...-counter-sort/
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
|