Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BubbleSort

 
Kathleen Tillman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21127
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kathleen Tillman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21127
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64856
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kathleen Tillman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kathleen Tillman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic