home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

need help with an alphabetizing algorithm

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1460
Discussions: 200,931, Posts: 2,379,161, Members: 246,297
Old March 18th, 2002, 12:08 PM   Digg it!   #1 (permalink)
Senior Member
 
Join Date: Oct 2001
Posts: 881
Send a message via AIM to zskillz
need help with an alphabetizing algorithm

ok... here's the deal. (using java) I'm reading in a comma delimited file and creating an object from each line. The rules of the assignment are that I have to alphabetize the objects and put them in a linked list as I'm reading them in. I'm having a hell of a time with this. I can get it so that all of the A's, B's, etc are together, but I cannot get them to be alphabetized within their respective areas. Can anyone offer me some help with this problem. it's annoying as heck. I think that I could use a recursive method, but I was hoping to speed up runtime by avoiding a bunch of method calls and instead just using some sort of while loops or something.

thanks all
-Z
zskillz is offline   Reply With Quote
Old March 18th, 2002, 01:20 PM     #2 (permalink)
ph34r t3h g04t
 
Whir's Avatar
 
Join Date: Oct 2001
Location: Kingsford, MI
Posts: 19,538
Blog Entries: 7
Do you HAVE to alphabetize them as you write the list, or can you write the list after you alphabatize them?? If you just have to write them in order, you could just save all the objects into a string array and triquick sort or bubble sort them. Otherwise, you could bubble sort each time you add something to your linked list...

I'm not a java programmer, but I don't see how that wouldn't work in java...

-Whir
Whir is online now   Reply With Quote
Old March 18th, 2002, 01:32 PM     #3 (permalink)
Ultimate Member
 
SickPup404's Avatar
 
Join Date: Oct 2001
Location: Sussex county, DE
Posts: 1,385
I'm a mainframe programmer, and what it sounds like you want to do is called a bubble sort.

A bubble sort looks kinda like this (pseudo-like pseudocode )

- You have a list that is "X" items big.
- Do "X" times
- Do "X - 1" times
- Is Item "X" > Item "X+1"
- Item "X" and Item "X+1" swap places
- Go back to inside Do
- Go back to outside Do

I could give you an example in COBOL, but I'd be embarassed to let people know I still speak it!

Hope that makes sense.

I had this nicely indented....

[edit for last comment]
__________________
There are only 10 types of people that understand binary.....

Last edited by SickPup404 : March 18th, 2002 at 01:35 PM.
SickPup404 is offline   Reply With Quote
Old March 18th, 2002, 02:07 PM     #4 (permalink)
Senior Member
 
Join Date: Oct 2001
Location: Utah
Posts: 551
I did something similar in C.
I would assume you would want to put them in order as you put them in the linked list.
I would suggest a doubly linked list. With 1 list being 26 long, for the 26 letters of the alphabet, with a linked list off of each one of those that is dynamic, where you put each word in order as you go. And, once you can picture it, it really isn't as hard as it sounds.
Plus, it should be quite a bit faster than bubble sorting at the end, or bubble sort for each element.

Another way to do it is to put them in a binary tree. Picking m as your root, and then put them in before or after depending on the alphabetical. Then, just traverse the list in different ways for alphabetical, reverse-alphabetical, etc.

fun stuff, but after a whole semester of just that, it gets old, but you sure learn pointers!
dragonb
dragonb is offline   Reply With Quote
Old March 18th, 2002, 02:35 PM     #5 (permalink)
Senior Member
 
Join Date: Oct 2001
Posts: 881
Send a message via AIM to zskillz
yes.. i have to alphabetize them as I write the list... which is really annoying, but that's a strict rule of the assignment. I suppose that a bubble sort is probably the quickest way to do it since the data will almost be totally in order anyway. here's the problem though.... I'm still having trouble comparing words that start with the same letter...

separating by first letter is easy since you can do charAt(0), but after the first letter, it gets much trickier to sort them alphabetically. is there some sort of unknown class/method in java that already does this or am I gonna have to figure it out on my own?

thanks for the help thus far
-Z

dragon... that sounds like a pretty good idea... but I'm pretty new to lists, so I'm wondering if I wanted to print out all of the data in the list.... wouldn't that be a little more complicated? or could I just print the 26 letter list and would that take care of everything else?. besides that... since the list of length 26 is constant... is it possible to build a list of an element in an array?... I mean - I could build an array of 26 (a-z) and then build a list of each element in the array... is this possible?
zskillz is offline   Reply With Quote
Old March 18th, 2002, 03:17 PM     #6 (permalink)
Senior Member
 
