logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
European Travel
Action Movies
Bible Basics
Houseplants
Romance Movies
Creativity
Family Travel


dailyclick
All times in EST

Full Schedule
g
g ASP Site

BellaOnline's ASP Editor

g

Bubble Sort Code Technique


If you are sorting content into an order, one of the most simple techniques that exists is the bubble sort technique. In essence you start at one end of the list, move one by one to the other end of the list, and if you ever reach a situation where two items are out of order, you swap them.

This is one of the most simple sort techniques that exists, that is taught in any basic programming course.

Let's say you have an array of Grades(5). You want to sort them so that the highest grade is at the beginning of the list, and that the lowest grade is at the end of the list. Note that this is NOT REAL CODE. This is an example of the concept, that you can apply to any language.

So you would fill Grades(5) with the values. Then you would say -

for ctr = 1 to 4
.for ctr2 = ctr + 1 to 5
..if Grades(ctr) < Grades(ctr2) then
...Temp = Grades(ctr)
...Grades(ctr) = Grades(ctr2)
...Grades(ctr2) = Temp
..end if
.next
next

So in essence you have the outer loop stepping through each item but the very last one. The inner loop steps through every untried item from whereever you are in the outer loop, going forward. The two are compared and if the higher number is not "on top", the are swapped.

Let's say your array is 90 70 80 100 60

On the first time through the loop, you begin with 90 (value 1) and compare it with the others, in order. Is 90 < 70? No. Nothing happens. Is 90 < 80? No. Is 90 < 100? Yes. The 100 takes spot 1, and the 90 takes spot 4. Is 100 < 60? No. Now we have guarantee that spot #1 is definitely the largest number in the entire array.

Now we work on the second largest number. We move on to stop 2. Is 70 < 80? YES, they swap spots. Is 80 < 90? YES, they swap spots. Is 90 < 60? No, so the 90 stays in spot 2.

And so it goes, until the entire array is settled in proper order. You can of course arrange the array in ascending or descending order just by switching the < to a > !
Add Bubble+Sort+Code+Technique to Twitter Add Bubble+Sort+Code+Technique to Facebook Add Bubble+Sort+Code+Technique to MySpace Add Bubble+Sort+Code+Technique to Del.icio.us Digg Bubble+Sort+Code+Technique Add Bubble+Sort+Code+Technique to Yahoo My Web Add Bubble+Sort+Code+Technique to Google Bookmarks Add Bubble+Sort+Code+Technique to Stumbleupon Add Bubble+Sort+Code+Technique to Reddit




RSS | Related Articles | Editor's Picks Articles | Top Ten Articles | Previous Features | Site Map


For FREE email updates, subscribe to the ASP Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


Content copyright © 2014 by Lisa Shea. All rights reserved.
This content was written by Lisa Shea. If you wish to use this content in any manner, you need written permission. Contact Lisa Shea for details.

g


g features
Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2014 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor