r/programming • • Dec 02 '19

Bubble sort visualization

7.4k Upvotes

268 comments sorted by

724

u/IdiotCharizard Dec 02 '19

good implementations of bubblesort won't do the extra comparisons after the n-kth index (n elements, kth iteration). Also, it can be very fast to check if the list is sorted rather than possibly wasting a few useless iterations

117

u/pedrovhb Dec 02 '19

Perhaps the title should be "Naive bubble sort visualization" (:

36

u/hylet Dec 03 '19

more like "paranoid bubble sort", keeps checking the last elements even though they are sorted

6

u/[deleted] Dec 03 '19

I have my bubble sort algorithm run continuously in the background. Just in case.

15

u/[deleted] Dec 03 '19

[removed] — view removed comment

3

u/SmokeyDBear Dec 03 '19

bubblesortandhash

659

u/[deleted] Dec 02 '19

good implementations of bubblesort

Say what now?

211

u/[deleted] Dec 03 '19

Algos like bubblesort can be preferable on small data sets as opposed to other "better" algos.

118

u/[deleted] Dec 03 '19

Or of you are on tiny micro and do not care about time but code size

69

u/RICHUNCLEPENNYBAGS Dec 03 '19

In that case isn't shellsort the best choice? Like Sedgewick's book doesn't even bother presenting bubble sort

49

u/[deleted] Dec 03 '19

I'm talking about "your micro have amount of flash in single digit kilobytes" cases so every byte counts. Even then I'd say it is still "try it after you optimized everything else" kind of problem

28

u/RICHUNCLEPENNYBAGS Dec 03 '19

I am not an embedded developer but this might be of interest. This guy is pretty down on bubble sort even in embedded scenarios and suggests a shellsort is usually the best choice. https://embeddedgurus.com/stack-overflow/2009/03/sorting-in-embedded-systems/

20

u/[deleted] Dec 03 '19

Sounds like a job for bogosort!

9

u/cbarrick Dec 03 '19 edited Dec 03 '19

It is true that the quadratic sorts can be better than the linearithmic sorts for small datasets. But insertion sort is almost always the best of the quadratic sorts.

Edit: I should add that the bidirectional bubble sort can be as good as insertion sort for random arrays, but insertion sort is an adaptive sort so it's still better for real world data.

38

u/Tyler_Zoro Dec 03 '19

Also, it's the most efficient algorithm on pre-sorted data and gets less efficient slowly, so if you think your data is mostly sorted, bubble sort can be the best choice.

Of course it will become the worst option quickly thereafter, not counting shuffle sort.

29

u/SirClueless Dec 03 '19

Isn't Insertion Sort strictly better on near-sorted data?

17

u/Tyler_Zoro Dec 03 '19

They're the same for trivial data sets (assuming you always test for last-pass in bubble sort), but yes, for non-trivial cases, IS is better.

9

u/lpreams Dec 03 '19

So is there any case in which bubble is better than insertion?

6

u/Tyler_Zoro Dec 03 '19

I don't think so. Bubble just ends up being insertion for trivial cases.

4

u/G_Morgan Dec 03 '19

Yeah and there are many scenarios that might look like that. For instance tacking onto a previously sorted list.

→ More replies (3)

10

u/bubblesort Dec 03 '19

Whadda ya mean, 'better'? Them's fightin words! Who says they better than me? I'll give em a poke in the eye!

5

u/StockAL3Xj Dec 03 '19

Are we really shortening algorithm to "algo" now?

2

u/FlatPlate Dec 03 '19

Weren't there a quote from someone important that said, no matter what you're doing you shouldn't use bubble sort?

3

u/[deleted] Dec 03 '19

Try saying that in an interview and see how it goes..

20

u/Ewcrsf Dec 03 '19

It would go well unless the interviewer is utterly incompetent.

9

u/ivosaurus Dec 03 '19

It should go poorly because you should be using insertion sort 100% of the time you could ever want to use bubble sort.

→ More replies (3)

1

u/doublehyphen Dec 04 '19

Insertion sort is virtually always faster than bubble sort. It is also more intuitive.

12

u/jarfil Dec 03 '19 edited Dec 02 '23

CENSORED

87

u/schplat Dec 02 '19

yup, you can "lock down" the last element after the first path, because it will assuredly be the correct spot after all passes.

Also, if the last n elements do not switch spots on given pass, you can "lock down" all of those spots. i.e.:

4,1,2,3,5 <1st sort pass> 1,2,3,4,5* <2nd sort pass> 1*,2*,3*,4*,5*

With *'s showing which iteration they get locked So you only do 2 passes on the above instead of 1 pass for each element. This is why bubble sorting can have a variable performance factors on different data. It depends on how out of order the data is initially.

4

u/debug_assert Dec 03 '19

I’m not sure you’re right. You can imagine a large number at the head of the list. It’ll take many iterations for it to bubble to the big-end side, possibly many iterations where the last n elements don’t change. If you “locked” them you’d have an incorrect sort.

6

u/blue_umpire Dec 03 '19

If you watch the gif in the post, and look at the 6. It moves as far right as it can in the first pass. If it were >= 9 it'd end up on the right at the end of the first pass.

3

u/root88 Dec 03 '19

What? After the first pass, the values are 4, 2, 0, 6, 5, 7, 9. The 6 moves from the 4th spot to the 5th spot on the second pass.

12

u/SirClueless Dec 03 '19

Yes, you're right. The logic only holds for the largest unsorted number, which in this list is 9 and happens to already start in place making it not a great illustration of this point. Every other number can get "stuck" on a larger number, as 6 does in the first pass in this video.

4

u/LoLlYdE Dec 03 '19

And that is correct? I fail to see your problem here. If you keep watching the gif after that, the 7 (9 can be ignored because its already in the correct spot) doesnt move again after the first pass, same for the 6 after the second pass etc

0

u/[deleted] Dec 03 '19

[deleted]

31

u/debug_assert Dec 03 '19

Well shit.

20

u/Dworgi Dec 03 '19

Isn't that the bubble part of the name? Biggest value floats to the top and stays there. That's how I always thought about it.

5

u/Log2 Dec 03 '19

It is. If you take a reversed sorted list, then you have bubble sort worst case: each iteration will place the next largest number in the correct spot, thus having O(n^2).

→ More replies (6)

1

u/Katanamatata Dec 03 '19

How exactly are the values being "locked down"? Are the indexes being added to an array if they are suspected to be in order?

10

u/norwegian Dec 03 '19

How exactly are the values being "locked down"? Are the indexes being added to an array if they are suspected to be in order?

Think you would need just one index for the lower bound, and one index for the upper bound. Two in total.

3

u/Katanamatata Dec 03 '19

Oh that makes sense! Thank you.

2

u/irqlnotdispatchlevel Dec 03 '19

I feel like this talk by Andrei Alexandrescu is relevant here.

-7

u/dzamlo Dec 02 '19

A good implementation of bubblesort is an implementation of another algorithme. Bubblesort is a very bad algo no matter the implementation.

114

u/Free_Math_Tutoring Dec 02 '19

Yes, everybody knows that bubblesort is never the best option. Thank you for contributing. Discussing specific optimizations that can be applied frequently is nonetheless interesting.

9

u/Adventurer32 Dec 02 '19

what is the best option?

53

u/Gangsir Dec 02 '19 edited Dec 02 '19

The best option depends on the data you're sorting. Is it large, or small? Will it vary in size? Mostly sorted, or completely random? Will it ever start out already sorted? How fast is it to compare two entries (do you have to check several sub-fields to say a[1] should be in front of a[3])? Does it need to be deterministically sorted (two values that evaluate equal will always end up in the same order, not be switched every time you sort)?

Etc. Choosing the right algo is hard sometimes, especially the more "chaotic" your data is (varies in size, sometimes sorted, sometimes random, takes forever to compare values, etc). On top of that you have to consider different constraints: Does the sort need to be fast? (Yes you could sort 1000000 entries with a bubblesort, but it'd take forever) Does the sort need to use little memory, or is the sort the only thing the computer is doing (so it's okay if it hogs memory)? How complex can the sort be, how much dev time do you want to put into it?

Bubblesort is never the best because no matter what your data is, there's always a better algo than bubble. Even bubble's perfect condition (data is already sorted, or only the last two values are out of order) can be sorted just as well or better with a different algo.

31

u/d4harp Dec 03 '19

Even bubble's perfect condition (data is already sorted, or only the last two values are out of order) can be sorted just as well or better with a different algo.

Bubblesort on pre-sorted data would have a time complexity of O(n), and space complexity of O(1). Isn't that the maximum possible efficiency? The only algorithm I know of with an equal best case is insertion sort.

(This is a question, not a correction. Sorry if I got this completely wrong)

21

u/Gangsir Dec 03 '19

Nah, you're right. That's why I said "equal or better algorithm". To my understanding, insertion overall would be better still, because as soon as you mess with that optimal scenario (un-sort the data a bit) bubble becomes slower.

If you had a data set that you could be assured would always come pre-sorted, then there'd be no difference between bubble and insertion. (You also wouldn't need a sort algo in the first place, but that's beside the point)

If bubble is the best algo, it's only because it's equal with another algo. It'll never be a lone upgrade.

→ More replies (2)

2

u/Willingo Dec 03 '19

You seem smart. How stupid would it be if I tested several algo run times on a sample of data and choose the one with the best run time?

16

u/Gangsir Dec 03 '19

You seem smart.

Aw, thanks.

if I tested several algo run times on a sample of data and choose the one with the best run time?

That'd be fine so long as time is your only concern, and further datasets you sort follow the same pattern as your control dataset. If you run all those algorithms against that qualifier and one turns out fastest, and you're only looking for speed, sure, drop that one in your codebase. This is fine especially if the code you're writing is throwaway and won't be used by tons of people (like making a little app for yourself).

However, often time isn't the only concern when you're implementing a sort at a job or whatever, they'll usually give you extra requirements like "must be able to handle data sizes of X to Y, data will always be randomly sorted, and sorting must take no longer than <some degree of big O notation> and consume no more than Z amount of memory at maximum size, but sorting doesn't need to be deterministic".

You'd find one that works at a decent enough speed and doesn't hog memory or take too long to develop (some sorts are quite lengthy to implement in some languages), and away you go. You can even drop the "dev time" qualifier if you find a library for the sort in your lang, it just falls back to "is the lib's implementation fast enough and memory-efficient enough for the data we're gonna be sorting?".

→ More replies (4)
→ More replies (6)

7

u/IdiotCharizard Dec 03 '19

whichever one is in the stdlib of the language you're using.

6

u/itmustbemitch Dec 03 '19

To give a more concrete answer which is less philosophically true than the other poster but perhaps more immediately useful, in very general cases, I believe the most common best sort is called quick sort.

The idea of it is that you pick a number, called a pivot iirc, in your list that you hope belongs close to the middle, then shove everything larger than the pivot to its right side and everything smaller than the pivot to its left side. Then you repeat the process on the left-side and right-side sub-lists, with a new pivot for each sub-list, until the whole thing is sorted.

There are some problems with this sort (the biggest being that it becomes extremely bad in the case that you pick pivots in an extremely dumb or unlucky way), and there are some optimizations I'm leaving out here, but in terms of average case performance it's generally the strongest choice

4

u/bubblesort Dec 03 '19

The best option is to ignore all the jerks in this thread talking shit about me. Bunch a trolls around here.

6

u/Gawdl3y Dec 03 '19

4

u/Chand_laBing Dec 03 '19

I know tremendous things about sorting bigly crooked data sets. That's why I use bubble sort. Hey Quicksort? Ya fired!

11

u/digitaldreamer Dec 02 '19

I wouldn't say that it's never good. It's fine for sorting small sets. It's simple and easy to implement. There's no need to complicate things if you know you're never going to sort over a certain size.

23

u/chucker23n Dec 02 '19 edited Dec 02 '19

But there’s also no need to implement your own sort at all, especially for small sets.

Bubble sort exists purely for educational purposes. Further optimizations are mildly interesting for the same reason bubble sort is: not for production use, but to explain how stuff works.

16

u/k-selectride Dec 03 '19

If you're in a hard real-time environment and you have data that you want to make progress towards sorting for each tick without storing intermediate state, you probably want something like bubble sort.

8

u/OMGItsCheezWTF Dec 03 '19

Yeah but that's a pretty niche case. In the vast majority of workloads your language, framework or underlying datastore's sort() method is probably good enough.

5

u/k-selectride Dec 03 '19

Yes absolutely.

2

u/warhead71 Dec 03 '19

Lots of old programming languages don’t have a sort to use. They rely on databases or sorted datasets.

16

u/tommcdo Dec 02 '19

It's rare to be in an environment that doesn't have an available sort library.

3

u/Katanamatata Dec 03 '19

I didn't know this but I'm new to algorithms

5

u/corporaterebel Dec 03 '19 edited Dec 03 '19

if you are sorting 2-10 items it has good performance.

(I use it to sort dynamic information while analysts are categorizing leads on data entry forms)

4

u/IdiotCharizard Dec 03 '19

Bubblesort is still good for learning, and while learning, it's still useful to know not to waste cycles.

2

u/bubblesort Dec 03 '19

I'm not bad, I'm just drawn that way.

2

u/[deleted] Dec 03 '19

If I have less than 10 elements, I wouldn't bother implementing anything more complex.

1

u/stevethedev Dec 03 '19

That's probably true but overcomplicates the explanation. This is "foo == bar" stuff.

2

u/IdiotCharizard Dec 03 '19

I think skipping the comparisons beyond the point where you know things are sorted helps people see the idea of bubblesort "bubbling" the largest elements to the top each iteration.

1

u/stevethedev Dec 03 '19

I'm not sure I agree. I think the "don't compare things we know are good" approach, while objectively better in practice, has more complicated code that makes it harder to correlate a video to the code.

The naive-case is stupid-easy to write, and pairs well with this video.

fn sort(array):
  for i = 0 to array.length:
    for j = 0 to array.length:
      if array[i] > array[j]
        (array[i], array[j]) = (array[j], array[i])

1

u/IdiotCharizard Dec 03 '19

All you have to do to this code is add a -i to the j loop limit to get the better behaviour. That is not significant enough complexity that it should be avoided IMO.

→ More replies (2)

-5

u/nikibobi Dec 02 '19

I came here to say this too

16

u/[deleted] Dec 02 '19

good implementations of bubblesort

Seems oxymoronic

2

u/chinpokomon Dec 03 '19

If your vector is already mostly sorted, it isn't necessarily bad. It's a good starting point for a creating a customized sorter just used for updating the order of a presorted vector which had a single value changed and you need to index it to the correct order. For that sort of resort, something like quick sort would be overkill. Keeping track of the upper range and not checking the whole range once a limit is found, that is a common optimization.

1

u/doublehyphen Dec 04 '19

I think insertion sort is just strictly better for that.

56

u/[deleted] Dec 02 '19

Is this in manim? It definitely looks like it

27

u/pedrovhb Dec 02 '19

It is! Source here.

6

u/AustinYQM Dec 03 '19

How hard is that to use. I would love to animate the different tree traversals for my students.

9

u/JoJoModding Dec 02 '19

I see manim, I vote up.

40

u/[deleted] Dec 02 '19

Thank you for this!

(I have a data structures and algorithms final in like 2 weeks send help)

38

u/pedrovhb Dec 02 '19

Glad it's useful!

I'm practicing my Manim, so I'll probably do a couple more easy sorting algorithms before moving on to trees (:

13

u/Vauc2000 Dec 02 '19

I was wondering why it looked so much like 3Blue1Brown

1

u/foofaw Dec 03 '19

Please do!

1

u/Odanie Dec 03 '19

Do it, Pedrão.

1

u/Kaligule Dec 03 '19

Cool, how do you like manim? I found it incredibly hard to get into because of the lack of good documentation.

2

u/unable_to_give_afuck Dec 03 '19

I have my Object Oriented Design Patterns final next week. This was helpful! I hope we get visualizations of more sorting methods.

1

u/uber1337h4xx0r Dec 03 '19

Rip, that's the class that made me want to consider suicide.

I ended up failing it once, dropping it the second time, and barely passing the final time lol.

1

u/[deleted] Dec 03 '19

Good you passed though!

I swear, I’m only not more panicked because my teacher provided us four practice finals on his website. He’s actually such a nice dude - too bad I’m trash at his class’ subject matter lmao

1

u/uber1337h4xx0r Dec 03 '19

It's not impossible at least. If a retarded person like me can pass, then so can you. And even on your first try lol

1

u/[deleted] Dec 03 '19

Don’t call yourself that! But thank you for the encouragement anyway lol

1

u/Alchestbreach_ModAlt Dec 03 '19

Finals in two weeks? Jeez what university do you go to. Ive done finals the first week of december for the last 3 years. I couldn't imagine schoolin it up almost to christmas

4

u/[deleted] Dec 03 '19

It’s my last final and it’s on the 17th. My earliest is in a week (luckily it’s for an easy class).

3

u/Alchestbreach_ModAlt Dec 03 '19

You got this. Just remember 2 things.

  1. Array index start at zero

  2. You can pretty much use any primative data type for switch statements too.

7

u/uber1337h4xx0r Dec 03 '19

Erm... Data structures is so much deadlier than that.

Think big O, time complexity, breadth search red black tree traversal or something like that.

3

u/[deleted] Dec 03 '19

“big O, time complexity”

Nam flashbacks intensify

→ More replies (2)

3

u/caninerosie Dec 03 '19

Array index start at zero

Unless your preferred language is lua, in which case indexes start at 1 for some reason

→ More replies (6)

2

u/unkown-shmook Dec 03 '19

My discrete math teacher is making us take our 3rd exam on the 12th and our final a week after. No in class review at all, half the class already dropped.

1

u/[deleted] Dec 03 '19

RIP. I failed discrete math the first time I took it and passed with a C later in a community college.

2

u/unkown-shmook Dec 03 '19

I’m at a B right now only because he never imputed my first exam so he’s gonna replace it with my final. Got lucky with how bad he is

→ More replies (1)

1

u/Alchestbreach_ModAlt Dec 03 '19

Was discrete structures not an option? Man thats way easier then discrete math.

1

u/unkown-shmook Dec 03 '19

Discrete math is a required class for my major

→ More replies (2)

1

u/bert1589 Dec 03 '19

Trimesters maybe?

2

u/equivalent8 Dec 03 '19

Really nice. Do you have more?

3

u/pedrovhb Dec 03 '19

I'll be making and posting more soon!

298

u/Shaky_Balance Dec 02 '19

89

u/RedditorsAreWeird Dec 02 '19

Ahh... the definitive guide to sorting.

37

u/yanitrix Dec 02 '19

That was the video my teacher showed us when we were talking about sorting in java

16

u/1RedOne Dec 03 '19

I'm waiting for someone to share the ones with colored lines which make noises while sorting, and it shows tons or tons of different ones

38

u/HiImLary Dec 03 '19

i got you fam

Whoever reposts this in 2 hours for the 10,000th time, give me credit.

19

u/1RedOne Dec 03 '19

I love bogosort

while not isInOrder(deck): shuffle(deck)

9

u/TheNiXXeD Dec 03 '19

It clearly makes the best music.

4

u/Cycloneblaze Dec 03 '19

After five minutes of the crescendos of other sorts, bogosort is incredibly relaxing

4

u/not_the_world Dec 03 '19

I love that it sounds exactly like bogo sort, kinda like a dial-up modem that was dropped on its head as a baby.

6

u/FiveOhFive91 Dec 03 '19

God I love this video.

4

u/b2a1c3d4 Dec 03 '19

Can anyone tell me wtf is going on with bitonic sort?

3

u/mccoyn Dec 03 '19

Merging a reverse-sorted array with a forward-sorted array is very slightly faster than merging two forward-sorted arrays due to cache locality. Bitonic uses n storage slots mapped to n-element array, but it doesn't care what the mapping is. Alternating from reverse to forward sorting happens to be the fastest mapping.

2

u/thirdegree Dec 03 '19

Bitonic is my favorite. No idea how it works, it's just like "ok so we're gonna make some mountains, clean it up a little, need a few valleys... And now it's sorted!"

58

u/[deleted] Dec 02 '19 edited Dec 02 '19

Why does it do a final scan after the penultimate one where no swaps were made? Surely we know we're done at that point.

Edit: looking again, it starts with a swap. Guessing that's why.

28

u/crimson1206 Dec 02 '19

Yeah your edit is correct. It only stops when there was no swap over one full iteration.

6

u/mattsoave Dec 03 '19

But you could safely stop after a pass where only the very first pair was swapped, right?

7

u/Nathanfenner Dec 03 '19

Yes, you can adaptively remember where the last swap was on the previous iteration, and stop looking past that point on future iterations.

(insertion sort still ends up faster)

2

u/Slapbox Dec 03 '19

In this case I think yes, because it was the leading pair of numbers that was swapped and that was the only swap. Someone please correct me if I'm wrong.

70

u/[deleted] Dec 02 '19

You could’ve had 420 69

17

u/lilyslilfeetsies Dec 02 '19

that’s what I was waiting for

7

u/xebecv Dec 03 '19

I love this good old visualization of various different sorts: https://youtu.be/kPRA0W1kECg

3

u/uber1337h4xx0r Dec 03 '19

Radix sort and merge sort made me come

1

u/G_Morgan Dec 03 '19

Bogosort - If you cannot handle me at my worst you don't deserve me at my best

6

u/[deleted] Dec 03 '19

Ugh I hate watching bubble sort, I just want to grab it by the neck and shake it

3

u/Azzk1kr Dec 03 '19

Like Cocktailsort?

1

u/[deleted] Dec 03 '19

I think cocktail/shaker has order n2 complexity as it's worst case, which is better than bubble actually. Cocktail is an innovation of bubble, no?

Edit: I can't deny bubble looks pretty nice in code though. It's so terse!

1

u/G_Morgan Dec 03 '19

I think it is the same but probably has better locality on larger data sets.

3

u/redalastor Dec 03 '19

When I was a teen and I learned to code with no CS notion at all, I invented my own sort which is bubblesort-ish but not exactly.

I would scan the array. If the two numbers I compared were sorted, I would move the pointer one cell further. If they weren't I would swap, then move the pointer left. Repeat until you reach the end of the array.

Is there a name for that algo?

7

u/[deleted] Dec 03 '19 edited Feb 18 '26

This post was mass deleted and anonymized with Redact

compare nail stupendous follow north chief cake quicksand rainstorm detail

5

u/[deleted] Dec 03 '19

I wonder if there's research in CS/maths trying to determine if there's a limited number of different sorting algos?

1

u/chrisrazor Dec 03 '19

And is there an ordering on such algorithms.

7

u/urbanspacecowboy Dec 03 '19

Sounds like the gnome sort, which is a simpler slower variation on the insertion sort.

3

u/redalastor Dec 03 '19

It is gnome sort!

I'm surprised to know I used it before it was introduced. I thought all simple sorting algos had been published many decades ago.

3

u/bubblesort Dec 03 '19

Yes! It looks exactly like me! This is gonna be my new social media picture.

3

u/PeasantSteve Dec 03 '19

Add it to the pile.

If you're going to do a bubble sort visualisation, at least have some folk dancing involved: https://youtu.be/lyZQPjUT5B4

1

u/leo_szilard Dec 03 '19

Could you do this for other searching and sorting algorithms as well?

1

u/pedrovhb Dec 03 '19

I plan on doing that!

2

u/[deleted] Dec 03 '19

I only believe in crab sort. 🦀

1

u/spacelama Dec 03 '19

I was expecting this to be the David Attenborough bubble sort visualisation: (about the 4th or 5th reply down: https://twitter.com/histoftech/status/1200585772618924032 )

2

u/WalkingTaco42 Dec 03 '19

Algorithms are becoming something people take for granted - you just use whatever framework elements are there and don't really need to understand.

Back in the day, you had to write all this crap from scratch. Often just pulling code you wrote at some point and pasting it in there.

The real reason to understand how it works is efficiency. You need to know if the input list is going to be relatively large or not and then pick a sort based on that. [Big O notation]|(https://en.wikipedia.org/wiki/Big_O_notation) is helpful for that - so in this animation, showing consideration to the size of the dataset (7 here) and the number of times our sort box loops over the list should be highlighted.

6

u/iVeryTasteful Dec 02 '19

thought it was going to be 4206957

3

u/MrSolidSht Dec 02 '19

1

u/VredditDownloader Dec 02 '19

beep. boop. I'm a bot that provides downloadable video links!

I also work with links sent by PM


Info | Support me ❤ | Github

3

u/Mborg15202 Dec 02 '19

It should have been 4206957

3

u/Non808 Dec 03 '19

3Blue1Brown?

4

u/its_sma Dec 03 '19 edited Dec 03 '19

Probably made with manim

5

u/SJWcucksoyboy Dec 02 '19

I feel like sorting algorithm visualizations have been done to death.

25

u/RayDotGun Dec 03 '19

Show me on this doll where the algorithm hurt you. We’ll sort this out, O(k)?

1

u/Sarke1 Dec 03 '19

Quantum Bogosort or GTFO.

1

u/MozzieMouss Dec 03 '19

1

u/VredditDownloader Dec 03 '19

beep. boop. I'm a bot that provides downloadable video links!

I also work with links sent by PM


Info | Support me ❤ | Github

1

u/Gagan2019 Dec 03 '19

Good video showing the idea behind the bubble sort.

1

u/adorak Dec 03 '19 edited Dec 03 '19

also slow af ... I made a python appllication once, testing various sorting algorithms and visualizing how fast they are and I remember Bubble Sort is beyond bad :)

To no ones surprise TimSort performed the best (afair) ... it was a few years ago so a) I forgot a lot b) it might be wrong by now. But Bubblesort's big O is n^2 (in the worst case) and TimSort is n*log(n) - every algorith with n^2 will fail miserably in their respective worst cases. They are however, great to visualize. There's also a dance choreography I saw once, that showed mergesort (If I remember correctly) - that was great.

edit (if you wonder): my python program had various test cases, ranging from small to large lists that were already sorted, randomized, few switches, reversed etc.. afaik I sorted each case 10k times to be somewhat statistically relevant ... but as I said is was several years ago :(

1

u/iiTheBeast Dec 03 '19

I do have a question how do you calculate asymptomatic notation, lets say for this? I thunk it was n2 tho

1

u/[deleted] Dec 03 '19

Why do people keep using bubble sort? I mean, isn't it one of the most time consuming sort types?

1

u/PutTheBlameOnMe Dec 03 '19

I really thought it was going to make 42069

1

u/sdexca Dec 03 '19

I remember making this swotting years back with I first started C. Ah the old memories 😊

1

u/Sunstorm777 Dec 03 '19

Sorry if I sound ignorant, but why do computer scientists need to learn the sorting methods?

1

u/geek_ki01100100 Dec 03 '19

Can we have a link to download this from?

1

u/[deleted] Dec 03 '19

did anyone else think it would say 42069

1

u/Asl687 Dec 03 '19

Used to do this on high score table for 8bit games. Add new score to bottom of table and do a single bubble sort pass every few frames, makes the new score move up the table line by line..

1

u/thealbaniandude Dec 05 '19

Why does this have so many upvotes? It's just bubble sort.

0

u/PVNIC Dec 02 '19

Congrats, you made bubble sort even slower.

1

u/TheBowtieClub Dec 03 '19

Relevant: the Crab Shell Upgrade bubble sort https://twitter.com/geekandahalf/status/1200440963753283584

1

u/spacelama Dec 03 '19

Indeed, the video of that particular bubble sort is about the 4th or 5th reply down: https://twitter.com/histoftech/status/1200585772618924032

1

u/B1SQ1T Dec 03 '19

Now I needa find a merge sort visualization ;-;

1

u/Pascal-C-El-Rojo Dec 03 '19

This is perfect timing, because I just learned this today in my Java class :)

0

u/Scuramble Dec 02 '19

You were so close to 420 and 69 come on

-2

u/Dank-memes-here Dec 02 '19

Bad animation. Should do 1 less comparison each run, and early-out once sorted

0

u/[deleted] Dec 02 '19

Nice!

0

u/Sir_Hurkederp Dec 02 '19

Thank you, i had to implement this but didnt quitr understand the algorithm i was given

0

u/SlippySloppyToppy Dec 03 '19

Is it just me or could you have made 42069?

0

u/ElectricalSloth Dec 03 '19

I DONT SEE NO BUBBLES

0

u/PJDubsen Dec 03 '19

That was painful to watch

-2

u/[deleted] Dec 02 '19 edited Dec 07 '19

Those aren't bubbles. Those are numbers.

Edit: Zero appreciation for dad jokes. :(

0

u/Saurons_Monocle Dec 03 '19

This would be pretty useful in teaching an Intro to Data Structures class!

0

u/mrkent27 Dec 03 '19

This reminds me of the animations in 3blue1brown videos

0

u/Mokaran90 Dec 03 '19

Useful, I was bashing my head around this one on the past weekend, it also does it with letters! Wtf!

0

u/DenormalHuman Dec 03 '19

Who implements sorting algorithms nowadays?

-15

u/[deleted] Dec 02 '19 edited Dec 03 '19

Why though? Why go through the trouble of creating a video explaining one of the worst sorting algorithms ever invented. Why do we even teach this in school?

edit: I really wasn't trying to gatekeep. Bubblesort is simply a shitty algorithm that has nothing to offer to the world. Teaching bubblesort is like teaching bogosort - there is nothing useful in the knowledge.

17

u/BruhWhySoSerious Dec 02 '19 edited Dec 02 '19

I always have to laugh when some gate keeper tries to act like a smart ass. Oh you are so smart but you can't see why they teach a stupid simple algo? Jesus Christ, it's to learn the basics and what not to do.

2

u/valarauca14 Dec 02 '19 edited Dec 02 '19

Not really?

Donald Knuth actually stated in "The Art of Computer Programming" there is no practical reason to teach Bubble Sort aside from the catchy name. He recommends teaching "Insertion Sort" as the first O(n2 ) sort students are taught.

Owen Astrachan (principle investigator of the multi-year AP Computer Science courses & exams) agrees. The guy is actually on a personal crusade to stop teaching Bubble Sort totally. He's used far harsher language to describe it.

There isn't a great reason to teach Bubble Sort. It isn't gate keeping, it is just wasting the teacher's & student's time because literally every other sort is superior. Shell Sort is actually far more intuitive, and Insertion Sort is heavily used in modern hybrid sorts algorithms (some of the fastest known, as a fall back mechanism within quick sort's partitions). There's way better uses of everyone's time than teaching Bubble Sort.

What I'm trying to say is prioritizing subject matter to be impactful and make best use of everyone's time isn't gate keeping.

3

u/chucker23n Dec 02 '19 edited Dec 03 '19

Donald Knuth actually stated in “The Art of Computer Programming” there is no practical reason to teach Bubble Sort aside from the catchy name. He recommends teaching “Insertion Sort” as the first O(n2 ) sort students are taught.

Bubble sort has nothing to do with a catchy name. It is stupid simple to explain to a broad variety of people. Regardless of sorting, it’s just an excellent beginner’s example of an algorithm.

Teaching insertion sort and pointing it out as O(n2) completely misses the point.

1

u/BruhWhySoSerious Dec 02 '19

I am shocked that a professor at one of the greatest comp sci programs on the planet thinks bubble sort is not worth his 2 minutes.

There isn't a great reason to teach Bubble Sort.

It's simple.

While a comp sci class at UC or an AP class will only spend 15 minutes on this, a community college course is going to spend days on this, and have MANY students missing any real mathematical background.

I'll actually look into the reasoning behind this later but I cannot imagine a good reason for skipping a 15 minute lecture on this purely as and introduction and example on what not to do. For many, understanding the difference between insertion, bubble, and merge is a complex topic which will need reinforcement.

→ More replies (4)

1

u/[deleted] Dec 02 '19

Yup, definitely wasn't trying to gatekeep.. was actually quoting my intro to CS professor from a decade ago.. he's known in his field for developing and writing books for CS101 courses. Gave a strong speech why he refused to teach bubblesort.

→ More replies (5)