473,473 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

String Alphabetising

1 New Member
Greetings all,

I am new to the java programming language. I seem to have hit the wall when I try to sort words a user inputs.

I am trying to sort like four input words from the user and then alphabetise them...

I used the s.compareTo(t) method but the way I did it ended up being much longer then I tought... I must be overlooking something...( I compared each of the words to each other individually )

I am thinking that I could maybe use a loop with the if statement to help me sort with the compareTo method but I am stuck sigh :(.

Any help or pointers to the right dirrection on this would be much appreciated
Jul 7 '06 #1
1 2604
D_C
293 Contributor
There are all types of different sorting algorithms, it is a very big topic, especially for beginners. It sounds like may have done bubble sort, the simplest, but least efficient one.

I think merge sort is the best in terms of run time and least confusing. Quick sort is a little better in practice, but difficult to explain. If you know you will only have four variables, you could type it out, but the recursion of merge sort is very nice.

Suppose you have four variables:
a b c d

compare a and b, swap them if b is smaller.
similarly, compare c and d. Suppose b < a, c < d, then we have two sorted lists:
b a
c d

Then just merge them together, i.e. merge sort. Compare b to c, suppose c < b.
Know we know that c is the minimum since c < d, and c < b < a.
c b a
d

We just need to know where d goes. We already know c < d, so we compare b to d. This comparison determines the next smaller number. Suppose b < d. It doesn't help us in placing d, but we know where b goes now, since c < b < a and c < b < d

Finally we have to compare d and a. Suppose d < a. Now we can have a sorted list because we know how everything ranks, c < b < d < a, so:
c b d a

You should always need five comparisons, if you are going to implement merge sort non-recursively, for four elements. I recommend you check out Google, there are plenty of merge sort, and other sort algorithm implementations :D.
Jul 8 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
2
by: s | last post by:
I'm getting compile errors on the following code: <code> #include <iostream> #include <fstream> #include <list> #include <string> using namespace std;
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
16
by: Steve | last post by:
Hi Guys, I have a string which contains data elements separated by spaces. I also have a function which returns the number of characters from the beginning of the string for a given number of...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.