• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why is my bubblesort not working ?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about what happens here...

...on the very first two elements.
 
Dennis Schroeder
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.











 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic