Worst Sorting Algorithm Ever -

Поділитися
Вставка
  • Опубліковано 27 кві 2021
  • Complete Java course: codingwithjohn.thinkific.com/...
    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.com/bogosort-s...
    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.audibletrial.com/johnclean...
    🖥️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/donate/?hosted...
    ☕Complete Java course:
    codingwithjohn.thinkific.com/...
    codingwithjohn.com
    #Shorts
  • Наука та технологія

КОМЕНТАРІ • 267

  • @abhirammadhu2973
    @abhirammadhu2973 2 роки тому +3176

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

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

      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 роки тому +19

      @@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 Рік тому +617

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

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

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

    • @kevin_mitchell
      @kevin_mitchell 22 дні тому +1

      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 22 дні тому

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

  • @natey9190
    @natey9190 2 місяці тому +33

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

  • @abhirammadhu2973
    @abhirammadhu2973 2 роки тому +905

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

  • @smaybius
    @smaybius 2 роки тому +801

    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 Рік тому +28

      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 Рік тому

      Um ACtUAly Iz caL dah bozo Zort

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

      Doesnt this code do exactly that?

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

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

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

    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?

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

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

  • @yahyasalimi3254
    @yahyasalimi3254 Рік тому +88

    That is basically "shuffle and Inshaallah"

  • @user-br4nv1mx8n
    @user-br4nv1mx8n 11 місяців тому +23

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

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

    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 Рік тому +8

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

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

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

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

      @@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

  • @user-he2bo4zg9c
    @user-he2bo4zg9c 2 місяці тому +3

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

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

      Wouldn’t average be O(n!)

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

    oh god i heard 'shuffle' and physically recoiled

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

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

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

    If it works, it works

  • @ohgodpleasehelpme4337
    @ohgodpleasehelpme4337 Рік тому +4

    If 🤞was a sorting algorithm

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

    HAHAHA, loved this short!

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

    "Bogosort", right?

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

    Everybody loves our little Bogo Sort

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

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

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

      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

  • @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.

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

    😹😹😹That's what I call efficiency

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

    Just laughed till tears. Brilliant humour, man!

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

    BOGO SORT!
    BOGO SORT!

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

    Expected runtime O(n!) 😂

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

    bro that's the best sorting algorithm!

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

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

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

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

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

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

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

    Miracle sort is beautiful

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

    This is worst than the sleep sort algorithm

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

    It is the famous Bogo sort

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

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

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

    Everyone is gansta until the miracle sort comes.

  • @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 9 місяців тому

      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

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

    Miracle sort: Hold my process

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

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

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

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

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

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

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

    Vsauce if he was a programmer

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

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

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

    Bogosort

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

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

  • @mateocucurull9598
    @mateocucurull9598 7 днів тому

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

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

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

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

    For the record, this is called Bogo Sort.

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

    You reinvented the wheel (its called bogosort)

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

    Basically Bogo sort

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

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

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

    Bogo sort lesgooo

  • @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

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

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

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

    Bogo.

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

    miracle sort:

  • @TheCCBoi
    @TheCCBoi 9 місяців тому

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

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

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

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

    you're amazing man 😀

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

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

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

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

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

    Bogo sort XD

  • @bloomface9146
    @bloomface9146 9 місяців тому

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

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

    Hey, but that best case!

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

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

  • @fgvcosmic6752
    @fgvcosmic6752 23 дні тому

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

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

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

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

    Bogosort

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

    Gotta love bogosort

  • @yaduvanshi_Sachin45
    @yaduvanshi_Sachin45 9 місяців тому +1

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

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

    Bro this is slower than my internet 💀

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

    I'd like to introduce you to miracle sort.

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

    bogo sort

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

    Amazing! Great sorter

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

    Ah yes, bogo sort.

  • @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

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

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

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

    You forgot about Bogobogosort

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

    What theme did you use for this video?

  • @saurabh7943
    @saurabh7943 9 місяців тому

    when elements are sorted How the program know it is sorted and stop ?
    \

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

    Useful skill for the dean of Greendale

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

    I love this -- Hilarious😂

  • @lucasc1035
    @lucasc1035 28 днів тому

    what if you execute this code in ia ??

  • @GGysar
    @GGysar 7 місяців тому

    Nah, there are worse ones, solar bit flip sort for example or my invention (dunno if someone else already came up with that), error sort, you take two devices and send the array back and forth between the 2 without an error correction code and check if it is sorted.

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

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

  • @AWriterWandering
    @AWriterWandering 9 місяців тому

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

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

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

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

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

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

    The best sorting Algo out there is miracle sort, just while(!sorted){} and you just hope and pray that some particle (or many particles) interfere with the system in just the right way to sort the list

  • @Ahmad-jc7by
    @Ahmad-jc7by Рік тому

    😂😂😂😂 super fast 😂

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

    Use bogosort algorithm to sorting something

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

    finally a worthy enemy

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

    You look so much like luke from polymathy, I thought he tarted teaching computer science all of a sudden lol.

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

    nah you forgot about quantum bogosort

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

    This is not a sorting algorithm. We can call this just; I'm feeling lucky😂

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

    "Miracle Sort"?

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

    Bogo😂Bogo

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

    How many shuffles did those 5 hours take?

  • @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.

  • @halfbakedpotatoe6351
    @halfbakedpotatoe6351 2 роки тому +2

    This is evil