# Java Alphabetize Algorithm Insertion sort vs Bubble Sort

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.

-
May be you are looking for this. This is like your question on stackoverflow. –  Vaibhav Agarwal Oct 17 '12 at 16:30
both are "equally" good and a good choice for small data sets –  ratchet freak Oct 17 '12 at 16:30

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.

-
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

If you only have 3 strings the difference between general purpose sorting algorithms is insignificant. Write what is simplest and move on with your life. Unless you're sorting many thousands of strings and doing it multiple times per second, you've spent more time writing this post than you'd ever save in your lifetime by your choice of sort.

-