• Post Reply Bookmark Topic Watch Topic
  • New Topic

why is my bubblesort not working ?  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, new to this forum and quite new to Java, have a bit of C++ experience.

I wonder why this is not working:



I think the code and what's it intended to do should be pretty clear, if not please ask.

Here's the console output I get:



Seems to me the bubbleSort() method is working on a copy of the passed array (as the array is unchanged afterwards) ?
In C++ I probably would have passed a pointer to an int array, how is this in Java ? (if that's the mistake) ?

Thanks.




 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

Ok - look at the output and you may find that there is something wrong ....

Now look at the bubble sort algorithm and see what you have missed.


(just a hint - bubble sort is O ( n^2 ) is yours?

-steve
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about what happens here...

...on the very first two elements.
 
Dennis Schroeder
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies.

I modified the algorithm like this and it's working fine now.



I also get that this is O (n^2), as mentioned above, because of two nested for.. loops, so I'll mark as solved.











 
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method's return type could be "void", in this case you wouldn't need also return anything, and you'd get array sorted too.
Try to figure out why.
 
Dennis Schroeder
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Method's return type could be "void", in this case you wouldn't need also return anything, and you'd get array sorted too.
Try to figure out why.


Because array is passed by reference, meaning method is working on the original array and not a copy of it.
Correct ?

Good point I hadn't thought about (if that's it), thanks.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Schroeder wrote: . . .
Because array is passed by reference, . . .
No, There is no such thing as pass by reference in Java®. All values are passed by value, whether reference types or primitives. Some languages do support pass by reference, but not Java®.

What you are passing is a mutable reference type, so you are not altering the reference passed. You are altering the state of the object behind that reference. Try modifying your method like this:-Now print out the original array and the value returned from that methodYou can read about that toString method here.
 
Dennis Schroeder
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:(Whole of last post)


Thanks for clarifying and giving input, appreciated.

I've done what you suggest and I get this on the console:


If it would have been passed by reference, the original passed in array would have also been altered, that's what you wanted to show me I assume ?

I've also read up here:
Java is Pass by Value

which further clarifies it.
So to summarize, Java passes pointers to objects ("adresses to objects", hence "pass by value") to functions but these are called Object references in Java speech, is this correct ?


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!