Join Date: Oct 2001
Location: Utah
Posts: 551
yea, that would work well, since the 26 is constant.
But, when you go to print it out, then you need code to traverse the array and the lists, where as if they are both linked lists, then there is a lot of similarity, and it can be done pretty nice with recursion.
I'll look into the other problem...

dragonb

<quote>dragon... that sounds like a pretty good idea... but I'm pretty new to lists, so I'm wondering if I wanted to print out all of the data in the list.... wouldn't that be a little more complicated? or could I just print the 26 letter list and would that take care of everything else?. besides that... since the list of length 26 is constant... is it possible to build a list of an element in an array?... I mean - I could build an array of 26 (a-z) and then build a list of each element in the array... is this possible?</quote>
dragonb is offline   Reply With Quote
Old March 18th, 2002, 03:57 PM     #7 (permalink)
Senior Member
 
Join Date: Oct 2001
Posts: 881
Send a message via AIM to zskillz
dragon... i just reread my post and it might not be clear....

i meant... can i build a list off of an array element.... or perhaps I could declare the array as an array of list elements.... then build the lists off of that...

i bet that would work

-Z
zskillz is offline   Reply With Quote
Old March 18th, 2002, 09:45 PM     #8 (permalink)
Banned
 
qball's Avatar
 
Join Date: Oct 2001
Posts: 447
Quote:
(using java) I'm reading in a comma delimited file and creating an object from each line.

why? defeats purpose of comma delimited (how do you alphabatize the commas?)...

Quote:
The rules of the assignment are that I have to alphabetize the objects and put them in a linked list as I'm reading them in.

Is this homework?

strange assignment, why not read 'em and sort?

ADD to assignment:

text file ALREADY sorted!
qball is offline   Reply With Quote
Old March 19th, 2002, 01:35 AM     #9 (permalink)
Senior Member
 
Join Date: Oct 2001
Posts: 881
Send a message via AIM to zskillz
q -
hopefully this will put all your qualms to rest

http://www.cs.colostate.edu/~cs200/a...signment3.html

-Z
zskillz is offline   Reply With Quote
Old March 19th, 2002, 11:23 PM     #10 (permalink)
Banned
 
qball's Avatar
 
Join Date: Oct 2001
Posts: 447
homework.

tho this is interesting:

Quote:
Assignments that behave the same as the compiled code whose implementations stray from these guidelines may be docked points during grading.

that's harsh.

http://developer.java.sun.com/develo...106/index.html
qball is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2874)
Obama the Muslim (14)
California Passes Anti-Flat-HDTV Le.. (39)
Is the PSU I received dead? (10)
windows vista security holes (9)
Install XP pro and a Vista laptop ?.. (11)
HIS HD5770 graphic card question (15)
Print spooler problem (13)
Foreign voltage (10)
Dept. of HS: NSA 'Helped' Develop V.. (15)
A good PSU? (10)
Ideal cheap graph card for PC-Gamin.. (16)
EVGA 9800 gtx help with finding a g.. (8)
New Computer wont recognize XP disc (7)
Recent Discussions
Wireless Televisions. (0)
Install XP pro and a Vista laptop ?? (11)
Partition Magic caused HDD problem (2)
Graphics Card Upgrade Question (1)
favorit (1)
solutions for virtical white lines on.. (1)
Regular Build (3)
Ideal cheap graph card for PC-Gaming? (16)
Fire in DVD (2)
Modern Warfare For the PC (33)
radeon x850xt platinum & shader 3 (3)
Have you switched yet? (84)
Wireless Router+Cable Modems and Much.. (0)
Optical Audio A-B Switch (1)
windows vista security holes (9)
The NTDVM CPU has encountered an ille.. (24)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (34)
Wireless speakers for PC? (11)
Print spooler problem (13)
Help getting around port 80 for camer.. (2)
Display shows 3x5 inch in middle of s.. (3)
monitor will not turn on at all, (1)
World's largest Monopoly Game using G.. (331)
Foreign voltage (10)
FiOS modem/router interfering with ne.. (7)


All times are GMT -4. The time now is 07:58 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28