• 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

for loops...sorter application

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi..i having problem understanding the flow of the for loops...how the bubble sort puts the list in the correct alphabetic order...
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ September 17, 2004: Message edited by: Dirk Schreckmann ]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you'd put the code tags around the relavant part of your post, it'd be easier to read...

but...

think of the two loops as pointers into a stack of cards. the outer one is where you want to put a card. you start at the beginning - you want to find the card that goes in the front of the list. after you find the right card, you can move this finger to the next spot - you now need to find the card for the second spot. once you find the second to last card and put it in place, you know the last card is where it needs to be, so you can quit. that's the outer loop.

the inner loop is the one that keeps track of the cards you're checking to put into the front spot. you don't have to check anything BEFORE your first finger, 'cause you know those are already sorted. so you can start looking at the first card AFTER where your first finger (outer loop) is pointing. that's why it's j = i+1...

anyway, for each of these cards, you look to see if it goes before the one you're first finger is pointing to. if so, you swap the two cards. you may make several swaps as your second finger moves through the list, but when it gets to the end, you know you have one card in the right position.

it's usually not very efficient for large projects, but it can be fast if the data is mostly sorted already. it works something like this (assume we want smallest to largest:


etc.

[ September 16, 2004: Message edited by: fred rosenberger - freakin' spaces]
[ September 16, 2004: Message edited by: fred rosenberger ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kieran pattni,

When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.
 
reply
    Bookmark Topic Watch Topic
  • New Topic