26287 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Oz shared services
collapse looks bad
for NetApp
Googlerola loses
bid to ban US Xbox
sales after ITC
slapdown
Samsung, carriers
tout first Tizen
mobes for late 2013
Google to double
encryption key
lengths for SSL
certs by year"s end
Facebook Home phone
plans canned in the
UK
Joyent cuts prices
on cloudy
infrastructure
Yahoo! continues
quest for youth
with yet another
acquisition
Internet2 superfast
boffin network
peers with Azure
cloud
Google slashes App
Engine NoSQL data
storage prices by
25 per cent
Orange customer
clobbered with SIX
FIGURE phone bill
Slashdot
Meet the 23-Ton
X-Wing, the World"s
Largest Lego Model
Android Malware
Intercepts Text
Messages, Forwards
To Criminals
Scientists Growing
New Crystals To
Make LED Lights
Better
Google Takes Street
View To the
Galapagos Islands
Bitcoin"s Success
With Investors
Alienates Earliest
Adopters
WIPO Panel Says Ron
Paul Guilty of
Reverse Domain Name
Hijacking
Red Hat"s Diane
Mueller Talks About
OpenShift (Video)
5-Pound UAV Flies
For 50 Minutes,
Streams HD From
Over 3 Miles
Google Code
Deprecates Download
Service For Project
Hosting
Sears Is Turning
Shuttered Stores
Into Data Centers
Article viewer

The Bubblesort algorithm



Written by:dimport
Published by:dimport
Published on:2003-06-21 07:19:46
Topic:Ruby
Search OSI about Ruby.More articles by dimport.
 viewed 10295 times send this article printer friendly

Digg this!
    Rate this article :
There are many times when you need to put seemingly random data in order. Here I'll explain one of the algorithms for organizing that data, the Bubblesort algorithm. (This isn't just ruby, it can be ported to any language)

The basic concept of a bubble sort is very simple - Go through the data, from the bottom. Take data in pairs. If the lower piece of data is larger than the higher one, swap them around. Repeat up to the top. Then, repeat the whole process, but stop one from the top, then 2... until you've run through all the data. The highest data basically 'floats' to the top, hence the name.
 
def Bubblesort(array)
    size = array.size() # find the size of the array
    pass = size
    while pass > 2 # If we're down to less than 2 pieces of data to sort this won't work, and it's finished
        (pass-1).times do |current|
                if array[current] > array[current+1]
                      array[current],array[current+1] = array[current+1], array[current] # swap them around
                end # if
        end # times
     end # while
     return array
end
Get it now? It's not a very efficient algorithm, but it's easy to implement, and not bad if you need something in a hurry. (Also note that ruby has Array.sort, which sorts it automatically, but with a different, probably more effecient algorithm)Here's a C implementation as well, if it's easier to understand: (swap() is a nonexistent function, but you get the idea)
 
void BubbleSort(int *data, int size){ // pass an array with the data, this func works right onto the data.
     int pass = size,k,current; //k for the iterator, instead of i, just for a change
     while (;pass > 1;pass--)
          for (k=0;k data[current+1])
               swap(data[current],data[current+1]); // my nonexistent function
      return;
}
Well, you should have it figured out by now. Hopefully you'll be able to use this somewhere, but with many of the OO languages coming out, you won't need it, the Array class (if it exists) has the sort function.

This article was originally written by rekless

Did you like this article? There are hundreds more.

Comments:
Anonymous
2008-01-05 02:54:10
The ruby bubble sort doesn't seem to work. It goes into an infinite loop due to the pass variable. Here's a quick version I wrote up

  def Bubblesort(array)
    array.each_index do |i|
      array.each_index do |k|
        array[i], array[k] = array[k] , array[i] if array[i] < array[k]
      end
    end
    array
  end
Anonymous
2010-01-12 20:10:09
This is a more pure version of the bubble sort algorithm:

def bubblesort(array)
    swapped = true
    while swapped do
      swapped = false
      for i in 0..array.length-2
        if array[i] > array[i+1]
          array[i], array[i+1] = array[i+1] , array[i]
          swapped = true
        end
      end
    end
    array
end


Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
reacher
The Ruby Way on Wed 27th Aug 6pm
I've recently come back to OSIX and taken all the challenges again. This next time around, however, I'm using Ruby to tackle them. I will admit that my old C versions of some solutions ran faster (especially the brute force ones), but coding the Ruby ve
bb
Ruby on Rails on Tue 10th Oct 9am
I installed ruby and wrote my first ruby on rails web application. It was quite an enjoyable experience. Certainly more fun than asp.net (which is bloody unproductive) I suggested we knock some internal application up in it for my company to have ap


     
Your Ad Here
 
Copyright Open Source Institute, 2006