The Simplest Sorting Algorithm (You’ve Never Heard Of)

2022 ж. 12 Сәу.
126 638 Рет қаралды

Our Patreon: / polylog
We know this beautiful algorithm from the recent preprint by Stanley P. Y. Fung:
arxiv.org/pdf/2110.01111.pdf
However, it turns out that other people stumbled across it multiple times, see e.g. the discussion here:
news.ycombinator.com/item?id=...
------------------------
Attributions:
To make this video, we used manim, a Python library: docs.manim.community/en/stable/
The color palette we use:
ethanschoonover.com/solarized/
Thumbnail contains this version of Raphael’s angels
www.kindpng.com/imgv/iJxhTTJ_...
Music: Messiah by Händel
en.wikipedia.org/wiki/File:Ha...

Пікірлер
  • Be careful not to make a sorting algorithm too fast. Years ago a friend worked for a timesharing service (consider it caveman cloud computing) and wondered why his new program took so long. He discovered that the system sort routine was stupidly slow. So he fixed it to be fast. A few days later his managers yelled, "WHAT DID YOU DO??" It turned out that his sort speedup also made many customer batch jobs run so fast that their CPU bills were suddenly WAY lower, and the timesharing company's revenues were significantly dropping. "PUT IT BACK THE OLD WAY" they said. So he did.

    @ddichny@ddichny Жыл бұрын
    • Management: "Efficiency? We don't want that around here!"😂😊👍

      @DarkVoidIII@DarkVoidIII Жыл бұрын
    • Additionally, the corollary to this is that suddenly they get 100x as many customers, and then yell at him, again, to "Speed it up, we ran out of time to process our customers! Too many of them are leaving!" 😊😂👍

      @DarkVoidIII@DarkVoidIII Жыл бұрын
    • The free market doing its thing again.

      @genghiskhan6688@genghiskhan6688 Жыл бұрын
    • @@genghiskhan6688 They are stupid and greedy, they could accommodate way more customers without adding more physical resources thus generating more revenue at a lower cost.

      @ConceptsMadeEasyByAli@ConceptsMadeEasyByAli Жыл бұрын
    • god i hate the specific capitalism brainworm that such managers have. It's no better than fraud.

      @OutbackCatgirl@OutbackCatgirl Жыл бұрын
  • The most simple algorithm is of course miraclesort; Is it sorted? Yes - done No - wait n seconds, then check again

    @shirazull4027@shirazull4027 Жыл бұрын
    • Diet Bogo sort i see

      @Apersonl0l@Apersonl0l Жыл бұрын
    • If you wait long enough, a cosmic ray will at least make the algorithm think it's done

      @diribigal@diribigal Жыл бұрын
    • @@diribigal Most models of including don't model cosmic rays as being possible.

      @gyroninjamodder@gyroninjamodder Жыл бұрын
    • @@gyroninjamodder I'm not sure what you mean by "of including". Did you mean "of computation"?

      @diribigal@diribigal Жыл бұрын
    • @@diribigal yes, models of computation

      @gyroninjamodder@gyroninjamodder Жыл бұрын
  • This is a horrific n^2 monstrosity

    @jaca2899@jaca2899 Жыл бұрын
    • It's sufficiently perverse for my tastes

      @veggiet2009@veggiet2009 Жыл бұрын
    • At least it's better than Stooge sort

      @timanderson5717@timanderson5717 Жыл бұрын
    • @@timanderson5717 An early version of the script featured an alignment chart of sorting algorithms that included stooge sort :)

      @PolylogCS@PolylogCS Жыл бұрын
    • Technically not worse than the initial algorithm. The factor changes, but it's O(n²) compared to O(n²), what's doable on one is doable on the other.

      @mozteq7536@mozteq7536 Жыл бұрын
    • The exact number of operations is more than selection sort (or even bubble sort and linear insertion sort for that matter) but all four of them scale by O(n²).

      @nanamacapagal8342@nanamacapagal8342 Жыл бұрын
  • "...for a sufficiently perverse definition of the word simple" GOLD

    @samevans4834@samevans4834 Жыл бұрын
    • 🤣

      @johnyjohnjohnson1317@johnyjohnjohnson1317 Жыл бұрын
    • Came to post just that; you are top comment. Take my like!

      @Tasarran@Tasarran Жыл бұрын
  • Try sleep sort. Start a thread for each element in the array and sleep for that ms, at the end of sleep print the number. It'll be sorted.

    @imerence6290@imerence6290 Жыл бұрын
    • Yes. Came here to say this. Sleep sort is the true simplest sorting algorithm.* * for a sufficiently perverse definition of the word simple

      @zellfaze@zellfaze Жыл бұрын
    • Another simple sorting algorithm, but works only for ints. Int ar1[5] = {5, 9, 2, 11, 8}; Int ar2[10000] = {0}; For(int I=0; i

      @Godfather-qr6ej@Godfather-qr6ej Жыл бұрын
    • @@Godfather-qr6ej bruh lmao

      @imerence6290@imerence6290 Жыл бұрын
    • @@Godfather-qr6ej You reinvented counting sort. Or radix sort, which have time complexity of logN, but if base of a log is equal of higher than count of array is linear (square memory complexity in this case)

      @Inf1e@Inf1e9 ай бұрын
    • this is funny :)

      @seenlenz@seenlenz8 ай бұрын
  • The simplest sorting algorithm is calling the standard library sort function.

    @gorgenfol@gorgenfol Жыл бұрын
    • This is what fully understanding the assignment looks like.

      @nolanbrewer574@nolanbrewer574 Жыл бұрын
    • And what algorithm does the library function use? Answer: Usually Quicksort or some variation of Heapsort. Sometimes, it's better to write your own algorithm, because the set-up for using the library function can involve dozens of lines of code and helper (comparison) functions, when all you need is to compare one or two members of structs/objects within an array.

      @DavidRTribble@DavidRTribble Жыл бұрын
    • Most likely introsort or timsort, which are many algorithms merged into one.

      @EasonTek@EasonTek Жыл бұрын
    • @@DavidRTribble actual answer… It uses whatever the team of engineers was paid billions of dollars to develop and optimise and it could change at any time. So it doesn’t matter what algorithm it uses… it works… and it is quick. 😂

      @Fogmeister@Fogmeister Жыл бұрын
    • std::sort is kinda efficient)

      @Inf1e@Inf1e9 ай бұрын
  • Symmetry is always a good candidate for simplicity, even though it might make things less efficient.

    @Number_Cruncher@Number_Cruncher2 жыл бұрын
  • "The only drawback is that it sorts in reverse order...:" and the horrendous O(n^2) runtime (as mentioned in other comments)

    @SergeAzel@SergeAzel Жыл бұрын
    • Indeed :) We should have mentioned that our quest is guided by beauty, not speed...

      @PolylogCS@PolylogCS Жыл бұрын
    • Clearly efficiency was not the point of the video. Just enjoy the ride.

      @tonyennis1787@tonyennis1787 Жыл бұрын
    • Sometimes, the time spent to program an algorithm is orders of magnitude greater than the amount of time you'd ever wait on it to finish. In such cases, having something incredibly unsophisticated and easy to remember can save you time. I doubt that something as common as sorting could be such a case, but IF both implementing a well-scaling algorithm and using a library function would take too much of your time, then this is the solution for you (or bubble sort).

      @Pystro@Pystro Жыл бұрын
  • This is sometimes called "the postman's sort". If you were a postman walking a route to deliver it, when you first pick up today's mail you would take a piece of mail and insert it in your carry bag. Then the next piece you would put 'where it belongs' in relation to the first piece on your route. Then each piece you pick up from the bin, you put it in the 'correct position' as you put it in your bag. It is simple, you only have to handle each piece of mail once. BUT, you have to somehow be able to 'insert' each new piece in the correct spot. With computer memory, that often means you have to shift other items up/down in your array to make room for it. (if you're using linked lists, 'insertion' is relatively fast/easy).

    @mikefochtman7164@mikefochtman7164 Жыл бұрын
    • Most human sorting are fast because semantically they are working with an index rather than binary comparison. For text the index is usually an alphabet. For a postman the index is the route map. The bag can be modeled by a preallocated indexmap, with a logical index structure but a continuous memory allocation.

      @YuFanLou@YuFanLou Жыл бұрын
  • I think one usefully "perverse definition of simple" here would be to sort in the smallest number of instructions. Sorting small buffers with minimal instruction memory could be useful for some IoT applications. Consider the "state machines" on the RP2040 where you only have space to store 32 instructions. n^2 is still going to be a smallish number of microseconds for a 16 byte buffer.

    @billywallace1360@billywallace1360 Жыл бұрын
    • But at least to my knowledge, they can only decrement and compare equality, so they can not practically be used for sorting.

      @gansimgluck9858@gansimgluck985811 ай бұрын
  • One of my favorite sorting methods is this: 1) For some number N times (preferably with N larger than the size of the list), randomly choose two items in the list and put them in order. Note that this is O(N). At this point, the list should be mostly in order with some items off a bit. 2) Then, use the sort of your choice that works well on sorted data. At this point, even a bubble sort is generally very efficient.

    @ericjohnson5969@ericjohnson5969 Жыл бұрын
    • Reminds me a bit of comb sort, except that the distance between elements is random instead of following a pattern.

      @Pystro@Pystro Жыл бұрын
    • @@Pystro One variation would be to step through each position on the list and choose a different random element on the list and put the two in order, if necessary. It would be interesting to see how it would compare for different numbers of passes through the array

      @ericjohnson5969@ericjohnson5969 Жыл бұрын
    • Simulated Annealing sort

      @georgesamaras2922@georgesamaras29223 ай бұрын
  • this is the first sorting algorithm i thought of in hacker rank. then it gave me the warning "operation was too slow. get outta here and optimize your code" 💀

    @bdenix1997@bdenix1997 Жыл бұрын
  • In the world of algorithms, “simple” algorithms are often inefficient. Technically, the simplest would be bogosort: 1. Shuffle the list. 2. Is it sorted? Yes → end No → go back to 1

    @adiaphoros6842@adiaphoros6842 Жыл бұрын
    • That has a random number generator tho, which is not very simple

      @iwersonsch5131@iwersonsch5131 Жыл бұрын
    • Yeah, I think the simplest is something like “search through to find the lowest value, and put it at the start of another list. Repeat”

      @lucar6897@lucar6897 Жыл бұрын
    • @@lucar6897 You’re describing selection sort. Which according to polylog, is more complicated than his simplesort, based on his definition of “simple.”

      @adiaphoros6842@adiaphoros6842 Жыл бұрын
    • The word "shuffle" hides a surprisingly large amount of complexity there. I will propose this sort as the simplest, provided you're running on actual hardware. It's even in-place, but I make no guarantees as to the average or best case time complexity: 1. Hope for bit errors resulting in the list sorting itself.

      @siosilvar@siosilvar Жыл бұрын
    • What about gulagsort? It's ought to be he simplest

      @the_multus@the_multus Жыл бұрын
  • I discovered this by mistake in high school many years ago (more that 20y). I realized that is a mistake in the code but after I saw that even so it sorts a vector. I saved the code like this for further research, my colleagues and my teachers didn't know why it still sorts. Now I know

    @vladpopescu6265@vladpopescu6265 Жыл бұрын
  • This sorting algorithm is called a student wrote bubble sort but made a common mistake, but it still works.

    @makarhunter@makarhunter Жыл бұрын
    • LMAO

      @raianmr2843@raianmr2843 Жыл бұрын
  • I heard of this as selection sort and not selectsort. One of the first sorting algorithms we encountered alongside bubble sort and insertion sort.

    @deltacream@deltacream Жыл бұрын
  • The selection sort is an improvement over Bubble sort that also has trow loops and one comparison in the inner loop. The second one is ... weird. Of course the number of operations is higher but at the same order (n^2 versus (n^2 - n) /2). Yes It is simple and I can't imagine a simpler way. Interesting because all the time we always think how to make algorithms faster, more efficient, but rarely find the simplest one.

    @agranero6@agranero6 Жыл бұрын
  • It's a neat sorting algorithm, in terms of it's looks, but it's not efficient at all.

    @livedandletdie@livedandletdie Жыл бұрын
    • It could be more efficient for niche applications where the data set isn't fixed. Data elements constantly being added, removed, or changed. Online databases, etc. The redundant instructions in this "simple" N-squared ugliness would actually be useful in this circumstance.

      @pwnmeisterage@pwnmeisterage Жыл бұрын
    • @@pwnmeisterage definitely not, if you want constantly sorted data with updates just use a map or a set which both allow for fast operations

      @maciejmalewicz9123@maciejmalewicz9123 Жыл бұрын
  • Sleep sort would like to have a word with you.

    @peezieforestem5078@peezieforestem5078 Жыл бұрын
  • I never expected simplest algorithm to be so complex.

    @the_akshay_p@the_akshay_p Жыл бұрын
  • Our teacher called this “bubble sort” and asked it at our final

    @phytoplancton4038@phytoplancton4038 Жыл бұрын
    • That's not bubble sort, tho. Bubble sort works by swapping adjacent values only.

      @AraliciaMoran@AraliciaMoran Жыл бұрын
  • Famous Bubble sort you've never heard of.

    @frun@frun Жыл бұрын
  • This is why I don't write my own algorithms. Me: "This looks pretty, I'm going to run with it." My CPU: *lets the magic smoke out*

    @krispockell685@krispockell685 Жыл бұрын
    • I feel yah. CPUs I do well with. only ever got the magic smoke you using a sledge (sledgehammers fix ANYTHING, btw - and if it breaks, it needed replacing anyway). My critical weak point is memory usage. ‘200MB memory to calculate the 6 steps to craft item X in a game - why do you need 200mb in a single string when each step just combines 2 items?’ Simple solutions may hold elegance… and fall to pieces if you insist on any realism.

      @Relkond@Relkond Жыл бұрын
    • You're lying. CPUs don't do thar

      @StefanReich@StefanReich Жыл бұрын
    • I actually wrote multiple sorting algorithms myself and many of them easily beat std::sort by an order of magnitude. The best "magyarsort" takes only 8.5x the time to sort 100 million integers than doing memcpy on them for example - which is extreme fast if you ask me. - and my sort is already faster than std::sort from around 64 elements (and starts to be the same speed around 20). So it basically beats the library sort from C++ in every use case. Also beats ska_sort by around a 2x speedup on random data - but uses more memory. There is a variant which uses less memory and still beats ska_sort and ska_copy sort.

      @u9vata@u9vata Жыл бұрын
  • I have in fact heard of this before. It is listed on Wikipedia as the I Can't Believe It Can Sort.

    @MCGeorgeMallory@MCGeorgeMallory Жыл бұрын
  • I find the Theta(n^2) bubble sort the simplest. For j from 1 to n: for i from 1 to n-1: if arr[i] > arr[i+1]: swap(arr[i], arr[i+1]).

    @Jetpans@Jetpans Жыл бұрын
  • That's exactly the algorithm i came up with before learning anything about sorting algorithms

    @zbarba@zbarba Жыл бұрын
  • I love the fact that the Select Sort can be made optimal by finding the min value index in time O(log(n)) by using a min-heap structure. So you would still have a linear part (the outer loop), but the inner one would be changed with a call to find the min value in a min-heap, that is by itself theoretically costant since the min value will always be in the root of the tree, but it becomes logarithmic due to the heapify function that rebuilds the heap after the extraction of the root value. In the end you would change the time complexity of the Select Sort from O(n^2) to O(n * log(n)) which is indeed proved to be optimal for a sorting algorithm. What you get is the algorithm known as Heap Sort.

    @DarkH4X0@DarkH4X0 Жыл бұрын
    • It is proofed to be perfect for algorithms that compare the values of the array that shall be sorted. You can look up Countingsort for example which has an even better time complexity, because it takes a different approach.

      @luismuller6505@luismuller6505 Жыл бұрын
  • Come on man only one video? It’s was amazing we need and more

    @Thewe34@Thewe34 Жыл бұрын
  • Why start at 1 tho? Starving African computers could have eaten that wasted bit...

    @valrina@valrina Жыл бұрын
    • omg that's awful, but funny 😂

      @SpiritmanProductions@SpiritmanProductions Жыл бұрын
  • Gnomesort is nice, it's about this size and is the only sort I can find that lets you keep track of the parity.

    @aron8999@aron8999 Жыл бұрын
  • is this then a less powerful form of (double optimised) bubblesort which saves two conditionals but never early outs? I searched for “simple sort” but got results for selection sort which seems to be the same algorithm

    @j.j.maverick9252@j.j.maverick9252 Жыл бұрын
  • Is this shell sort or bubble sort? Two very simple algorithms for sorting large lists, either would be fairly quick on a small selection of numbers, but which of the two shown in the video would be more efficient to use, if, for instance, there were more than 100 entries to be sorted?

    @DarkVoidIII@DarkVoidIII Жыл бұрын
    • Insertion and selection sort are similarly fast. The general recommendation is to call the sort function from the standard library since whoever wrote it is a bigger efficiency nerd than you.

      @totally_not_a_bot@totally_not_a_bot Жыл бұрын
    • An optimized selection sort and insertion are similar, though the insertion will still be slightly faster. Bubble is much slower, and shell sort much faster. Here's the output of a program I wrote in C for fun, for speed testing multiple sorting algorithms. For reference, this was on a Ryzen 7950X, but the times remain relative across different systems. (+Opt = Optimized Algorithm, and NR = Non-Recursive): Sort method: Size: 1024 4096 16384 65536 ========================================================================= Bubble sort: 0.001314 secs 0.020288 secs 0.327573 secs 5.379057 secs Bubble +Opt: 0.000959 secs 0.013028 secs 0.204979 secs 4.454882 secs Selection: 0.000683 secs 0.010469 secs 0.166774 secs 2.666797 secs Selection +Opt: 0.000421 secs 0.006290 secs 0.098621 secs 1.562943 secs Insertion: 0.000278 secs 0.004402 secs 0.072242 secs 1.150316 secs Shell sort: 0.000073 secs 0.000464 secs 0.003553 secs 0.021817 secs Quicksort NR: 0.000034 secs 0.000153 secs 0.000691 secs 0.003058 secs C Quick sort: 0.000053 secs 0.000206 secs 0.000930 secs 0.004307 secs

      @nunyobiznez875@nunyobiznez87510 ай бұрын
  • Very nice. Strange I've never seen it before.

    @banderfargoyl@banderfargoyl Жыл бұрын
  • This is mind opening for undergrads like me

    @fuzzy-02@fuzzy-02 Жыл бұрын
  • "You've never heard of" Proceeds to show the algorithm we all thought of when we first started

    @davinki3728@davinki3728 Жыл бұрын
    • I didn't. Mine was like bubble sort except with twice as many operations that did nothing. I was 9.

      @SianaGearz@SianaGearz2 ай бұрын
  • I’d say the simplest sorting algorithm is just sort(a).

    @chixenlegjo@chixenlegjo Жыл бұрын
    • well, yes, but (at least in JS) it doesn't work with numbers since it sorts them by the higher *first* number. by this rule, 100 < 95 < 8

      @the_neto06@the_neto06 Жыл бұрын
    • @@the_neto06 To add on to what you said, [-2, -1, 0, -0, 2, 11].sort() evaluates to [-1, -2, 0, -0, 11, 2]

      @ryan8742@ryan8742 Жыл бұрын
    • array.sort((a, b)=>(a-b)) This has the added benefit of being able to sort a list of objects by their properties. objectArray.sort((a.x, b.x)=>(a-b))

      @Panda_Gibs@Panda_Gibs Жыл бұрын
  • The double for-loop reminds me a bit of the Floyd-Warshall algorithm.

    @breathermachine@breathermachine Жыл бұрын
  • MIT algorithms course taught me to use quicksort but do a shuffle first. Reason being? An almost sorted list can reach n squared time with quick sort.

    @colinmaharaj@colinmaharaj Жыл бұрын
  • Good representation of the algo. Add more videos pls. Tnx.

    @codingindeep@codingindeep Жыл бұрын
  • Beautiful video I like it ❤️

    @oussamatut3739@oussamatut3739 Жыл бұрын
  • The simplest sorting algorithm in terms of both coding and time complexity is obviously qbSort: EnthropyGenerator.Permute (array); For (int i = 0; i < array.length - 1; i++) {if (array[i] > array[i+1]) this.universe.terminate();}; Can you even call that 4 lines? It's more like 3 And of course, the complexity is only O(n).

    @michalberanek2783@michalberanek2783 Жыл бұрын
  • I think I used this before, but with a name of bubblesort.

    @wizardnotknown@wizardnotknown Жыл бұрын
  • This is awesome video , i love it can you make more videos about algorithems in simplest ways ??? please i really like your style of explaination

    @haithamalbadi2192@haithamalbadi2192 Жыл бұрын
  • It feels a lot like Bubble Sort.

    @nuzayerov@nuzayerov Жыл бұрын
  • i don't get how it's simplier than i = 1 to n { j = i to n {swap state}}, it's only slower

    @nicolasturek7563@nicolasturek7563 Жыл бұрын
  • Of course I know simple sort, selection sort, insertion sort (since they where part of the algorithm and data structures lecture at my university back then in the 90s). However the video is nicely done and the visualization helps to understand how each respective algorithm works. There are other KZheadrs who focus on the pure illustration of different sorting algorithms, but as soon as I mention channel names here, my comment gets deleted. So feel free to search for them, too 🙂

    @tychobra1@tychobra1 Жыл бұрын
  • Can you make a video about sleep sort? That thing is so stupid, it is kinda smart.

    @GGysar@GGysar Жыл бұрын
  • Dumb question (I followed an entry level gpu programming class a couple of years ago, but completely forgot even basic concepts and terminology): could it be faster on gpu than any other O(n^2) sorting algorithm because each thread operates on the same amount of data and there are no/fewer stalls this way?

    @aluminatestrontium7298@aluminatestrontium7298 Жыл бұрын
    • The idea is that its a number of calculations. Theres n elements in the list, and you have to iterate through each of them twice. Its a bit of concepts above basic programming courses.

      @gn0my@gn0my Жыл бұрын
    • @@gn0my in a sequential computation model I agree, but in a massively parallel model such as gpu the idea is "load a bunch of data, do computations in parallel, wait everyone to finish and gather data", which implies that wasting computation may sometimes be faster because synchronization requires less time. I was wondering if this is one of those cases or not

      @aluminatestrontium7298@aluminatestrontium7298 Жыл бұрын
    • @@aluminatestrontium7298 Can't parallelise this because the steps have to be done in order

      @galoomba5559@galoomba5559 Жыл бұрын
  • It even works in my custom scripting language I wrote

    @tesses50@tesses50 Жыл бұрын
  • Can u explain how radix sort works

    @generalyoutubewatching5286@generalyoutubewatching5286 Жыл бұрын
  • You deserve a subscribe man

    @mohammedhayyoun@mohammedhayyoun Жыл бұрын
  • So bubble sort? The first sort algorithm taught usually? :)

    @michaelgraff6978@michaelgraff6978 Жыл бұрын
  • This video was so satisfying

    @ciberman@ciberman Жыл бұрын
  • I've got a Big O notation sensor in my mind's computational complexity modulus, whenever I see a loop inside the other, what I'm actually seeing is a ridiculous slow quadratic algorithm, and I'll always pass if I can...

    @freedom_aint_free@freedom_aint_free Жыл бұрын
  • bro Bubble Sort MK2 be looking dope

    @sirnikkel6746@sirnikkel6746 Жыл бұрын
  • Is time complexity will be the same?

    @lorddiaz9658@lorddiaz9658 Жыл бұрын
  • If you not prefer stability in sorting, then I have developed a new algo for sorting which is pretty simple.

    @nomankhan7190@nomankhan71903 ай бұрын
  • ✨ The wobbly dobbly bubble sort ✨

    @medoncho2397@medoncho2397 Жыл бұрын
  • Urgh two loops? Soo complicated! When I need a simple sorting algorithm I always use Gnome Sort, that only needs one loop!

    @QuantenMagier@QuantenMagier Жыл бұрын
  • Never heard of? 1990 CS undergrad. This was a few weeks into the first semester of freshman year.

    @terrymiller111@terrymiller111 Жыл бұрын
  • i literally thought of this myself too lol

    @justinj.421@justinj.421 Жыл бұрын
  • yea definitely I've never heard about bubble sort haha

    @namphan9281@namphan9281 Жыл бұрын
  • SleepSort is simpler and has an O(1) complexity. Have N threads each wait for its elements value. Threads will terminate in order, resulting in a sorted list.

    @LulledLion@LulledLion Жыл бұрын
    • now prove its correctedness for all x E R 😈

      @goomymaster6417@goomymaster6417 Жыл бұрын
    • It works. The true nasty part is where constant K is worse than NLogN, but K gets simplified to 1 in the O notation.

      @LulledLion@LulledLion Жыл бұрын
    • Arr[2] = -1

      @theepicturtleman@theepicturtleman Жыл бұрын
  • I always use this algorithm, unless I really need something to be super fast.

    @player400_official@player400_official Жыл бұрын
  • They got us on the second half, not gonna lie.

    @fuzzy-02@fuzzy-02 Жыл бұрын
  • ... also called bubble-sort, cause the elements go up like bubbles ...

    @seboeh5173@seboeh5173 Жыл бұрын
  • It takes twice as many steps as the original sort, so it'll only be half as fast. Also, it sorts counterintuitively, so it is not "simple" in that regard. I'd rather call it "perverse sort".

    @bart2019@bart2019 Жыл бұрын
  • ima be real with u this is what i thought bubble sort was

    @182exe@182exe Жыл бұрын
  • I'm very curious what language is used in this video, or is it just a pseudo-language? It's using BASIC-style For loops, a ':' continuation or line ending, but [] brackets for array indexing, which isn't used in BASIC. The formatting also looks Pythonic, but not with those BASIC-style for loops. I even asked chatGPT about it, but it's only guesses were QBasic, FreeBASIC, and VB, all of which use standard BASIC array() brackets for indexing, which nearly broke that poor AI when I tried to explain that.

    @nunyobiznez875@nunyobiznez87510 ай бұрын
  • "Less is worse." 😂

    @emjizone@emjizone Жыл бұрын
  • Eu já programei com um código, quase idêntico, em Algol.

    @MsRenatoj@MsRenatoj Жыл бұрын
  • Wait I think we have a bug here because naturally arrays are zero indexed meaning i and j should start at '0' not '1', :)

    @siphamandlambuyazi5649@siphamandlambuyazi5649 Жыл бұрын
    • Most programming languages used by mathematicians (e.g. Fortran, Julia, R, Matlab, Maple, Mathematica, Maxima) index from 1. If you're labelling by order, rather than counting memory offsets, then it's natural to start from 1.

      @spacelem@spacelem8 ай бұрын
  • Next do exclusion sort!

    @remiwi2399@remiwi2399 Жыл бұрын
  • This is the algorithmy teacher taught to me and I use it when I solve problems because it's so easy to remember and sufficiently fast.

    @shaggyrogers9028@shaggyrogers9028 Жыл бұрын
    • oOoOoOoO.OoOoOoOo you dont do that

      @nayjer2576@nayjer2576 Жыл бұрын
  • I think my favorite has to be bogobogo sort. You recursively sort each sublist of size k from an input n until the k+1th element is bigger than the max of the sublist. It has to be n!^n or something

    @mismis3153@mismis3153 Жыл бұрын
    • But but but, sorting a sublist doesn't change the maximum of that sublist ...

      @landsgevaer@landsgevaer Жыл бұрын
    • @@landsgevaer No, but keep in mind that you do it recursively. You start with a list of size 1, which is always sorted. Then you check if the next element is bigger than that of the size 1 list. If it is, then the sublist of size 2 is sorted, otherwise, you shuffle the sublist of size 2 and start over (in this case, until the 1st element is smaller than the 2nd, which has 50% odds). Then, do the same for the sublist of size 3, etc...

      @mismis3153@mismis3153 Жыл бұрын
    • @@mismis3153 I found a link. Turns out it is way more convoluted. You shuffle the original array, make a copy of the array, then bogobogosort the copy excluding the last element, next if the last element of the copy by chance is the largest, then you check if the (now sorted) copy is the same as the original, otherwise you shuffle the entire copy and start over; if the copy wasn't the same, you reshuffle the original and start over.

      @landsgevaer@landsgevaer Жыл бұрын
    • @@landsgevaer not exactly. You got the spirit but you need to read it more carefully

      @mismis3153@mismis3153 Жыл бұрын
    • @@mismis3153 I read it more than once. Apart from my description being brief, where is the error then? I've actually reasoned that my interpretation leads to the same order of complexity, so I am curious where I misspoke or misunderstood, if anywhere ...

      @landsgevaer@landsgevaer Жыл бұрын
  • basically marking an bed sort even worse, and that still debatable the simplest, like, index sort does not even need comparisons

    @vincentsolus9227@vincentsolus9227 Жыл бұрын
  • that is N² that will be simpler if it be less than N lg N

    @Xayuap@Xayuap Жыл бұрын
  • I guess this is just bubble sort, Isn't it?

    @satviksrivastava5236@satviksrivastava5236 Жыл бұрын
    • No.

      @SianaGearz@SianaGearz2 ай бұрын
  • This is the bubble sort. Yes, it is simple to implement, but it is also incredibly inefficient and quickly becomes unusable (try sorting 1 million items using this algorithm!)

    @zachowland@zachowland Жыл бұрын
    • No. You're the bubble sort.

      @SianaGearz@SianaGearz2 ай бұрын
  • I've made this algorithm on my own in a logic programming class. I was the only one who was able to do it.

    @benjaminwaltermauss3349@benjaminwaltermauss3349 Жыл бұрын
  • Love your channel. A simpleton like me can always feel big brain here.

    @Rawi888@Rawi888 Жыл бұрын
  • It fails to sort the 0th element.

    @astamite-@astamite- Жыл бұрын
  • Don't all arrays start from 0? Why does this one start from 1?

    @hlexnc@hlexnc Жыл бұрын
    • That's what I thought too and I had to look it up when I saw this, evidently it seems that there are some languages that start at 1. Someone left a comment on stackoverflow about how counting at 0 is a thing started by the C language but that languages before C tended to start at 1. Supposedly this is a list of languages that start counting at 1: ALGOL 98 APL AWK CFML COBOL Fortran FoxPro Julia Lingo Lua Mathematica Matlab PL/I RPG R Ring Sass Smalltalk Wolfram Language XPath/ XQuery

      @BanjoCatfish@BanjoCatfish Жыл бұрын
    • @@BanjoCatfish add Maple and Maxima to that list (both languages used primarily by mathematicians).

      @spacelem@spacelem8 ай бұрын
  • Leaves me wondering how you’d rate radix sort for simplicity. It may be more complex, but for a little complexity it’s silly efficient at sorting huge sets of data, doing it without ever comparing elements.

    @Relkond@Relkond Жыл бұрын
    • IBM punch card sorters used radix sort.

      @tonyennis1787@tonyennis1787 Жыл бұрын
    • @@tonyennis1787 I’ve never had the luxury to play with punch cards at any level… their using radix sort actually makes a ton of sense - especially if all they had was an ability to sort one card at a time into a left or right output bin. You might need to sort a stack of cards log-n base 2 times, but it’d be bloody simple compared to manually collating the cards, especially for large sets

      @Relkond@Relkond Жыл бұрын
    • @@Relkond if memory serves, there were 10 output bins, one for each digit.

      @tonyennis1787@tonyennis1787 Жыл бұрын
    • @@tonyennis1787 I”ll bet there were different models with different numbers of output trays, depending on budget. You’d need 10 log base(trays) passes per digit to sort. My single exposure to card reader lore is the tale of Friar-tuck/Robinhood - malware used to demonstrate to Xerox a security vulnerability in their server they sold, which Xerox was slow to patch. The demo exploit made clear to Xerox why not patching the vulnerability was not a viable option… The demo exploit was installed (without authorization) to a Xerox-owned server. ‘The Xerox card reader had two output stackers; it could be instructed to stack into A, stack into B, or stack into A unless a card was unreadable, in which case the bad card was placed into stacker B. One of the patches installed by the ghosts added some code to the card-reader driver... after reading a card, it would flip over to the opposite stacker. As a result, card decks would divide themselves in half when they were read, leaving the operator to recollate them manually.’ - The Jargon File - Hacker Lore.

      @Relkond@Relkond Жыл бұрын
  • Same complexity O(n^2) as InsertSort and SelectSort.

    @mercuriete@mercuriete Жыл бұрын
  • Simple in therms of lines of code. But still a O^2 complexity level. To get it lower, you probably need to not use an array but a binary tree.

    @TheSiriusEnigma@TheSiriusEnigma Жыл бұрын
    • insertion sort is potentially slightly faster considering worst case scenario is o(n²) and best case scenario is o(n) + 1 swap

      @inn5268@inn52689 ай бұрын
  • aka BUBBLE SORT

    @xynyde0@xynyde0 Жыл бұрын
  • What is this 1-based indexing

    @Pscribbled@Pscribbled Жыл бұрын
  • I smell something sort of BUBBLY

    @vaisakhkm783@vaisakhkm783 Жыл бұрын
  • omw to destroy my IT exam 😎

    @V0W4N@V0W4N Жыл бұрын
  • huh. I thought that was called shell sort.

    @GiovanniCKC@GiovanniCKC Жыл бұрын
  • to say swap() is like to say .sort()

    @Xayuap@Xayuap Жыл бұрын
  • Quite perverse indeed

    @-7n@-7n Жыл бұрын
  • Dont do this in a job interview.

    @gn0my@gn0my Жыл бұрын
  • So, as a beginner I have to ask. Is this code Java script?

    @jpete190@jpete190 Жыл бұрын
    • No, python

      @26-dimesional_Cube@26-dimesional_Cube Жыл бұрын
    • @@26-dimesional_Cube It’s not Python. That for loop syntax does not exist in Python.

      @JMBalaguer@JMBalaguer Жыл бұрын
    • its just a pseudo code deyummm

      @tgenrex7896@tgenrex7896 Жыл бұрын
  • Studied this sorting method in 12th grade. Bubble sort, Good for small range of numbers, Will kill the cpu time if pushed in for large range....

    @christopherilayaraja1399@christopherilayaraja1399 Жыл бұрын
    • This is not bubble sort.

      @SianaGearz@SianaGearz2 ай бұрын
  • # my choice for p in permutations(a): if is_sorted(p): return p

    @SuperNolane@SuperNolane Жыл бұрын
  • Simplest? Ha! *laughs in bogosort*

    @a_random_person_@a_random_person_ Жыл бұрын
    • But as other people mentioned in the comments, generating random numbers is in fact pretty tricky. :)

      @PolylogCS@PolylogCS Жыл бұрын
    • @@PolylogCS technically yes but practically no. Most random and pseudo-random number generators are outsourced, and get boiled down a only a single line. Also, a randomized order a pretty simple* concept. *for certain definitions of simple

      @a_random_person_@a_random_person_ Жыл бұрын
    • *Laughs in gulagsort* …as I've mentioned…

      @the_multus@the_multus Жыл бұрын
  • Buble sort

    @Amlya@Amlya Жыл бұрын
  • Actually you can sort in one loop by using a bubble sort but moving the pointer back by one when you swap so it finishes in one pass. Maybe simpler?

    @thehint1954@thehint1954 Жыл бұрын
KZhead