Worst Sorting Algorithm Ever -

Поділитися
Вставка
  • Опубліковано 18 жов 2024
  • Complete Java course: codingwithjohn...
    Bogo sort is a terrible but hilarious sorting algorithm. Let's write it in Java!
    Full source available HERE, if you want to use this to yourself: codingwithjohn...
    Learn or improve your Java by watching it being coded live!
    Hey, I'm John! I'm a Lead Java Software Engineer and I've been in the programming industry for more than a decade. I love sharing what I've learned over the years in a way that's understandable for all levels of Java developers.
    Let me know what else you'd like to see!
    Links to any stuff in this description are affiliate links, so if you buy a product through those links I may earn a small commission.
    📕 THE best book to learn Java, Effective Java by Joshua Bloch
    amzn.to/36AfdUu
    📕 One of my favorite programming books, Clean Code by Robert Martin
    amzn.to/3GTPVhf
    🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial
    www.audibletria...
    🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)
    bit.ly/3QPNGko
    📹Phone I use for recording:
    amzn.to/3HepYJu
    🎙️Microphone I use (classy, I know):
    amzn.to/3AYGdbz
    Donate with PayPal (Thank you so much!)
    www.paypal.com...
    ☕Complete Java course:
    codingwithjohn...
    codingwithjohn...
    #Shorts

