Distribution count sorting
I have written this piece of code:
public static String DistributionCountingSort(int array3[], int firstindex1, int lastindex1, int arraySize1)
{
int arrayrange = firstindex1 - lastindex1;//lastindex1 is 'l' and firstindex1 is 'u'
int DArray[] = new int[arraySize1];
int SArray[] = new int[arraySize1];
System.out.println(lastindex1); //0
System.out.println(firstindex1);//9
System.out.println(arrayrange);//9
for(int j = 0; j<arrayrange; j++)
{
DArray[j ] = 0;
}
for(int i = 0; i >< array3.length; i++)
{
int A = array3[ i];
DArray[A - lastindex1] = DArray[A - lastindex1] + 1;
}
for(int j = 1; j<=arrayrange; j++)
DArray[ j] = DArray[j - 1] + DArray[ j];
for(int i = array3.length - 1; i>=0; i--)
{
int temp = array3[ i] - lastindex1;
SArray[DArray[temp] - 1] = array3[ i];
DArray[temp] = DArray[temp] - 1;
}//end of for loop
String output3 = "\n\nData items in ascending order\n";
output3 += " "+ SArray[0];
for(int counter = 1; counter < SArray.length; counter++)
output3 += ", "+ SArray[counter];
return output3;
}//end of method DistributionCountingSort
To match this algorithm
ALGORITHM DistributionCounting(A[0...n-1],L,u)
//Sorts an array of integers from a limited range by distribution counting
//Input: An array A[0....n-1] of integers between L and u ( L<= u)
//Output: Array S[0...n-1] of A's elements sorted in nondecreasing order
for j = 0 to u-L do D[ j] = 0 //initialize frequencies
for i = 0 to n-1 do D[A[ i ] - L] = D[A[ i ] -L]+1 //compute frequencies
for j =1 to u-L do D[ j] = D[j-1] + D [ j]
for 1=n - 1 downto 0 do
j = A[ i ] -L
S[D[ j] - 1] = A[ i ]
D[ j] = D[ j] - 1
return S
but I keep getting and out of bound error when I set the array size to anything less than 10. Also I can't change the array value range to anything more than 0-9. Can someone please help me?
Message was edited by:
JuppieJux

