File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "bubblesort" Watch "bubblesort" New topic
Author

bubblesort

chris rous
Greenhorn

Joined: May 16, 2012
Posts: 21

im trying to programm a bubble sort and here is the code i have





im not getting any errors, and i can run the program. but this is what comes out when i run it: 9 8 7 6 5 4 3 2 1 0
any help?
rohit chavan
Ranch Hand

Joined: Oct 08, 2010
Posts: 132

Welcome to coderanch, Chris!

What you are seeing is because you are passing wrong input array. SIMPLY!!!

If you see closely, you are performing all the operations on some input array "a" and not on the array you have defined i.e. "high", inside that for loop.


Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

actually there are couple of faults.

1. why do you introduced high[] ? //assumed a will replace high

2.

your condition i < k can easily break since you are using current index and next index(i and i + 1) remember k = array.length
so either change k to length - 1 or condition to i < k - 1 right?

3. actually what are you doing in swap method? swap the array index's *value*.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to JavaRanch chris rous !
chris rous
Greenhorn

Joined: May 16, 2012
Posts: 21



it works how it was, i just forgot to run in under Main =D
chris rous
Greenhorn

Joined: May 16, 2012
Posts: 21

Seetharaman Venkatasamy wrote:
your condition i < k can easily break since you are using current index and next index(i and i + 1) remember k = array.length
so either change k to length - 1 or condition to i < k - 1 right?



i had changed it to k-1 already but forgot to update, but thats for the help =D
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575



and if you use java 5 then there is a convenient way to print an array meaningfully as in
chris rous
Greenhorn

Joined: May 16, 2012
Posts: 21

Seetharaman Venkatasamy wrote:

and if you use java 5 then there is a convenient way to print an array meaningfully as in


i have java 7
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
chris rous,
Your post was moved to a new topic.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

chris rous wrote:
Seetharaman Venkatasamy wrote:

and if you use java 5 then there is a convenient way to print an array meaningfully as in


i have java 7


Then you can do that. You just need to have at least version 5.
rohit chavan
Ranch Hand

Joined: Oct 08, 2010
Posts: 132

Won't it be better if you print the array after each iteration, so as to understand how the BUBBLE SORT works???...
chris rous
Greenhorn

Joined: May 16, 2012
Posts: 21

rohit chavan wrote:Won't it be better if you print the array after each iteration, so as to understand how the BUBBLE SORT works???...


how do i do that? i understand bubblesort but i have to make a program that compairs from left to right and right to left and not just right to left like bubblesort. it sorts but idk if it does it how i want it to.

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

chris rous wrote:
rohit chavan wrote:Won't it be better if you print the array after each iteration, so as to understand how the BUBBLE SORT works???...
how do i do that?

Have a look at Arrays.toString().

i understand bubblesort but i have to make a program that compairs from left to right and right to left and not just right to left like bubblesort. it sorts but idk if it does it how i want it to.

Rohit's suggestion would probably help you to see how it's doing its job; but only you can tell if it's working "how you want it to".

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: bubblesort