КОМЕНТАРІ • 283

  • @abhirammadhu2973
    @abhirammadhu2973 3 роки тому +3318

    But there is still a 0.000000001 % probability that this algorithm might outperform the fastest sorting algorithm known till date. 😂

    • @maurolionelmipianoyyo11
      @maurolionelmipianoyyo11 2 роки тому +24

      Is it bogo

    • @wihatmi5510
      @wihatmi5510 2 роки тому +54

      @@Pennervomland Checking whether a list is sorted is easy and doesn't require do sort it first. You just need to check for every element whether it's smaller or equal as the next.

    • @funktorsound
      @funktorsound 2 роки тому +20

      @@Pennervomland it doesn’t have to be sorted already. It can be in any order, and after on random shuffle there is a 1/n! probability that it will be sorted. So for the 14 element list in the video there is a 1/14! = 1/87178291200 = 1.147e-11 probability it would have been sorted after one shuffle

    • @ytg6663
      @ytg6663 2 роки тому +1

      @@maurolionelmipianoyyo11 watch pogo

    • @noahhkun5097
      @noahhkun5097 2 роки тому

      Yes this has a 0.0000001% chance (I know this is an arbitrary number) to outperform the best (but also underperforms lots of others who can also do this on the first line)

  • @persimmon93
    @persimmon93 2 роки тому +700

    You say it's not fast.
    I say it CAN be fast.

    • @rodrigoqteixeira
      @rodrigoqteixeira 7 місяців тому +1

      The guy who is employing people says otherwise. Wake up you would be crying if google used that.

    • @kevin_mitchell
      @kevin_mitchell 3 місяці тому +3

      True, it could be the fastest ever developed. Imagine one million items to sort, and it randomly gets it right the first time. No other algorithm can come even close.
      But then again, if only the first two are out of order ... mmm

    • @kevin_mitchell
      @kevin_mitchell 3 місяці тому

      @@rodrigoqteixeira Actually, I'd prefer that compared to seeing Stack Overflow at the top of the list.

    • @VDMpeniel
      @VDMpeniel Місяць тому

      LOL love it hahaha!

    • @akube5605
      @akube5605 16 годин тому

      O(1) if ur lucky

  • @natey9190
    @natey9190 4 місяці тому +71

    Everyone laughs until it gets a million entries right in a single second

  • @abhirammadhu2973
    @abhirammadhu2973 3 роки тому +923

    Python programmers on Leetcode with their one liner solutions be like😂

  • @yourcasualdeveloper
    @yourcasualdeveloper 2 роки тому +368

    Bruh, that was genius. I will do this in my algoritms exam tomorrow

  • @Temari_Virus
    @Temari_Virus 2 роки тому +100

    The caveat with this algorithm is that if the shuffle method isn't a proper pseudo-random shuffle, it may never terminate. I prefer to use cosmic sort instead, which simply waits until the list is sorted by random bit flips caused by cosmic rays. That way you only have to worry about checking if the list is sorted.
    Unbelievably, this algorithm also has an O(n) cousin, called quantum bogo sort. It uses entangled particles to randomly shuffle a list, and then checks if the list is sorted. If the list isn't sorted, it destroys the universe. In all the surviving universes, the list was sorted in constant time!

    • @the11382
      @the11382 Рік тому +3

      I guess it's faster with a particle accelerator?

  • @smaybius
    @smaybius 3 роки тому +812

    The algorithm is called bogo sort

    • @chocolatechips2805
      @chocolatechips2805 2 роки тому +19

      i thought it was bogus sort

    • @pranavnyavanandi9710
      @pranavnyavanandi9710 2 роки тому +7

      Buga buga sort.

    • @praisedare
      @praisedare 2 роки тому +30

      This isn't bogo sort. Bogo sort calculates every permutation of the given input and searches for the permutation where all the items are sorted. This is just plain random sort.

    • @rubixmc7320
      @rubixmc7320 2 роки тому

      Um ACtUAly Iz caL dah bozo Zort

    • @not.taxpayer
      @not.taxpayer Рік тому +1

      Doesnt this code do exactly that?

  • @nucukak
    @nucukak Рік тому +64

    it's like the second most popular algorithm to assemble Rubik's cube

  • @yahyasalimi3254
    @yahyasalimi3254 2 роки тому +94

    That is basically "shuffle and Inshaallah"

  • @rodrigoboaventura6004
    @rodrigoboaventura6004 Рік тому +14

    If it was bad for you, it is your fault. If you are lucky it will be always the best one. Don't blame the algorithm for your bad luck

  • @Fytrzaczek21
    @Fytrzaczek21 Рік тому +10

    Best-case complexity for unsorted array: O(1)
    Worst-case though, depending on implementation of shuffle, can be literally infinity.

    • @shubhrajit2117
      @shubhrajit2117 3 місяці тому

      Won't the worst case be O(n!)
      (assuming u r trying all possible permutations of the array)

    • @Fytrzaczek21
      @Fytrzaczek21 3 місяці тому

      @@shubhrajit2117 IF you're trying all possible permutations. Probably typical implementations in standard libraries make sure of that, but if someone made a simple shuffle function using pseudo-RNG, there may be such arrays that will never be sorted correctly.

  • @angryfish8394
    @angryfish8394 2 роки тому +71

    That's discusting, i love it

  • @Nickitoks
    @Nickitoks Рік тому +26

    "Bogosort is the slowest"
    Bogobogosort joins the chat
    Bogobogobogosort joins the chat
    Infinite family of Bogobogosorts joins the chat
    *Miracle sort joins the chat*

  • @catlovingtrio
    @catlovingtrio 6 місяців тому +1

    If you think that's the worst, you clearly haven't heard of bogobogosort.

    • @ERRORRubiksZeraBrand
      @ERRORRubiksZeraBrand 5 місяців тому

      bogobogosort is like bogosort but instead in picks 2 random numbers and shuffles them, if it's sorted then shuffle again but if it isn't sorted, it's gonna start over again

  • @garlicxi
    @garlicxi 2 роки тому +15

    Don't make fun of bogosort, he's just tryna learn!

  • @snartboy5000
    @snartboy5000 8 місяців тому +1

    oh god i heard 'shuffle' and physically recoiled

  • @dengxiaopinggaming5500
    @dengxiaopinggaming5500 2 роки тому +9

    If it works, it works

  • @luckytrinh333
    @luckytrinh333 4 місяці тому +1

    Expected runtime O(n!) 😂

  • @YerushalayimShelZahv
    @YerushalayimShelZahv 2 роки тому +14

    HAHAHA, loved this short!

  • @marble17
    @marble17 2 місяці тому

    "2 line sorting algorithm"
    The other lines:

  • @I_O_O_I
    @I_O_O_I 2 роки тому +1

    Just laughed till tears. Brilliant humour, man!

  • @kinghotcoc0
    @kinghotcoc0 Рік тому +1

    Everybody loves our little Bogo Sort

  • @ytuser4562
    @ytuser4562 Рік тому

    This is the equivalent of hitting a tree with a wrench until you make a chair.

  • @BrownSloth0
    @BrownSloth0 11 місяців тому

    Everyone is gansta until the miracle sort comes.

  • @LitoMike
    @LitoMike 4 місяці тому +1

    Miracle sort is beautiful

  • @abhishankpaul
    @abhishankpaul Рік тому

    Miracle sort: Hold my process

  • @programming5182
    @programming5182 3 роки тому +12

    Hey John! Can you please make some videos to explain in depth what is POJO in Java, interface and abstract class, how arraylist store object in Java, and object pass in method mean,...

    • @CodingWithJohn
      @CodingWithJohn  3 роки тому +8

      Thanks for the ideas! I've got one just about ready to go for this week already, but I'll definitely see if I can do one of those for next week.

  • @betareleasemusic
    @betareleasemusic Рік тому

    "...In Java. It's not fast." Man, you're roasting Java so hard.

  • @ljront3126
    @ljront3126 4 місяці тому

    You are working really hard. Just remember, you are a champion by programming,. So it counts always :))

  • @Garfield_Minecraft
    @Garfield_Minecraft Рік тому +1

    bro that's the best sorting algorithm!

  • @astronaut517
    @astronaut517 Рік тому

    My brain be like when I'm trying to solve the math problem.

  • @Brahvim
    @Brahvim 2 роки тому +39

    "Bogosort", right?

  • @CreamusG
    @CreamusG 10 місяців тому

    Miracle sort will always be my favorite sorting algorithm that theoretically can succeed.

  • @teainnit27
    @teainnit27 Рік тому +1

    BOGO SORT!
    BOGO SORT!

  • @frankkevy
    @frankkevy 2 роки тому +5

    😹😹😹That's what I call efficiency

  • @girres43
    @girres43 Рік тому

    this is the worst sorting algorithm and the best at the same time

  • @yas-sinesl9105
    @yas-sinesl9105 8 місяців тому

    This is worst than the sleep sort algorithm

  • @cmyk8964
    @cmyk8964 Рік тому

    For the record, this is called Bogo Sort.

  • @px5383
    @px5383 Рік тому

    personal favourites: 14:13 for the sounds, 20:05 bc that one is impressive and so cool

  • @dxg5659
    @dxg5659 8 місяців тому

    Randomizing the numbers until it’s sorted.
    That’s kind of BogoS.

  • @sarvasvudawant8815
    @sarvasvudawant8815 Рік тому

    I actually managed to sort a list of 10 elements within a second, I feel like a chad now

  • @otistically
    @otistically Рік тому +1

    I knew Bogo sort was slow, but not THAT slow.

  • @jkli6031
    @jkli6031 2 роки тому +1

    It is the famous Bogo sort

  • @ctoid
    @ctoid 11 місяців тому

    There must exist a universe where Bogo sort is the fastest every time

  • @apollowellstein188
    @apollowellstein188 Рік тому

    Theoretically it is both the fastest and slowest. It can be done on the first shuffle, and it can never be done.

    • @andrewsad1
      @andrewsad1 Рік тому

      There are a couple algorithms that could theoretically be faster. You got Solar Bitflip and Miracle Sort (basically the same thing) where the output _could_ become sorted before you ever check it.
      You also have Dictator Sort, where the list is already sorted and if you disagree you die, and Intelligent Design Sort where clearly the array is meant to be in this order, because there's only a 1/n! chance of this particular order occurring

  • @SusAmongUs658
    @SusAmongUs658 2 місяці тому

    Bogo Sort

  • @Atrulion
    @Atrulion 2 роки тому +4

    Well if you think about it, it could also be the fastest... if you are extremely lucky

  • @sumerrana6805
    @sumerrana6805 Рік тому +1

    ever heard of lucky sort?. does nothing, just checked whether the start is sorted, if it's not, it'll check again and hope for a miracle

  • @jasonchiu272
    @jasonchiu272 4 місяці тому

    In an alternate universe, bogo sort will sort any length data 100% of the time, and no one knows why.

  • @Par4d0xxum
    @Par4d0xxum 8 місяців тому

    I wanted to learn some programming but then i see random characters, letters and numbers and i dont want to anymore 😂

  • @yaduvanshi_Sachin45
    @yaduvanshi_Sachin45 Рік тому +1

    I'm first' time watching this channel and this guy , kasam se ye banda mereko Jony Sins lag rha hai.😂

  • @precious2383
    @precious2383 6 місяців тому

    Vsauce if he was a programmer

  • @rodrigoalvarosantosd6892
    @rodrigoalvarosantosd6892 2 роки тому +1

    You reinvented the wheel (its called bogosort)

  • @d1kgaws12
    @d1kgaws12 Рік тому

    Basically Bogo sort

  • @energy-tunes
    @energy-tunes Рік тому +1

    Surprised it took that short lol. We are looking at over 13! permutations here

  • @6c4m
    @6c4m 10 місяців тому

    Bro this is slower than my internet 💀

  • @sirmemes1225
    @sirmemes1225 Рік тому

    Ah yes, bogo sort.

  • @sheldoniusRex
    @sheldoniusRex 13 днів тому

    I can go slower. While the list is not sorted use the random number generator to select one entry to be swapped, then one different position to swap it in to, including the possibility of swapping that item back into it's original position, then output a complete change log starting from the beginning of time, then check for completion. When complete print the entire change log again ending with the sorted list.

  • @bloomface9146
    @bloomface9146 Рік тому

    When you realize this is harder to implement than the selection sort, and you did it in an interview💀💀💀

  • @algoimer5013
    @algoimer5013 Місяць тому

    There’s still worse sorting algorithms.
    Heard of one where it just does nothing until a bit magically changes its value. (It does nothing)

  • @mucookul
    @mucookul 5 місяців тому

    The best sorting algorithm is miracle sort, you just wait until the list magically sorts itself!

  • @named_account
    @named_account 3 місяці тому

    miracle sort:

  • @joeman677
    @joeman677 10 місяців тому

    Hey, but that best case!

  • @clicksandbeeps61
    @clicksandbeeps61 11 місяців тому

    Bogo sort lesgooo

  • @ygt_ylc
    @ygt_ylc Рік тому

    The sorting of second number group possibility is one out of 14!.13!. ……. To 1!. That’s INSANE and VERY HUUUUUGE.

  • @mateocucurull9598
    @mateocucurull9598 2 місяці тому

    You kind of look like Michael from Vsauce if he were normal

  • @PetrKL21
    @PetrKL21 Рік тому

    Useful skill for the dean of Greendale

  • @noah5592
    @noah5592 2 роки тому

    Ctrl + C *switches to notepad.exe* Ctrl + V

  • @santiagoferrari1973
    @santiagoferrari1973 10 місяців тому

    Bogo.

  • @AWriterWandering
    @AWriterWandering Рік тому

    The funny thing about BOGO is that it can be both the slowest and the fastest algorithm.

  • @cygniomega8322
    @cygniomega8322 Рік тому

    i mean, it has got the potential, now it just need consistency.

  • @TheCCBoi
    @TheCCBoi Рік тому

    The funny thing is, this sorting algorithm is one of the fastest with quantum computers lol.

  • @bestmanalive99
    @bestmanalive99 2 роки тому

    Gotta love bogosort

  • @Maxy.waxyyy
    @Maxy.waxyyy Рік тому

    Bogo sort XD

  • @hamidbluri3135
    @hamidbluri3135 2 роки тому

    you're amazing man 😀

  • @Wizdabest1
    @Wizdabest1 5 місяців тому

    I'd like to introduce you to miracle sort.

  • @fgvcosmic6752
    @fgvcosmic6752 3 місяці тому

    O(n²)? Nah, O(n!)

  • @erb6411
    @erb6411 2 роки тому +1

    Yeah, it's O(n) might be pretty bad but it runs in O(1) for best case!

    • @aravind2624
      @aravind2624 2 роки тому

      Newbie here.
      How does it check whether the list is sorted in O(1) time.
      Sorry if question doesn't make sense

    • @umthondo3627
      @umthondo3627 2 роки тому +1

      its not O(1) best case because it has to check the whole list first, so best is O(n)

    • @pranavnyavanandi9710
      @pranavnyavanandi9710 2 роки тому

      Wrong it's O(n) as a comment by @umthondo here point out. Worst case is infinite I would guess. Since it can go on and on like in the video shown above.

    • @pranavnyavanandi9710
      @pranavnyavanandi9710 2 роки тому

      @@aravind2624 It's O(n) actually. Since the algorithm has to loop over the entire list which is performing n steps to check if the list is in sorted condition. Check the lecture by Abdul Bari on youtube. That's how I kind of got to understanding it.

  • @unknownuserwhoisitlol8776
    @unknownuserwhoisitlol8776 2 роки тому

    bogo sort

  • @doaldodemoya9412
    @doaldodemoya9412 2 роки тому +3

    have the same algorithm but instead of sorting it puts money on my wallet lol

  • @dorisch8038
    @dorisch8038 2 роки тому

    You forgot about Bogobogosort

  • @expressproductions1458
    @expressproductions1458 2 роки тому

    Amazing! Great sorter

  • @mikethewhizz5085
    @mikethewhizz5085 Рік тому

    It might outpace other algorithms in small lists

  • @monalisacayabyab7580
    @monalisacayabyab7580 4 місяці тому

    Bogosort

  • @thedoublek4816
    @thedoublek4816 2 роки тому +1

    "Miracle Sort"?

  • @FarzanaFathima-t4e
    @FarzanaFathima-t4e 4 місяці тому +3

    Fun fact: This is called Bogo sort.
    Best case: O(n)
    Average case: O(n*n!)
    Worst case: ∞

    • @qq4954
      @qq4954 4 місяці тому

      Wouldn’t average be O(n!)

  • @DemeterVT
    @DemeterVT 2 роки тому

    It has a complexity of two to the power of n, I see why it’d take so much time.

  • @BobChess
    @BobChess 10 місяців тому

    Bogo Sort is fastest and slowest at the same time. Use your luck!

  • @hugoblommaskog7268
    @hugoblommaskog7268 Рік тому

    Could make it *actually* 2 lines by scipping the curly brackets in the while loop

    • @CodingWithJohn
      @CodingWithJohn  Рік тому

      You could make all your programs just 1 line just by replacing all the newlines with spaces: ua-cam.com/video/YpS0Jh5yqIw/v-deo.html

  • @floriangro7662
    @floriangro7662 2 роки тому +6

    Bogosort

  • @noddle045
    @noddle045 18 днів тому

    It’s not the worst sorting algorithm ever…. You could make one that never results in a sorted array, or if that’s disallowed (which would disqualify bogo sort), one that cycles through every possible permutation except the sorted one using the maximum amount of average time/memory between permutations, and then reaching the sorted array as the very last possible permutation.

  • @nayeemurrahman5228
    @nayeemurrahman5228 Рік тому

    best case scenario O(1). that should be the marketing title 🐸

  • @manosmakris8308
    @manosmakris8308 Рік тому

    The propability of sort a list of n elements by shuffling the elements until you sort the list is (1*100/n!)%

  • @spacex6997
    @spacex6997 2 роки тому

    It can be the slowest or the fastest.. if you're astronomically lucky.

  • @harshchafle2967
    @harshchafle2967 5 місяців тому

    Bogo😂Bogo

  • @d2513850
    @d2513850 Рік тому

    Use bogosort algorithm to sorting something

  • @radstevee
    @radstevee Рік тому

    Time complexity: O(SENDHELP)

  • @rplays4344
    @rplays4344 8 місяців тому

    finally a worthy enemy

  • @somebodylikesbacon1960
    @somebodylikesbacon1960 Місяць тому

    best case: O(1)
    average case: O(n!)

  • @Gladuos1
    @Gladuos1 Рік тому

    Bogo sort? More like BOZO sort. Gottem