We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,389 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Programming Assistance

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;   
   }
}
}
4
Contributors
5
Replies
8 Hours
Discussion Span
1 Year Ago
Last Updated
6
Views
nyemba
Newbie Poster
3 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 0

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.

Aviras
Junior Poster in Training
87 posts since Jul 2008
Reputation Points: 24
Solved Threads: 8
Skill Endorsements: 0

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...

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

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

nyemba
Newbie Poster
3 posts since Sep 2011
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 0

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

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

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. ;)

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0733 seconds using 2.74MB