• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Help me with BubbleSort

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I have with the bubbleSort. Let me know what I am doing wrong if you would.
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic