This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes BubbleSort Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BubbleSort" Watch "BubbleSort" New topic
Author

BubbleSort

Kathleen Tillman
Greenhorn

Joined: May 09, 2009
Posts: 12
Hi All -

I'm having a bit of a problem: My assignment is to enter an array of numbers and then sort them using bubble sort. I have got the bubble sort working(finally), but I also have to do four things: show the unsorted array before sort, show the unsorted array after it has been sorted; show how many passes; and show how many swaps. Right now, I've only gotten so far as sorting, I haven't even touched the counter yet, which is not what I'm asking about (yet) because I haven't worked on it. What I am asking about, though, is help with the problem I'm currently having. I have gotten the sorted array to print, but I can't seem to get the array to print before the sort. My code is below. Does anybody have any idea how to get the program to show the array before it's sorted?


It compiles correctly somehow, but when I run it this is what I get:

Before sorting, the list elements are:
[I@66848c
After sorting, the list elements are:
5 12 17 23 38 44 77 84 90

Any help I could get would be appreciated.

Thanks,
Kat
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Why can't you use the same code, that you used to print the array after the sort, to print the array before the sort?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kathleen Tillman
Greenhorn

Joined: May 09, 2009
Posts: 12
I tried that.

I copied



All I added was to change System.out.print to System.out.println instead to separate and I got this:

----jGRASP exec: java Program7
Before sorting, the list elements are:
23
After sorting, the list elements are:
5 12 17 23 38 44 77 84 90


I'm not at all sure why none of this isn't working.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

You didn't copy the complete for loop -- which means that it shouldn't compile.

Can you show us the code, with it copied, so we can see what you did wrong?

Henry
Kathleen Tillman
Greenhorn

Joined: May 09, 2009
Posts: 12
Sure. No problem. I'm trying to post everything, right, sorry.

Here's the code. I'm not sure where the mistake is.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Carefully evaluate where you are putting your braces.

The indentation style helps to reveal these types of problems. It's red flag when you have two close braces at the same level in properly indented code.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kathleen Tillman
Greenhorn

Joined: May 09, 2009
Posts: 12
Ok, I got that. Thank you. It was just a matter of moving that bracket up. One more question on this. I cannot seem to make the next line print separately. Right now the code looks like this:



and the return when I run looks like this


Before sorting, the list elements are:
23 17 5 90 12 44 38 84 77 After sorting, the list elements are:
5 12 17 23 38 44 77 84 90
----jGRASP: operation complete.


Why is it not separating the line of numbers and the "after sorting, the list elements are:"

Sorry, to ask so many questions.

Also, can you lead me to a website or somewhere that can help me when it comes to inserting a counter to count swaps and passes?

Thanks so much, Kat
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39062
    
  23
Lose the copied loops. What you really want is a public static void printIntArray(int[] array) method. Then you call it before and after sorting.
You will have to insert a variable as a counter, and increment it each time you make a comparison.
You are getting the two outputs on the same line because you have never told the JVM to go onto a new line. Look at this version of a method you should have seen written after "System.out."
Kathleen Tillman
Greenhorn

Joined: May 09, 2009
Posts: 12
Thanks all for your help. I'm hoping that I'm doing this right by posting a continuing question for the same problem.
I've worked on this code all weekend to try to get it to work and I think I got most of it correct. The problem I'm having is I don't think I'm getting the passes working correctly and I'm not sure about the swaps either, though I finally am making it work as showing an answer.
The other problem I'm having is that I can't seem to get everything to print on separate lines. Can you please look at my code below and see if you see anything wrong?



This is my output...


Before sorting, the list elements are:
23 17 5 90 12 44 38 84 77 Total number of Swaps: 12
Total number of Passes: 12
After sorting, the list elements are:
5 12 17 23 38 44 77 84 90
----jGRASP: operation complete


Any help anybody could give me would be greatly appreciated.

Kat
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42035
    
  64
You may want to read up on the difference between the "print" and the "println" method of the java.io.PrintStream class. Note that you can start a new line by calling "println()" without any argument.


Ping & DNS - my free Android networking tools app
Kathleen Tillman
Greenhorn

Joined: May 09, 2009
Posts: 12
Thank you so much! That took care of the line problem.

Is there any possible way someone can also help me with the swaps and passes portion of the problem? Like I said, I have portions showing up, but I'm not sure if they are correct.

Thank you so much!

Kat
 
GeeCON Prague 2014
 
subject: BubbleSort