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
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())
{
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;
}
}
}``````

Could you give us your main method, or how those 2 methods you posted are used together ? I don't see what a method with integer arrays have anything to do with your problem, unless they're the letter number of in the alphabet of each letter.

Next time, please copy & paste whatever compiler complains or error you got after running. Saying that you have trouble doing it doesn't really help anyone to understand your problem. Many of us do not have time to go through your code and look for error.

Anyway, get back to your code... in getUserInput() method line 11~15, I am not sure that your loop works correctly? How do you input a null value to the loop in order to exit the loop? You should set up a rule to exit the loop. In this case, you may say if the incoming value is equal to "q" or "quit" then you quit the loop.

In CountingSort() method line 22, you declare an array incorrectly. The array of integer should be declared as int[] C = new int[A.length].

Hmm... Actually after looking at the whole code in the method, you just copy & paste the pseudo code of the algorithm! Before I go any further, I must ask you to fix the syntax of "for" loop first...

Could you give us your main method, or how those 2 methods you posted are used together ? I don't see what a method with integer arrays have anything to do with your problem, unless they're the letter number of in the alphabet of each letter.

I was attempting to have an output with arranged names in alphabetical order only strings but begining of the name i have AS00001 for Alex Smith and AR00002 Adam Richard

I'm having trouble sorting names in alphabetical order using counting sort

Not surprising really - the counting sort algorithm is for sorting small integers (or objects keyed by small integer keys). Are you sure you are trying to do the right thing here?

ps This is a cross-post, please check the DaniWeb Member Rules.
http://stackoverflow.com/questions/7564973/name-sorting-using-counting-sort

Hmm... The person is trying to fish for an answer for codes... The person even ignored what I pointed out for the error/bug of the program...

I think the sort can still be done in this case. I have implemented the sort just to test it out and it worked. Though, I guess I will give up this post because of that cross-post. Let the other answers. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.