• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bubblesort

 
chris rous
Greenhorn
Posts: 21
Android Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch chris rous !
 
chris rous
Greenhorn
Posts: 21
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


it works how it was, i just forgot to run in under Main =D
 
chris rous
Greenhorn
Posts: 21
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


and if you use java 5 then there is a convenient way to print an array meaningfully as in
 
chris rous
Greenhorn
Posts: 21
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chris rous,
Your post was moved to a new topic.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10257
59
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic