This week's book giveaway is in the Java in General forum.We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# Help me with BubbleSort

Stephen Norris
Ranch Hand
Posts: 44
Below is my program and I am lost on how I am going to use bubbleSort on this thing. Please help me because it is due tomorrow.

[ October 29, 2003: Message edited by: Stephen Norris ]
[ October 29, 2003: Message edited by: Stephen Norris ]

Joel McNary
Bartender
Posts: 1840
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.

Stephen Norris
Ranch Hand
Posts: 44
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
This is what I have with the bubbleSort. Let me know what I am doing wrong if you would.

Joel McNary
Bartender
Posts: 1840
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... )

Stephen Norris
Ranch Hand
Posts: 44
When I try to compile, the error says that it can't resolve symbol:
symbol: method swap (int[], int, int)
swap(array, element, element + 1)

John Smith
Ranch Hand
Posts: 2937
When I try to compile, the error says that it can't resolve symbol:
symbol: method swap (int[], int, int)
swap(array, element, element + 1)

Well, where is your swap() method?

fred rosenberger
lowercase baba
Bartender
Posts: 12122
30

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.