Stephen: First, a point of order: please use the [CODE] and [/CODE] tags when posting code; it preserves the indentation and makes it easier to read. (You can go back and edit you post, replacing the *code* with the appropriate tags...) Now, on to the meat of the matter: Are you having a problem getting your bubble sort code working, or a problem understanding what a bubble sort is? I don't see any attempt at a bubble sort in your code, so I will assume the latter. Since I am not in the habit of doing other homework, I will offer some pointers and explainations to bubble sorting, but the actual code is an exercise left to the reader A bubble sort is a simple (yet highly ineffecient) method of sorting a List/array of objects. It is called "bubble sort" because it forces the item with the largest value to the end of the array. It then finds the item with the next highest value and ofrces it to the end. To accomplish this, it iterates through the array and compares an item with the next in the array. If the first item is greater than the second, it swaps the positions of the two items. It then moves down the array in such a manner that the item with the greates value will constantly be being swapped into the next position, until it reaches the end of the array. As a hint, I will state that bubble sorts require nested for loops. I hope that this helps.

I have messed with it and it is not working. I do understand how the bubbleSort works, but the example in the book makes so many method calls that I find myself just sitting and staring. Also, thanks for the code help, it does look a lot better this way. I know that you don't want to do my homework and believe it or not, but I don't want you to because I need to learn how to do this stuff for tests. I just wish that you could see this example.

Stephen Norris

Ranch Hand

Posts: 44

posted 14 years ago

This is what I have with the bubbleSort. Let me know what I am doing wrong if you would.

What sort of output are you getting? Assuming that your swap function execute properly, I'm not seeing anything overtly wrong with the code at a first glance. Just one thing to note: you can increase performance slightly by stating in you inner for loop:

Also, you will need a semicolon at the end of the call to the swap function, and I suggest putting curly braces around that if-block, even though it is only one line. It helps readability (expecially when the closing curly braces for the for loops are mis-aligned... )

A bubble sort is a simple (yet highly ineffecient) method of sorting a List/array of objects.

On a side note, I have issues with such a blanket statement. Usually a bubble sort is inefficient, yes. Usually it's the wrong choice. But not always. In fact, sometimes, it's the BEST choice, and is VERY efficient.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad: