• 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

Trouble with bubble sorting

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you can see the questions is in the code as comments.
Would really appreciate if someone kind could help.


 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Ungerfält wrote:
boolean swapped = true; //Is´t there an easier and more logic way to make the outside loop?
while(swapped){
swapped = false;
for(int i = 0; i<=list.length -2; i++){
if(list[i] > list[i+1]){
int temp = list[i+1];
list[i+1] = list[i];
list[i] = temp;
swapped = true; //This way makes it really hard to understand logically.
}
}
}



This is saying ... keep doing the outer loop until you encounter an iteration of the inner loop that doesn't actually swap anything. It kinda makes sense to me. What do you propose, as a logic change, to make this easier to understand?

Also, note, if understanding is the main concern here -- then isn't just using comments a good idea here?

Henry
 
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
Welcome to the Ranch, Daniel.

When you post code, could you please UseCodeTags (← click). I've done it for you this time.
 
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have also changed the long lines; it wasn't at all neat possibly because you are mixing tabs and spaces. Only use spaces.
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tanks to both of you!
"This is saying ... keep doing the outer loop until you encounter an iteration of the inner loop that doesn't actually swap anything. It kinda makes sense to me."

Yes I understand all that but its just that in my world the loop would stop when it comes to the line, swapped = false? Why cant I just use a do-while loop that says do this while its true or while list[i] !>list[i+1] (not bigger than i+1)

Plus I dont know why I have to have two sets of println codes at the end. And I dont se how they work toghether.. as I havent written all the code by myself.
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lastly,why list.length (-2) ?
In my world it would be -1 ? though the last number, for example in this list (3,2,5,4,1) would be "4", which is minus one from the last and not minus two? The last comparation would be if 4 is greater than 1?
Then just print out the whole list? print(list.length), cause we want the whole list?
 
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I fully understand you, but let's say array {3,2,5,4,1}.
Don't forget that array indexes starts from 0. So the highest index in this array is 4. The length is 5.

length-1 is element with a content 1.
length-2 is element with a content 4.

Is it clear?

 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Ungerfält wrote:Lastly,why list.length (-2) ?
In my world it would be -1 ? though the last number, for example in this list (3,2,5,4,1) would be "4", which is minus one from the last and not minus two? The last comparation would be if 4 is greater than 1?
Then just print out the whole list? print(list.length), cause we want the whole list?



If the array is 20 in length, then it has 0..19 elements. 19 is ARRAY_SIZE - 1. Then if you want to check if you have room to look at the next element, you need index < ARRAY_SIZE - 1. I think index <= ARRAY_SIZE - 2 is confusing.
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its crystal ! ;-) Thanks folks! But what about the post further up..



(Yes I understand all that but its just that in my world the loop would stop when it comes to the line, swapped = false? Why cant I just use a do-while loop that says do this while its true or while list[i] !>list[i+1] (not bigger than i+1)

Plus I dont know why I have to have two sets of println codes at the end. And I dont se how they work toghether.. as I havent written all the code by myself.)

Anyone understands what I mean?
 
Knute Snortum
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

Daniel Ungerfält wrote:in my world the loop would stop when it comes to the line, swapped = false? Why cant I just use a do-while loop that says do this while its true or while list[i] !>list[i+1] (not bigger than i+1)



Because there may be more than one swap. You want to do all the swaps and exit the loop only if you do NO swaps.

Plus I dont know why I have to have two sets of println codes at the end.



Think of it this way: You have a, b, c, d. So you print "a, " then "b, " then "c, " but now you just want to print "d", so you need another print statement. Also, the first three are "print" and the last is "println". Try doing it all in one print statement; it won't work.
 
Liutauras Vilda
Marshal
Posts: 8863
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

Daniel Ungerfalt wrote:Plus I dont know why I have to have two sets of println codes at the end. And I dont se how they work toghether..


Two print statements I think are for the very simple reason, because owner of this code tried to avoid coma after the last element. Can't see any other reason at the moment.


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