I'm having trouble sorting names in alphabetical order using counting sort, forinstance Gregory John, Alex Smith, Adam Richard, Alex Ryan
Output hould be in this order
Adam Richard
Alex Ryan
Alex Smith
Gregory John
My code so far
import java.util.ArrayList;
import java.util.Scanner;
public class Names
{
//private static int[] c;
public ArrayList<String> getUserInput()
{
ArrayList<String> names = new ArrayList<String>();
Scanner in = new Scanner(System.in);
while (in.hasNext())
{
names.add(in.next());
System.out.println(names);
}
in.close();
return names;
}
private static CountingSort(int A[], int B[], int k[])
{
int i;
int C[0];
for(i = 0; i <= k; i++){
C[i]=0;
}
for(int j=1; j <= A.length; ){
C[A[j]] = C[A[j]] + 1;
}//C[i] now contains numbers of elements equals to i
for(int i=1; i < k; i++){
C[i] = C[i] + C[i - 1];
}
for(int j = A.length; j--){
B[C[A[j]]] = A[j];
C[A[j]] = C[A[j]] - 1;
}
}
}