Slashdot Mirror


The World of YouTube Bubble Sort Algorithm Dancing

theodp writes In addition to The Ghost of Steve Jobs, The Codecracker, a remix of 'The Nutcracker' performed by Silicon Valley's all-girl Castilleja School during Computer Science Education Week earlier this month featured a Bubble Sort Dance. Bubble Sort dancing, it turns out, is more popular than one might imagine. Search YouTube, for example, and you'll find students from the University of Rochester to Osmania University dancing to sort algorithms. Are you a fan of Hungarian folk-dancing? Well there's a very professionally-done Bubble Sort Dance for you! Indeed, well-meaning CS teachers are pushing kids to Bubble Sort Dance to hits like Beauty and a Beat, Roar, Gentleman, Heartbeat, Under the Sea, as well as other music.

68 comments

  1. Re:Bogus algorithm by laurencetux · · Score: 1

    i guess its similar to the way logarithms are taught where you spend like 3 days learning how to deal with random bases when only 2 are normally actually used.

    and Bubble Sort actually has a defined endpoint unlike say Random Sort

  2. Call me conervative, but by scorp1us · · Score: 1

    I don't think we should be teaching our kids exponential running time O(n^2) algorithms. Randomized partitioned merge sort theta(n lg n). Sure, bubble sorts seem harmless today, it leads to criminal token rings.

    --
    Slashdot's rate-of-post filter: Preventing you from posting too many great ideas at once.
    1. Re:Call me conervative, but by Anonymous Coward · · Score: 0

      This must be some new definition of exponential that I've never heard before.

    2. Re:Call me conervative, but by stoploss · · Score: 4, Informative

      I don't think we should be teaching our kids exponential running time O(n^2) algorithms.

      Call me liberal, but I don't think we should be teaching our kids improper definitions for "exponential" *or* myths that O(n^2) algorithms like bubble sort are bad.

      Quick: which is going to be faster to sort a list of 4 items, bubble sort or randomized partition merge sort? What's that you say? Proper algorithm selection requires more than knee-jerk application of platitudes? Exactly.

    3. Re:Call me conervative, but by Anonymous Coward · · Score: 3, Insightful

      Believe it or not, but bubble sort really is used in the real world, and not because of incompetence.

      Now, maybe it isn't used for most cases, but it is far from being worthless. There are situations where it's really the most optimal algorithm to run, mostly where you are both space constrained (so can't afford the extra memory to allocate more than the list to be sorted), and when you want a low number of instructions to run (because there is no other algorithm which can be performed with as few instructions as you have with bubble sort, and no, abacus sort doesn't count, as you're essentially implementing a specialized piece of hardware to do the sorting for you), but won't use a large data set in the mean time (let's say, less than 100 items, if I recall correctly, but it might be something like 10 or so, which, after that point, the extra instruction overhead in the other algorithms doesn't matter). For instance, one useful place for bubble sorts is in some drivers which need to sort integers, but which don't have a lot to sort, and which using a more complex algoritm would impair their runtime speed. With the right data set, bubble sort ends up being the most optimal algorithm for them to use, ironically.

      You just forget that when we say an algorithm is O(n^2) or whatever, that we're discarding insignificant terms which don't contribute to its long term growth, because most of the time, those terms don't really matter. We don't teach any algorithms in computer science classes that are worthless to know. You just need to know when it's appropriate to use what and when, and by simply discarding bubble sort, you're kind of showing that you don't really know that, and are cargo cult programming here.

    4. Re:Call me conervative, but by plopez · · Score: 1

      I would just skip the sort and do a linear search. As a matter of fact I *am* that lazy.

      --
      putting the 'B' in LGBTQ+
    5. Re:Call me conervative, but by Anonymous Coward · · Score: 1

      Working with these algorithms exercises the brain. The benefit is not just having the knowledge, but having the mental ability to understand a complex novel problem and implement an optimal solution to it.

      I have interviewed many (and worked with a few) self-taught programmers who skipped all that "already solved" stuff and usually just hit the Internet to find ready-made solutions to their problems. There were many simple things they could do well. But once in a while a problem required the building of a sophisticated architecture, and these people were completely incapable of making any progress. The ones who plowed right in and came up with something awesome were the ones with formal Computer Science education.

      Just my experience, there are exceptions to every rule, etc.

       

    6. Re: Call me conervative, but by Anonymous Coward · · Score: 0

      Yikes. You're kidding, right?

      Insertion sort has all the benefits you mention, plus the advantage of being very easy to prove correct. This is important in reality as well as in academia.

    7. Re:Call me conervative, but by dottrap · · Score: 2

      You are correct. But there is a much more direct answer to defend Bubble Sort.

      In the real world, i.e. on real hardware, bubble sort usually faster than other algorithms for small data sets. This is due to cache locality. A cache miss can mean the difference between 4 clock cycles vs. over 400 cycles, simply waiting for 4 little bytes to be read from RAM.

      Cache misses are now the biggest problem for high performance programming. For instance, (good) video game programmers are very aware of this fact.

    8. Re: Call me conervative, but by TheRaven64 · · Score: 1

      Insertion sort is terrible for the use cases the grandparent described. For one thing, it requires allocating a new data structure for storing the data (an immediate disqualification for a lot of embedded tasks). Second, it has much worse cache interaction because it requires searching the second array. Assuming that your target is an array, then it also requires a bit memcpy for each insert, which means that it likely requires a similar number of memory operations to the bubblesort, but with more temporaries. You can do a bubblesort in-place, with good cache locality, and only a handful of registers (insert base, top, current, and two for holding the current elements). If your CPU has 8 GPRs then the space requirements of a bubblesort are effectively zero - no memory required.

      --
      I am TheRaven on Soylent News
    9. Re:Call me conervative, but by Anonymous Coward · · Score: 0

      Which just proves your parent poster's argument though.

      Linear search is even worse on huge datasets than bubble-sort for the purpose of sorting, but is simple to implement, and in practice, works fast enough on most smaller datasets.

      CS education is really bad in that way: It teaches people to over-optimize and optimizing too early, instead of optimizing at the end of a prototyping- or an early agile/scrum stage.

      It is too bad in a way, because when CS students meets the "real world", they often get sorely disappointed and may start missing real opportunities, requirements and deadlines.

      Many very bright people still believe programs _ideally_ should be engineered like it's the 60s, while the real-world applications trend to the opposite direction of more iterations and agile/lean-type methodologies.

      The difference between building a bridge and a computer program?
      The bridge is expected to stand still in a static non-changing state for as long as possible.
      The computer program is expected to be finished yesterday and to accommodate requirements asked for tomorrow.

      I've yet to see this reality really sink into most practicioners, though this is in practice the reality they have to deal with.

    10. Re: Call me conervative, but by Half-pint+HAL · · Score: 1

      Actually, insertion sort can be done with a single array structure. When you insert a value into the new list, you delete it from the old list. So the total len(old) + len(new) = len(original). You can use a single int to identify the partition between the segment of the array that represents the new list and the segment that represents the new, sorted list, and effectively do sort-in-place. Hell, it's not even an extra variable, as you would need that index to keep track of the next element to add to the new list anyway.

      --
      Got them moderator blues I blieve I walk out the do', With these mod-points I been gettin', I 'most never post no mo'
    11. Re:Call me conervative, but by rasmusbr · · Score: 2

      FYI O(n^2) is called quadratic complexity/time, O(n^3) is cubic, O(n^1) is linear and O(n^0) = O(1) is constant.

      Exponential complexity would be O(c^n), where c is a constant.

    12. Re:Call me conervative, but by Anonymous Coward · · Score: 0

      +1 insightful, but with the caveat that sometimes knowledge of "the old ways" is often helpful particularly when there are unusual constraints (so you can't just throw more hardware at it).

      I once had to write a program to do essentially a bunch of point-in-polygon comparisons, with one file containing the points and the other containing the (many, many) polygons. Plus, the boss said "I don't want any fancy algorithms, just get it coded". Fair enough. Coded, ran (and ran, and ran) on sample data. I extrapolated to the full data set:
          "Boss, it's going to take about forty days run time to complete the whole analysis".
          "Uh, see what you can do to speed it up."
          Now, I could have spent a lot of time recoding with something like quadtrees or figuring out how to partition it up to be more parallel, but what I ended up doing is putting the two inputs and the output file on three different disk drives -- seek time had been dominating and I got the job down to complete in about 30 hours.

    13. Re: Call me conervative, but by Anonymous Coward · · Score: 0

      Wrong. Insertion sort is normally implemented in-place - and as such it is fine for a driver needing to sort a few items. Insertion sort *can* be done using two arrays - but why throw in such an abominable complication? The simple in-place insertion sort is what we usually mean when talking about insertion sort.

  3. Re:Bogus algorithm by The+New+Guy+2.0 · · Score: 4, Interesting

    A key part of all database systems is the fact that you can ask for a sort order without having to write a sort program. While simple sorts can move quickly, a bubble sort can move even faster. When you're dealing with a multimillion record table, this saves minutes and power per query.

    Everybody develops Bubble Sort the same way, proving it's an eventual discovery that no longer qualifies for patents. Teaching it is a basic way of showing the programming language's loop terms and variable scopes, so it's an elementary program to write.

    I guess this dance is reminding us that Bubble Sort can be applied to dating. If the girls rank themselves correctly, it takes a bunch of "go over there..." dates in order to get it right.

  4. Re:Bogus algorithm by The+New+Guy+2.0 · · Score: 1

    Yep, Random Sort is subject to bad draws of randomness that cause it to randomly walk to a longer solution. A combo technique of random sort somewhat, then bubble sort is often effective.

  5. Re:Bogus algorithm by Anonymous Coward · · Score: 1

    While I don't disagree with your overall point, if it's not obvious to a CS student why a bubble sort works after a moment of thought, they're in the wrong field.

  6. Re:Bogus algorithm by Ken_g6 · · Score: 3, Insightful

    I agree on Bubb..I mean, BS. But Selection Sort is really only useful with big objects that you don't want to move much. These days everything's a Reference, so it doesn't matter so much. It makes for a really boring dance, too.

    Insertion Sort is more useful in modern use cases. If something's "almost sorted" it's very quick.

    Shell sort might be even better. It's practically identical to Insertion Sort except only subsets of dancers would step out at one time. And, with a good gap sequence, it gets done much quicker than either of the above.

    --
    (T>t && O(n)--) == sqrt(666)
  7. Re:Bogus algorithm by lucm · · Score: 1

    When you're dealing with a multimillion record table, this saves minutes and power per query.

    What database are you using? Sqlite? OpenOffice Base?

    --
    lucm, indeed.
  8. Re:Bogus algorithm by zippthorne · · Score: 3, Insightful

    Hang on..

    Are you seriously suggesting that bubble sort is useful for N in the millions?

    --
    Can you be Even More Awesome?!
  9. Not bad, but... by Len · · Score: 1

    I'll be impressed when they dance a quicksort to Flight of the Bumblebee.

    1. Re:Not bad, but... by david_bonn · · Score: 1

      I'll be impressed when they dance a quicksort to Flight of the Bumblebee.

      Or a merge sort to Aaron Copland's "Fanfare of the Common Man".

  10. Re:Boring by Ol+Olsoc · · Score: 2

    They didn't even have a pole. Just what are they teaching women about how to make money these days?

    I expect to be downmodded to the lowest level of turtles, but I think it is the idea of since today, dancing is quite popular, and that if they can get young girls to think that programmers dance all day, they might decide to become programmers.

    I mean Beyonce is a programmer right?

    --
    The shepherds did so well protecting the flock that the sheep no longer believed that wolves existed.
  11. More useful as a dance... by Tony+Isaac · · Score: 3, Informative

    ...than as a sorting algorithm!

  12. Re:Bogus algorithm by plopez · · Score: 1

    But it's web scale! ;)

    --
    putting the 'B' in LGBTQ+
  13. Re:Bogus algorithm by plopez · · Score: 1

    Shell sort was always my favorite in terms of elegance, IMO

    --
    putting the 'B' in LGBTQ+
  14. Re:Bogus algorithm by xvan · · Score: 3, Interesting

    And more important than average efficiency, shell sort makes better dances!

  15. Re:Bogus algorithm by Anonymous Coward · · Score: 1

    I think The New Guy confused Bubble Sort with Quicksort. When you have been out of academia for a while, it is easy to get the names mixed up.

    In the modern server computing environment, it is extremely rare that anyone needs to choose a specific sort algorithm. It is far more important to know when and what to sort than it is to tweak the algorithm's details.

  16. Re:Bogus algorithm by beelsebob · · Score: 4, Insightful

    It's usually mentioned in CS courses because the first stage in introducing these classes is "think about sorting some numbers - how do you go about doing it", and generally Bubble Sort is the first formalisation that falls out of that. The fact that Selection Sort is the one that you think of is neither here nor there - most students come up with something looking like bubble sort.

  17. Re:Bogus algorithm by beelsebob · · Score: 3, Interesting

    No one in their right mind would implement quick sort to sort millions of database entries either though. They'd likely implement something like merge sort.

  18. Re: Bogus algorithm by Anonymous Coward · · Score: 0

    A _proof_ that bubble sort works is actually pretty hard to construct.

    There is absolutely no reason to teach bubble sort. Insertion sort should be the first one to be taught.

  19. Re:Bogus algorithm by davester666 · · Score: 4, Insightful

    It entirely depends on the order of data in the database. If you know the data is already mostly sorted, then it can perform much better than other methods.

    --
    Sleep your way to a whiter smile...date a dentist!
  20. Re:Bogus algorithm by linuxrocks123 · · Score: 2

    Umm ... (+1, Funny) ... I hope?

    Neither of those sorting algorithms is particularly good, but random sort (bogosort) is hilariously bad.

    --
    vi ~/.emacs # I'm probably going to Hell for this.
  21. Re:Bogus algorithm by linuxrocks123 · · Score: 2

    Well, the problem with QuickSort is that if you're unlucky with your pivot choice you can get O(n^2) runtime. The problem with MergeSort is that you have to copy the array, which, for n in the millions, could be an issue.

    For such a case I would recommend heapsort or introsort (which is just "quicksort unless I'm getting unlucky, then I do heapsort instead").

    HeapSort doesn't get enough love :(

    --
    vi ~/.emacs # I'm probably going to Hell for this.
  22. Re:Bogus algorithm by Anonymous Coward · · Score: 0

    I wish score +1 Awesome existed. Ah well, +1 insightful will hove to do.

  23. Re:Bogus algorithm by Anonymous Coward · · Score: 0

    I disagree. The simplest idea that comes naturally and logically is to first find the smallest number and put it first, then find the next larger number and put it after, and repeat. Swapping adjacent items over, and over, and over again, is a much more systematic and "invented" approach, and not at all a natural way of going about it.

  24. Re:Bogus algorithm by Anonymous Coward · · Score: 0

    Or 3...

    (10, 2, e)

  25. Re:Bogus algorithm by InfiniteLoopCounter · · Score: 1

    Hang on..

    Are you seriously suggesting that bubble sort is useful for N in the millions?

    The GP must be the database guy they hired for the Russian stock market.

  26. Re:Bogus algorithm by TheRaven64 · · Score: 1

    Bubblesort has two advantages. The first is that, because it's only swapping adjacent elements, it has very good locality of reference (which means better cache usage, but can also mean more amenable to fine-grained locking). The second is that it performs well on almost-sorted data (that O(n^2) is the worst case - it's closer to O(n) if your inputs are mostly sorted). These two mean that there are situations where bubblesort can be useful, though they're quite rare.

    --
    I am TheRaven on Soylent News
  27. Re:Bogus algorithm by TheRaven64 · · Score: 2
    Insertion sort is one of these good-on-paper algorithms. It's very fast if insertion is cheap. But insertion relies copying unless your data structure is a linked list. If it's an array (worse, a contiguous on-disk store) then that copying can be very expensive. If it is a linked list, then you're going to have very expensive search (sure, you may still be O(log(n)), but that constant multiple is going to be hurt by the fact that you're hammering your cache and killing your branch predictor).

    Teaching algorithms separately from data structures is one of the biggest flaws in modern computer science education. It's impossible to reason sensibly about one without the other.

    --
    I am TheRaven on Soylent News
  28. Re:Bogus algorithm by TheRaven64 · · Score: 1

    That's only if you don't have the requirement to sort in place. Bubblesort is also a good way of getting students to start thinking in terms of induction. Each step in the bubblesort leaves the array more sorted than it was before.

    --
    I am TheRaven on Soylent News
  29. Re:Bogus algorithm by Half-pint+HAL · · Score: 3, Interesting

    As others have said, bubble sort is efficient on mostly-sorted datasets. Bubbling sorting your database after every X insertions (for some value of X) or before a search (whichever comes first) makes the world a nicer place.

    --
    Got them moderator blues I blieve I walk out the do', With these mod-points I been gettin', I 'most never post no mo'
  30. Re:Bogus algorithm by Anonymous Coward · · Score: 1

    I don't understand why Bubble-Sort is even mentioned in CS courses. It is not immediately apparent to new students why repeatedly swapping adjacent items in this manner eventually results in a sorted list. With Selection-Sort on the other hand it is immediately clear why it works, and it is also faster than Bubble-Sort, and appropriate to use in simpler cases.

    Bubble-Sort is a bogus algorithm and should not be mentioned nor taught to new students, and henceforth it should be shortened and known as BS, because that's exactly what it is.

    The bubble sort algorithm is the easiest sorting method for most beginning computer science and programming students to comprehend. Next you will be suggesting they be taught the radix sort algorithm as their first assignment. It is the same nonsense I hear from supposedly experienced programmers when they claim BASIC is a terrible introductory language to teach basic programming concepts. The personal computer industry and much of modern software has been developed by hobbyist programmers who taught themselves BASIC on their 8-bit computers. The trash that is modern-day programming in the form of "design patterns", "unit tests", "frameworks" obsessed hipsters and their clueless managers is a bane on the profession.

  31. Re:Bogus algorithm by Anonymous Coward · · Score: 0

    I remember coding these sorting algorithms in 6502 assembly language on a Commodore VIC-20 in the early 1980s. Those were the good years.

  32. Re: Bogus algorithm by Anonymous Coward · · Score: 0

    A _proof_ that bubble sort works is actually pretty hard to construct.

    There is absolutely no reason to teach bubble sort. Insertion sort should be the first one to be taught.

    Insertion sort might be misconstrued as sexual harassment by the radical feminist computer science students and faculty.

    CAPTCHA: disgusts

  33. Re:Bogus algorithm by Anonymous Coward · · Score: 1

    Mostly I just set up a weekly job to ALTER INDEX {index_name} ON {table_name} REBUILD. Overnight between Saturday and Sunday is usually a good time to do this.

    How the DBMS gets that job done is Not My Problem(tm), just as long as it works.

  34. Re:Bogus algorithm by Half-pint+HAL · · Score: 1

    (Obviously I mean updates, not insertions.)

    --
    Got them moderator blues I blieve I walk out the do', With these mod-points I been gettin', I 'most never post no mo'
  35. Re:Bogus algorithm by Half-pint+HAL · · Score: 1

    The best solution is always dependent on the task and the dataset.

    --
    Got them moderator blues I blieve I walk out the do', With these mod-points I been gettin', I 'most never post no mo'
  36. Re:Bogus algorithm by OakDragon · · Score: 1

    ...With Selection-Sort on the other hand it is immediately clear why it works, and it is also faster than Bubble-Sort, and appropriate to use in simpler cases.

    Sure, but can the kids dance to it?

  37. If data is mostly sorted why not insertion sort? by Sits · · Score: 1

    If I believe the data is going to already be mostly sorted why wouldn't I just use insertion sort?

  38. Re:Bogus algorithm by Anonymous Coward · · Score: 0

    Bubble sort (and similiars like insertion sort) is ok for starting an introductory course. Next lesson, you tell them you have something remarkably better, and show them the nlogn sorting algorithms. And how they are awesomely faster for sorting a few million items.

    As for basic, it is a waste-of-time introductory language. Those who made it after starting with basic, made it because they managed to unlearn basic and move on to something better. (I am guilty of going that way - even 6510 assembly were more useful than basic.) Talented programmers don't need basic - other than to get them interested in computers in the first place. But if your'e going to teach, just skip basic. "The best language" is another debate, but surely a more typed language, with actual compilers would be useful . . .

    Basic is only for those who aren't going to go beyond basic anyway. If their ambition merely is to code word macros - basic is for them. Can't call them programmers though...

  39. Re:Bogus algorithm by Carewolf · · Score: 1

    It's usually mentioned in CS courses because the first stage in introducing these classes is "think about sorting some numbers - how do you go about doing it", and generally Bubble Sort is the first formalisation that falls out of that. The fact that Selection Sort is the one that you think of is neither here nor there - most students come up with something looking like bubble sort.

    Most people get to insertion sort or bucket sort first, since those are the ones that arise naturally from sorting playing cards in respectively your hand or the whole deck on the table.

  40. Re:Bogus algorithm by The+New+Guy+2.0 · · Score: 1

    MS Access.

    It's not always the best program for databases, but it has the easiest to use UI. Visual Basic 6 and VBA are almost interchangeable.

  41. Re:Bogus algorithm by The+New+Guy+2.0 · · Score: 1

    This is a great dilemma that database engines deal with. With all of the sorting programs possible, which is the one that will solve it the fastest? There's not much of a chance for figuring it out on the first pass... you'd have to sort the database using all the sorts to get times, and that's wasted work. Database engines have some logic to say which they should guess is right the first time, and then if the query is asked for again it can rely on cache or place only the new records into the list.

    This is why a database programmer should recalc his test data frequently, it's going to help the live system know where to start.

  42. Excellent visual explanation for non-geeks by Whiteox · · Score: 1

    I sent the Hungarian dance link to my Magyar uncle who is somewhat computer illiterate. This'll show him how binary can solve simple order.

    --
    Don't be apathetic. Procrastinate!
  43. Re:Boring by Whiteox · · Score: 1

    OTOH Why not make a dance to visualize code?

    --
    Don't be apathetic. Procrastinate!
  44. Re:Boring by Anonymous Coward · · Score: 0

    They did, it's called the 'Fish-Slapping Dance.'

  45. Re:Boring by Anonymous Coward · · Score: 0

    Hmmm... Can you code that for me?

  46. Re:Bogus algorithm by QQBoss · · Score: 1

    I teach both C and Data Structures. Bubble sort is for my C class where I am trying to make sure the students fully comprehend arrays (most of my students come from a non-existent programming background, and the school isn't sold on my teaching them Python as a first language just yet as apparently I am the only instructor who can use it meaningfully), how indexes work (getting them to reverse an array or a string doesn't quite seem to do it for about half of them), and my better students will have implemented bubble sort on linked lists by the end of the semester, as well. Understanding that bubble sort works isn't a problem for them, but they are only starting to think beyond what a single loop can do algorithmically. I have tried jumping to insertion sort and as a whole there is poor integration of the knowledge to take forward. Good/better/best options just can't happen for most of these students at this point, and so it has to wait for Data Structures where the first sort they learn is Insertion Sort and I don't care which language they use. I guess if someone started using Mindfuck or Ada I might start to care. They add every sort to one big program where they can calculate how much work each sort does and how much system time it takes to operate for randomly generated lists.

    As for concerning themselves with cache misses as one person suggested it, until students have a better understanding of systems, that can't be readily done with a class of 40+ frequently unmotivated students (it can with ~10 motivated students, the breakpoint occurring somewhere in the middle, of course), though I start introducing the penalties of register/cache/RAM/disk in this class so that by the time I am teaching embedded systems the students who chose that path usually have a good grasp of it, with the best ones looking even at divide instructions with disdain. With the semester system being what it is, though, teaching all of that as a simultaneous, cohesive chunk of information just isn't going to happen.

  47. Re:Boring by Anonymous Coward · · Score: 0


    while (music_playing())
    {
          dancer1.slap(dancer2, leftCheek, smallFish);
          dancer1.slap(dancer2, rightCheek, smallFish);
          dancer1.slap(dancer2, bothCheeks, smallFish);
    }
    dancer2.slap(dancer1, head, bigFish);