Bubble sort

Just a quick little project that I did while in discrete math. :slight_smile:
The whole sort could be done in one frame, but it’s way cooler to watch it do everything :wink:

http://pastebin.com/EARXF6p6

As always, feel free to use any of the code.

@Ceres But that’s where all the fun comes in, doing something because you want to see your results. Don’t do it because you have to do it or because it’s already done, do it for the fun you will have.

nice one! thanks for sharing.

Thanks! I know that it’s not very useful and there are better algorithms out there, but it was a fun little 20 minute project :slight_smile:

Would be good to visually compare the different sorts of sorting algorithms, this is great!

Thought I would share an example that I had. It’s set to go slow so you can see what’s happening. You can change the speed with the parameter slider. A lower number means a faster speed. What I demo is to start at the bottom and compare a size to the size above it. If the bottom one is smaller than the one above it, I flip the two and continue to move it up. When I reach the top, I start at the bottom again. I guess it looks like a bubble moving it’s way to the top of the screen.


displayMode(FULLSCREEN)

function setup()
    parameter.integer("delay",1,20,10)
    rectMode(CENTER)
    tab={}
    for z=1,100 do
        table.insert(tab,math.random(10,WIDTH-10))
    end  
    bottom=100 
    done=false
    a=0
end

function draw()
    background(40,40,50)
    fill(255)
    fontSize(10)
    for z=1,100 do
        fill(255)
        if z==bottom then
            fill(255,0,0)
        end
        rect(WIDTH/2,HEIGHT-z*10,tab[z],HEIGHT/100-2)
        fill(0, 134, 255, 255)
        if z==bottom then
            fill(255)
        end
        text(tab[z],WIDTH/2,HEIGHT-z*10)
    end
    if not done then
        srt()  
    else
        fill(255)
        fontSize(30)
        text("Done",600,950) 
    end
end

function srt()
    a=a+1
    if a<delay then
        return
    end
    a=0
    if tab[bottom]<tab[bottom-1] then
        tab[bottom],tab[bottom-1]=tab[bottom-1],tab[bottom]
        flip=true
    end
    bottom=bottom-1
    if bottom==1 then
        if not flip then
            done=true
            bottom=0
        else
            bottom=100
            flip=false
        end
    end
end

@dave1707 It does almost the exact smame thing as mine. ;:wink: one thing that could be added is every cycle, you can check one less pair because it has “floated” the smallest (in my code, the biggest) to the top. This increases the efficiency of the algorithm especially when it gets near the end.

@Luatee I was thinking the same thing. I might set up a quick example to compare algorithm speeds. Maybe show the same table on the top and bottom of the screen but have them being sorted with different algorithms.

@1980geeksquad Like you, I wrote this because I didn’t have anything else to do at the time. I know it goes from the bottom to the top each time and it doesn’t have to, but it really doesn’t do anything useful so I didn’t see the point to fix it. The whole point in writing it was for the fun of it. That’s the majority of the code I write, it has not real purpose, just something to do. I save everything just in case someone asks a question, then I have an example to show them.

I’ve actually had to do this at work, to fix up someone’s broken hash algorithm, I tried quicksort and heapsort, and settled on an intelligent quicksort. But the heapsort was almost as good, and was a lot shorter to code. I spent an enjoyable week on this, including the time it took to convince everyone that it made sense!

I haven’t tried to code it in Lua, because Lua already knows how to sort!

@dave1707 - yes, but I’m ready for a new kind of fun. My current project is to make a map of the sky. The stars are easy, but the planets are a lot more work. This is only possible because now we have high-precision arithmetic in Codea - yesss!

@Ceres I made a few programs back in the 80’s for an Apple II that showed the planets. One show the planets in orbit and the other against the zodiac. I converted the one for orbits to run in Codea. See the link below.

http://codea.io/talk/discussion/1841/solar-system-simulation-1980/p1

@dave1707 Wow, those two articles really are “fascinating,” both the description of the method and also how you got it to run on your machine. I did a similar thing a few years later, but on a machine from PCs Limited. That was probably a lot easier to work with. I thought I’d sell it, but no, that didn’t pan out at all. It’s still out there, if you’re curious, at http://www.starsend.net/skymap.htm . The last version of Windows it runs on is XP, which just fossilized.

I’m working on an Code version of this, much simplified and using more modern data and hopefully better coding style. I doubt it will good enough for an official app, but it’s fun.

Do you remember how you figured out how to do the calculations? I had a book by Jean Meeus that had almost everything I wanted.

@Ceres I have several books on orbits, and also a book by Jean Meeus, Astronomical Algorithms. I got those long after I wrote those 2 programs. The book that got me started was Fundamentals of Astrodynamics that I got in the mid 70’s. That book was way over my head, but I managed to get enough formulas out of it to use when I wrote the 2 programs. I took a quick look at your site. It looks interesting and I’ll spend more time in it later today.

I had a copy of Fundamentals of Astrodynamics, but can’t find it. Maybe I gave up on it. So you were clever enough to get something out of it! I also have Meeus’s earlier Astronomical Formulae for Calculators, which I used for the first few versions of SkyMap. Then I got his Astronomical Algorithms and started incorporating the new things there, but it uses a different standard epoch and I never quite reconciled everything. The discrepancies are relatively small, but always bothered me. In the Codea app, I’m trying to do better.

By the way, this started out as a discussion on sorting. I used heapsort in skymap, and years later found a small bug in it. It really is hard to get things right, even in a small, well-known algorithm like that one.