Java Alphabetize Algorithm Insertion sort vs Bubble Sort
- by Chris Okyen
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.