Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am supposed to "Develop a program that alphabetizes three strings. The program should allow the user to enter the three strings, and then display the strings in alphabetical order." It's instructed that I need to use the String library compareTo()/charAt()/toLowerCase() to make all the characters lowercase so the Lexicon comparison is also a alphabetical comparison.

Input Pseudo Code:

String input[3];

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter three strings: ");

for(byte i = 0; i < 3; i++)
    input[i] = keyboard.next()

The sorting would be

Insertion Sort:

321

2 3 1

2 31

231

1 23

1 2 3

1 23

1 23

123


Bubble Sort

321

231

213

123

Which would be more efficient in this case? The bubble sort seems to be more efficient though they seem to have equal stats for worst best and avg case, but I read the Insertion Sort is quicker for small amounts of data like my case.

share|improve this question
both are "equally" good and a good choice for small data sets – ratchet freak Oct 17 '12 at 16:30

2 Answers

Insertion sort is much better choice then bubble sort, because if the input array is almost sorted, it iterates only few times. As opposed to bubble sort, where small disorder in the array may cause the quadratic behaviour.

share|improve this answer
It is actually possible to optimize the Bubble sort algorithm so it reduces the number of iterations with almost sorted arrays – José Valente Dec 17 '12 at 20:00

May be you are looking for this.

This is like your question on stackoverflow.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.