@Dan Storjohann: Welcome to Ranch!
I think you must read about strings. In
java, Strings are not array of characters.
More over this code of yours is not going to work for mixed case input.
Let us consider that you are going to work with only one case(either lower or upper), and then try to sort out the problem.
Before moving on to you solution:
Few tips:
1)avoid using array of characters.. it doesnt do any good.
2)avoid using complicated form of any algorithm.
Did you work this bubble sort out on pencil and paper?
If not then I will advise you to do it,you will learn much more.
After you do this, just think of how can you sort any thing, compare the values and the replace them in proper position, this is what a comparison based sorting algorithm is all about.
The name and efficiency of the algorithm is derived from the way elements are compared and replaced.
Programmers have different views on each versions of algorithm( google for bubble sort with early exit and so on, sky is the limit.)
for me,a simple bubble sort works like this:
compare the first element with the adjacent element, compare on criteria(increasing or decreasing order), swap them.
repeat the above code for as many times as the number of elements.
So let us now solve your problem of
peopleassuming that is supposed to be sorted in ascending order.
so now follw the steps.
compare first element and its adjacent element: is e<p? yes..swap.
now its
epople
compare the second element with the adjacent element: is o<p? yes..swap.
now its
eopple
compare the third element with the adjacent element: is p<p? no..dont swap.
now its
eopple
compare the fourth element with the adjacent element: is l<p? yes..swap.
now its
eoplpe
compare the fifth element with the adjacent element: is e<p? yes..swap.
now its
eoplep
now repeat this for as many times as the number of characters, in this case its 6 and you can see that your input is sorted.
i will suggest you to write your bubble sort is simple manner as above using just two for loops.
If you still don't get it, then post your work,then lets see further.