Why the FASTEST Sorting Algorithm can(t) be O(N)!

Sdílet
Vložit
  • čas přidán 4. 07. 2024
  • What is the fastest sorting algorithm known to man? We find out by attempting to find an O(n) time complexity sorting algorithm. Until now, the sorting research has found O(n*log(n)) to be the best.
    We discuss concepts of time complexity, decision-making, and how the best algorithm is bounded by the permutation tree of an array. A final trick is used to reduce complexity to O(N)!
    N! Approximation: en.wikipedia.org/wiki/Stirlin...
    log(N) video: • Why does log(N) appear...
    00:00 Introduction
    00:19 Prerequisites
    00:40 Sorting Algorithms Comparison
    01:32 Building a Permutation Tree
    04:35 Finding the best permutation
    05:45 Time Complexity Analysis
    07:21 Proof through Contradiction
    08:08 Parallel Sorting!
    09:25 Thank you!
    System Design Video Course:
    interviewready.io
    Along with video lectures, this course has architecture diagrams, capacity planning, API contracts, and evaluation tests. It's a complete package.
    Sources:
    hackernoon.com/timsort-the-fa...
    stackoverflow.com/questions/3...
    stackoverflow.com/questions/2...
    #sorting #algorithm #sortingalgorithm

Komentáře • 1K

  • @mohitthorat8580
    @mohitthorat8580 Před 4 lety +180

    You said to express a number we require atleast Log(n) operation. But we don't include this in the Time complexity analysis of bubbe sort, still it's completely is O(N square). Why? Shouldn't it be more than N square?

    • @gkcs
      @gkcs  Před 4 lety +114

      I think this part of the video is the most misunderstood bit, and I take responsibility for not communicating my thoughts accurately:
      A number takes logN bits to represent. When we choose a position for it to be placed, the index will take logN bits to represent. Each bit requires a decision to be made -> 0 or 1. Hence we make logN decisions for deciding a single position amongst N positions.
      This is how I came to the series logN + logN - 1 + logN - 2 + ... + 1
      Bubble sort goes through the numbers ahead of it. Each comparison is assumed to be an O(1) operation. This makes sense because the computer hardware can compare 32/64 bits in a single operation, and we rarely sort numbers larger than that.
      If you sort an array of arbitrarily large numbers, the complexity of bubble sort will be O(N^2* log(N)). The same as bubble sorting strings of size L: O(N^2 * L).
      If L is small enough or constant, the complexity reduces to O(N^2), since the factor of L is ignored.

    • @mohitthorat8580
      @mohitthorat8580 Před 4 lety +13

      @@gkcs Makes sense. Thanks!

    • @noasmr46
      @noasmr46 Před 4 lety +5

      Gaurav Sen why does a number take LogN bits to represent? If bits are only 0 and 1 won’t a number like 255 need 8 bits (11111111)? But log(255) base ten is 2 so only 2 bits to represent?

    • @khannom1693
      @khannom1693 Před 4 lety +1

      @@noasmr46 I think he meant log base 2 of N

    • @gkcs
      @gkcs  Před 4 lety +18

      @@noasmr46 A bit is a digit in the binary world. A decimal digit is a digit in the decimal world. You are mixing the two.
      Binary? log(N) to the base 2.
      Decimal? log(N) to the base 10.
      Check out: czcams.com/video/Xe9aq1WLpjU/video.html

  • @souravkumarcode
    @souravkumarcode Před 4 lety +2149

    This is what clickbait looks like for computer science students.

  • @TonKcedua
    @TonKcedua Před 2 lety +192

    Actually, the Faith Sort's time complexity is O(1) - you just take the input array, put all your faith in it being already sorted, and return it. Worst case scenario whatever god you believe in takes care of the rest.

  • @SongSoundGood
    @SongSoundGood Před 5 lety +1700

    There is a sorting algorithm working in O(n) called eliminationsort. You eliminate every element that is out of order. By the end, you have a sorted list.

  • @ubermensch9678
    @ubermensch9678 Před 4 lety +432

    Ever heard of Schrodinger's + Heisenberg's sort? 🤩
    The array is already sorted as long as.......
    You don't look the array 😂

    • @raunvk
      @raunvk Před 4 lety +2

      Brilliant XD

    • @ron-davin
      @ron-davin Před 4 lety +1

      lul

    • @ryzen980
      @ryzen980 Před 3 lety +1

      smart!

    • @pi_rand4266
      @pi_rand4266 Před 3 lety +3

      Best comment

    • @vineeth505
      @vineeth505 Před 3 lety +9

      It is both sorted and unsorted at the same time until you look at it to be precise😂
      Cool one though!

  • @MeFreeBee
    @MeFreeBee Před 5 lety +907

    The trick to faster sorting is to always buy your data pre-sorted!

    • @gkcs
      @gkcs  Před 5 lety +25

      Hahaha!

    • @Rahul-Nalawade
      @Rahul-Nalawade Před 5 lety +8

      This is where he could have talked about best case for Insertion Sort.

    • @khhnator
      @khhnator Před 4 lety +4

      is a joke, but that's pretty much what high performance programs try to do: push all extraneous calculations to before you actually needs to run the program

    • @maciejmalewicz9123
      @maciejmalewicz9123 Před 4 lety +30

      Assumption sort. Assume its sorted, return.

    • @kongchan437
      @kongchan437 Před 4 lety +5

      @@maciejmalewicz9123 ----- I go one better - I do not even have to assume it is sorted. I will just declare it sorted already, in the order of whatever the list is already showing.... hey, why bother to sort at all … you get whatever you see...by the time we spent discussing all these optimal sorting techniques, the brute force program code would have run the job many times over, right ? :-)

  • @tusharsingh2439
    @tusharsingh2439 Před 4 lety +224

    pregnant woman: (hits blunt) it wont affect my child.
    the child: let's use bubble sort.

    • @JeffHykin
      @JeffHykin Před 3 lety +9

      Actually, at the extremely low level of an L1 CPU cache (very small lists, very small data), bubble sort takes the least amount of real time. It's faster (in memory) to access and compare cached nearby elements than it is to go get elements from RAM and put them in the cache.

    • @dr_davinci
      @dr_davinci Před 3 lety +2

      Nah, he be using bogosort.

    • @tusharsingh2439
      @tusharsingh2439 Před 3 lety +1

      @@dr_davinci delete sort, better

    • @purple.requiem
      @purple.requiem Před 3 lety

      @@tusharsingh2439 no its worst sort

    • @Draxper
      @Draxper Před 3 lety +2

      @@JeffHykin no, for small lists, insertion sort works best. As a matter of fact quicksort is also supposed to use insertion sort once partitions are small enough

  • @tjoloi
    @tjoloi Před 5 lety +217

    "O(n) is not possible"
    Do you want to learn about our lord and savior Sleep Sort?

    • @gkcs
      @gkcs  Před 5 lety +6

      Hahahaha 😝

    • @khhnator
      @khhnator Před 4 lety +7

      i stand by the word of Gravity sort

    • @satibel
      @satibel Před 3 lety +3

      in all seriousness, O(n) and even O(log(n)) time are possible with parallel sorting networks.
      though they can only sort a fixed n, if you need a really fast sort for n

    • @dale116dot7
      @dale116dot7 Před 3 lety

      Radix sort I think is O(n). It works really well if you drop your deck of FORTRAN cards and you need to get them back in order.

  • @SlimThrull
    @SlimThrull Před 5 lety +606

    Random Sort is able to do it in O(n). It just doesn't happen very often. ;)
    C'mon, you know you deserved this comment for that title. :P

    • @gkcs
      @gkcs  Před 5 lety +10

      Hahaha!

    • @ivosu4936
      @ivosu4936 Před 5 lety +12

      I thought it is in O(1)

    • @gkcs
      @gkcs  Před 5 lety +17

      @@ivosu4936 You need to verify the guess too 😛

    • @ivosu4936
      @ivosu4936 Před 5 lety +23

      @@gkcs or you can just roll with it, cause in one of manny parallel universes it is sorted

    • @dhruvilpatel856
      @dhruvilpatel856 Před 5 lety +3

      You might be knowing this, but time complexity has to taken considering all cases instead of just best cases

  • @Yash-_-777
    @Yash-_-777 Před 3 lety +41

    youtube guy explaining fastest sort algo:
    me simply using .sort() method in python : KODER!!!

    • @ItachiUchiha-ub2iu
      @ItachiUchiha-ub2iu Před 3 lety +7

      That .sort() in Python is using Timsort algo developed by Peter Tim, having worst and best case as n*log(n).

    • @Yash-_-777
      @Yash-_-777 Před 3 lety +1

      @@ItachiUchiha-ub2iu yes that's similar to merge sort I Guess

    • @nehalgupta79
      @nehalgupta79 Před 3 lety +2

      O(1) for sure m8

  • @bruhe3178
    @bruhe3178 Před 4 lety +159

    Good content but bad clickbait ):
    The fastest sorting algorithm should have been O(1): By pointing to an empty list.
    An empty list is always sorted.

    • @gkcs
      @gkcs  Před 4 lety +1

      Hahaha!

    • @ShubhamKumar-sq2pg
      @ShubhamKumar-sq2pg Před 4 lety +4

      or a list with only one element ;)

    • @ryzen980
      @ryzen980 Před 3 lety

      lol!

    • @rajtiwari2308
      @rajtiwari2308 Před 3 lety +2

      We are talking about the worst case here, Don't even write a program it will be O(0) 😂

  • @BikashKumar-pz8hc
    @BikashKumar-pz8hc Před 3 lety +9

    I totally and absolutely love the creative treatment.
    This video always brings up a smile.
    What's more the explanation is clear to an non-computer science student like me.
    Great work Gaurav Sen.

  • @nitanshu.v
    @nitanshu.v Před 5 lety +8

    Thanks!! I really enjoyed the way you explained the concept.

  • @arwahsapi
    @arwahsapi Před 4 lety +4

    Dozens of years of my life as a programmer, I mostly use Sort() method in almost C# collections. I never care how it works. You have explained the subject very well mate. Thanks

  • @JAYPRAKASH-uy8rg
    @JAYPRAKASH-uy8rg Před 3 lety +6

    My senior who works at a MNC suggested me your channel for system design videos, and trust me if I am saying that this channel has great great content for budding fresher's.
    A big fan of yours 🙌🏻

  • @gkcs
    @gkcs  Před 5 lety +474

    Hey everyone! This video is meant to explain why O(n) is impossible for a sorting algorithm.
    I try to keep metadata as relevant as possible, but I have to acknowledge market realities. The titles have to be catchy and we all love drama. Thanks for all your feedback. Cheers!

    • @keshavlakhotia1432
      @keshavlakhotia1432 Před 5 lety

      Gaurav, i didn't get that part of god at 5:18 , i think you mixed two N's
      Like first u said if N is the single number u need logN time to say it to god , then after some time u mixed that logN with the original N i.e. number of elements in an array . Isn't it??

    • @gkcs
      @gkcs  Před 5 lety +3

      Yes, but they are the same in this case. When we choose an element to put into the sorted array, we are effectively telling God an index from the original array. This index can take values from 0 to N-1, which is N choices.
      So we need to say a number upto N, which takes log(N) time. And this number N is equal to the number of elements in the array.
      Once a element is removed, we have N - 1 choices, and God can now be told any number between 0 to N-2 to place in the second position. This requires log(N-1) time. And so on.

    • @keshavlakhotia1432
      @keshavlakhotia1432 Před 5 lety

      @@gkcs Ok, so just like insertion sort but rather than iterating for that min/max value god will help us , nice!!

    • @gkcs
      @gkcs  Před 5 lety +1

      Yup!

    • @5nine838
      @5nine838 Před 5 lety

      Hey , can u plz explain or share source why radix sort is not of order n , and what about bucket sort

  • @gopishivakrishna9707
    @gopishivakrishna9707 Před 4 lety +4

    haha.. laughed at the start looking at so many YOU.. Simple and neat explanation... I've been watching your videos lately and guess what ? You have a Subscriber ;)

  • @janhavisavla9654
    @janhavisavla9654 Před 5 lety +20

    Thanks to your videos. You're one of the reasons I'm placed today, that too within 15 days since the inception of placement season in my college. I used to suck at coding, now I can safely say that I'm amongst the top 20 coders of my class(PS: my college(VJTI,Mumbai) has very good coders ) . I am placed in Accolite (which recruits only 60 engineers all over India , I was amongst the 6 people they chose out of 300 students in a pool campus process,went through 1 written coding round , 3 gruesome tech rounds and one HR round ). I went till the last round of Morgan Stanley (top 5,but they chose only 2 sadly) and your system design videos helped me a lot through the process.
    Now I've started competitive programming and I'm enjoying it a lot.
    From a person who didn't get a single internship last year, to being amongst the first students to be placed this year. You've inspired me.
    Thank you!
    Keep up the good work! :)

    • @gkcs
      @gkcs  Před 5 lety +2

      I am super impressed Janhavi! Congrats! 😍

  • @LucidProgramming
    @LucidProgramming Před 5 lety +296

    Hi Guarav. I really like your content, and this video is no exception. I understand the marketing realities present on CZcams force one to make "clickable" titles. I really don't want to see smart channels like the one you run devolve into the Buzzfeed of CZcams. Granted, this is a far cry from the typical clickbait, but I just don't find those tactics to be genuine.
    I wouldn't take the time to comment on other channels employing these same tactics, I just feel like the quality of content you usually produce is above the clickbait tactics. Hope you take this as constructive criticism, and not as a personal attack.

    • @gkcs
      @gkcs  Před 5 lety +17

      I will, thanks for the feedback! 😁

    • @nirmitjoshi5741
      @nirmitjoshi5741 Před 5 lety +11

      He has mentioned that in comment section. Don't watch it if you don't find it useful. Gaurav keep it up I loved this video. Why do we learn this ? 😎😎😂😂😂

    • @vedprakash-bw2ms
      @vedprakash-bw2ms Před 5 lety +3

      This was a click-bait.

    • @manasgunda1421
      @manasgunda1421 Před 3 lety

      Yeah click-bait, but good video

  • @codediva007
    @codediva007 Před 5 lety +1

    Thank you very much Gaurav, I was surfing for this and you gave the best concise explanation. Cheers to your efforts behind this!

    • @gkcs
      @gkcs  Před 5 lety

      Thank you!

  • @nomads27
    @nomads27 Před 5 lety +10

    Nicely explained, but that God part is little confusing, instead of that it would be better to say,
    1) Comparisons will be at-most height of your decision tree
    2) no of leaf nodes of this tree would be n! i.e. no. of permutation possible
    3) and h >= log(no. of leaf node)

  • @reethikavanaparthy6984
    @reethikavanaparthy6984 Před 5 lety +16

    I didn't ever thought of this concept....tq for making such a good video and sharing

  • @arielfuxman8868
    @arielfuxman8868 Před 4 lety +3

    I did not understand the part at which you started saying "you can represent a number with log n +1
    What does it have to do with the permutation?

  • @go2bose
    @go2bose Před 5 lety +1

    Brilliant explanation of the derivation of fastest algo for sorting

  • @BryanDieudonne
    @BryanDieudonne Před 5 lety +5

    I love the content! Thank you!

  • @sagnikb7
    @sagnikb7 Před 4 lety +3

    I came here for System Designs ! But omg now I have started learning logarithms, such a wonderful video ... especially where u prove fast sort cannot exist!

    • @gkcs
      @gkcs  Před 4 lety +1

      Thank you!

  • @ankurxshukla
    @ankurxshukla Před 5 lety +1

    This guy makes most precise videos..

  • @funkymunky1776
    @funkymunky1776 Před 3 lety

    You are amazing bro I wanna learn programming because of your content and I’ve been active with user interface design and you changed my life I hope you know that

  • @farruhhabibullaev5316
    @farruhhabibullaev5316 Před 2 lety +11

    More correctly, I would say "comparision based sorting algorithms best run time is nlog(n)" but integer based sorting algorithms such as counting, radix & bucket sorts are run in order of N asympoticially. But not quite O(n), the best one is O(n*sqrt(loglogn)) but it's hot research area at the moment, we can't claim it's impossible without any proof.

    • @Matyanson
      @Matyanson Před 2 lety +2

      Where can I find any info on this? What is the name of the concept algorithm?

  • @victorcaldentey6295
    @victorcaldentey6295 Před 5 lety +3

    Best intro ever in an Algorithms video!

  • @anmolmishra1914
    @anmolmishra1914 Před 5 lety +2

    Well, this was really interactive.
    Editing was better this time. I liked your idea. :)

  • @alkatiwari6650
    @alkatiwari6650 Před 3 lety +1

    I subscribed it immediately. I am glad that CZcams recommended this. I have seen your video with Anshika Gupta where you were talking that " Agar 8 semester me nahi padha to kya padha". You are too sarcastic. Really liked the way you are teaching. 🙏 Thankyou Gaurav Sir

  • @aranjan
    @aranjan Před 5 lety +24

    Ohh .... That was really cool 😍
    Can you discuss the multiprocessor part to reduce the complexity to O(logN) again ?

    • @gkcs
      @gkcs  Před 5 lety +3

      I'll try in one of the future videos 😋

    • @shubhygups
      @shubhygups Před 4 lety +2

      ​@@gkcs if we use N number of processors likewise you said logn number processors to achieve O(N) can't we achieve O(logn) like this? why we need infinite processors than? or similarly nlogn processors to achieve O(1) time complexity to sort array (which is obviously impractical) than why using infinite processors we still achieved O(logn) time.
      Thanks

    • @DanielNyong
      @DanielNyong Před 4 lety

      @@shubhygups That's not practical lol unless you have 4 items to sort. Then bogo sort isn't even that bad.

  • @nabanjand
    @nabanjand Před 4 lety +5

    Well, any comparison sort where one compares the elements to each other cannot be done in less than O(nlogn) time!
    Non-comparison sort like Bucket Sort can be in O(n)
    (The special conditions required for bucket sort are:
    1) The values to be sorted are evenly distributed in some range min to max.
    2) It is possible to divide the range into N equal parts, each of size k.
    3) Given a value, it is possible to tell which part of the range it is in.)

  • @nonoobott8602
    @nonoobott8602 Před 3 lety

    For me, I loved the explanation cos I'm kinda new to sorting algorithms. Thanks for sharing

  • @sunnyyasser5625
    @sunnyyasser5625 Před 5 lety +1

    Wooaah! Keep growing man!!

  • @Lastmomenttuitions
    @Lastmomenttuitions Před 5 lety +33

    great video bro

  • @SameerKhan-qy7ot
    @SameerKhan-qy7ot Před 5 lety +13

    Me: Trying to print output correctly.
    GS: Today we'll sort in O(n). :D

  • @dhruvreshamwala511
    @dhruvreshamwala511 Před 4 lety +1

    Tricked me into understanding something more fundamentally. Not even mad.
    Great explanation !

    • @gkcs
      @gkcs  Před 4 lety +1

      Thanks!

  • @sanketdeshpande4216
    @sanketdeshpande4216 Před 5 lety +2

    Best video 👍 thanks for sharing your knowledge

  • @kushalchordiya7229
    @kushalchordiya7229 Před 5 lety +10

    This was a great video
    On a completely unrelated side note, you look like kanan gill and biswa Kalyan rath had a child.

    • @gkcs
      @gkcs  Před 5 lety +1

      Hahahaha

  • @AshirwadPradhan007
    @AshirwadPradhan007 Před 5 lety +21

    I liked the old saas-bahu "No no no" part.... btw Great work buddy!

    • @gkcs
      @gkcs  Před 5 lety +3

      Hehehhuhuhuhahahhaha! Thanks!

  • @madhukalapala9673
    @madhukalapala9673 Před 5 lety +2

    Good video got some insights on this minimal time complexity

  • @baltazarus3307
    @baltazarus3307 Před 5 lety

    Great video, man! Keep it up!

  • @vinnieworks1854
    @vinnieworks1854 Před 3 lety +5

    That was me when I learnt sorting yrs ago lol “ why are we learning this?”

  • @keshavlakhotia1432
    @keshavlakhotia1432 Před 5 lety +74

    Gaurav Sen found a friend good in video editing XD

    • @gkcs
      @gkcs  Před 5 lety +62

      One of the Gaurav Sen's in the video helped me :P

    • @keshavlakhotia1432
      @keshavlakhotia1432 Před 5 lety

      😆

    • @subho3651
      @subho3651 Před 5 lety +10

      @@gkcs very soon we will ask you to teach us video editing.

    • @blasttrash
      @blasttrash Před 5 lety +9

      No he learnt shadow clone jutsu from naruto. :P

  • @Market_Majnu
    @Market_Majnu Před 4 lety

    Nice way sir u driven out the technique to learn with fun

  • @ky5069
    @ky5069 Před 5 lety +1

    Nice video Gaurav, but I did not understand the transition from the usage of log10 for digit counting to log2 usage when unravelling the factorial. Would the digit count example be applicable for base 2 numbers?
    Or is the O(n log2 n) the minimum time complexity because you have to do 2 choices for each item (either stay put or move in relation with its compared item)?
    Thanks!

    • @gkcs
      @gkcs  Před 5 lety

      The base doesn't matter in order complexity analysis. log10 n = (log2 n)/(log2 10).
      1 / (log2 10) becomes a constant factor.

  • @shivukotihal3366
    @shivukotihal3366 Před 4 lety +4

    This is high level punch for cs students. I will prove you wrong just because of this video. Jai mahishmathi

  • @ezpz4akash
    @ezpz4akash Před 5 lety +6

    State Space Search To Prove This! Awesome 🤘

  • @RamizZamanJEEPhysics
    @RamizZamanJEEPhysics Před 5 lety

    The first few seconds cracked me up.... Very nice video

  • @carlavirhuez4785
    @carlavirhuez4785 Před 2 lety

    Oh my god! So clear and easy! Thank you very much!!

  • @gamingbutnotreally6077
    @gamingbutnotreally6077 Před 5 lety +6

    I can make an Unsorting algorithm in O(1) 😜 Great video Gaurav!

  • @HCTripleC
    @HCTripleC Před 5 lety +64

    Your proof is valid however it only applies to comparison-based sorting, as those algorithms are reliant on relativity between one another.
    Since non-comparison based sorting are all based on the foundation that you can sort a data set with no knowledge of any other piece of data except for the one which you are ordering.
    It is this fact that permits non-comparison based sorting to have a better worst-case time than O(n log n).
    The limit for a non-comparison based sort would be O(n) since you would have to access each data member at *LEAST* once during the sort.
    Proof:
    Let X(n) be an unordered data set of n items, and let Y(n) represent the ordered data set of X(n)
    Let H(v) be an ideal hash function which maps a member of X(n), v, to its corresponding index in Y(n)
    If O(H(v)) = O(1), then the number of steps needed to perform to create Y(n) would be:
    S = O(H(X(1))) + O(H(X(2))) + . . . + O(H(X(n)))
    = O(1)_1 + O(1)_2 + . . . + O(1)_n
    = n * O(1)
    = O(n)

    • @benswanson4807
      @benswanson4807 Před 5 lety +2

      yasss

    • @gkcs
      @gkcs  Před 5 lety +10

      That's correct, thank you for pointing it out :)

    • @NitishRaj
      @NitishRaj Před 5 lety

      Thanks Homer Calcalavecchia sir.

    • @khushitshah979
      @khushitshah979 Před 5 lety +2

      What about collisons in hash function.. that can end up in O(n) instead of O(1) so we need completely collision proof hash function👍👍👍. To achieve these and i don't think there exit one(not sure)

    • @neensta5404
      @neensta5404 Před 5 lety +7

      @@khushitshah979 he already stated .." 'Ideal' hash function"

  • @newways9263
    @newways9263 Před 5 lety +1

    great video..keep sharing your knowledge:)

  • @shrshk7
    @shrshk7 Před 4 lety +2

    Though it's a clickbait he proved that there's a mathematical limit to how fast we can sort with some help from wikipedia article, I'd say he's smart.

  • @sterinsaji4513
    @sterinsaji4513 Před 5 lety +20

    Why are we learning this hehehe😂😂😂thug life

  • @pratikbhagwat95
    @pratikbhagwat95 Před 5 lety +7

    Ssly yooo... What the heck.. Had lot of expectations from fast sort in the beginning.. Anyway that was really an amazing way to prove that O(n) is nt possible while sorting.

    • @arnabsom3251
      @arnabsom3251 Před 5 lety

      Its possible just get a quantum computer you can do it in O(root(n)) time

    • @dale116dot7
      @dale116dot7 Před 3 lety

      I think radix sort is O(n), though that is the running time in a mechanical sorting machine if you discount the gathering of the cards and resetting the machine to run the next digit. Though if you have 100,000 cards sequentially numbered then put out of order, I think there should be 500,000 comparisons and five gatherings.

  • @shreya3862
    @shreya3862 Před 3 lety

    Absolutely loved your video! 🔥

  • @infnik87
    @infnik87 Před 4 lety +2

    You forgot to maintain an important point. The lower bound is Ω(nlogn) for comparison sorts. It can be less than that for other classes of sorting algorithms.

  • @heatvisuals
    @heatvisuals Před 5 lety +3

    Sir. Love this video.
    Pimp daddy max!

  • @vedant6633
    @vedant6633 Před 4 lety +10

    Radix sort can practically do it in O(n)

    • @nikolatasev4948
      @nikolatasev4948 Před 2 lety

      He just proved mathematically that Radix sort does not exist! At 8:04.
      Who are you going to believe, him or your lying eyes?

  • @davidjames1684
    @davidjames1684 Před 5 lety

    One "simple" enhancement to a sorting algorithm that only grabs the largest (or smallest) number each pass is to grab both. For example. suppose we had ten scrambled numbers to sort between 1 and 10 (such as 7,4,6,1,8,10,5,3,9,2). The "hi-lo" sort algorithm would make a pass thru all 10 numbers and would record the position of the lowest and highest numbers (position 4 = 1, position 6 = 10). It would then put the 7 in temp memory (since we need to move the 1 there) and would also store the 2 in temporary memory (since we need to move the 10 there). The new array (after the first pass) would be 1,4,6,7,8,2,5,3,9,10. Now we can sort the subarray (4,6,7,8,2,5,3,9) since we know the 1 and 10 are already in the proper positions. I am calling this the hi-lo sort. It is a good start to try to improve on such as maybe try picking off the 2 highest and the 2 lowest numbers each pass.
    I realize this is somewhat similar to the cocktail shaker sort except that the direction can always be low to high index in the array of numbers, thus perhaps making it slightly easier to implement.

  • @nitishkumar-py9ru
    @nitishkumar-py9ru Před 5 lety +4

    Nice proof. Never thought in this direction. You explain like biswa Kalyan rath , so I it was more fun.

    • @gkcs
      @gkcs  Před 5 lety

      Hahaha, I get that a lot 😛

  • @tthtlc
    @tthtlc Před 5 lety +7

    Yes, many different algorithms all can given O(n) performance in the "best case" scenario as listed out here: en.wikipedia.org/wiki/Sorting_algorithm
    but most of the time, this is not achievable. So best we can get is average performance, which is usually worst than O(n).

  • @vadirajjahagirdar2615
    @vadirajjahagirdar2615 Před 5 lety

    Hi Gaurav, I like your explanation. Thanks for such a knowledge transfer. It was really interesting.

  • @poosaipandiv4947
    @poosaipandiv4947 Před 3 lety

    Broo first time I see your video
    Really enjoyed and well understand the concept.
    I think this is the first video which is watched without skipping.

    • @gkcs
      @gkcs  Před 3 lety

      Thank you!

  • @codexhammered007
    @codexhammered007 Před 5 lety +6

    Woaah. You got me with the title. I guess python's "list.sort()" or quite famously known as "timsort" is faster than any other sorting module present in any language. Is it so?

    • @gkcs
      @gkcs  Před 5 lety

      I am taking a video on Timsort next 😋

    • @NarendraPathai
      @NarendraPathai Před 5 lety +1

      Java has opted for timsort as well

  • @vishalchauhan9832
    @vishalchauhan9832 Před 5 lety +3

    Awesome intro and Amazing lecture sir !

  • @mohammedsalmaninamdar4951

    Highly informative , especially the beginning and the repetitive edits near the end😁

  • @ujjal147
    @ujjal147 Před 5 lety +1

    Thanks CZcams for the recommendation.

  • @WittyGeek
    @WittyGeek Před 5 lety +12

    Saw this JIT!! Nice video explaining why O(n) sorting doesn't exists.
    P.S: The introduction was next level swag!! Keep such things more as it makes it more interesting.

  • @htxdy
    @htxdy Před 4 lety +5

    If you define a constant value for the log(n) variable it will be n log C, no matter how big the C is, the time complexity will always be O(n)
    h3h3h3

    • @htxdy
      @htxdy Před 4 lety +1

      Thats basically how counting sort and radix sort works also

  • @StewieGriffin
    @StewieGriffin Před 5 lety

    do you store the index of the unsorted array into the sorted array and just replace the index from the sorted array with the position of the unsorted array?

  • @shyamkachhadiya4964
    @shyamkachhadiya4964 Před 5 lety +2

    Hey bro damn nice😂😂.....seriously love such videos

  • @1Maklak
    @1Maklak Před 3 lety +6

    The fastest pairwise comparison algorithm is O(N*log(N)) and the proof is similar to what you've shown. Basically, a comparison can be enough information to reject about half of remaining possible permutations of numbers.
    Radix sort (using counting and indexes, not buckets) is the fastest in practice for tens of thousands or more elements and leaves other sorting algorithms in the dust. It IS almost linear in practice, since an array of 32 bit numbers requires just 4 passes, while 64 bit numbers require 8 passes. It is also pretty cache-friendly. Mathematically, n! can be less than or equal to a^b for some a,b

  • @Aditya-us5gj
    @Aditya-us5gj Před 4 lety +5

    so it's something like as we can't travel at O(speed of light) similarly we can't sort an algorithm with complexity less than O(nlog(n)) 😁😁

    • @gkcs
      @gkcs  Před 4 lety +1

      I made a supposedly "clickbait" video on this. So I took another dig at it 😛

    • @Aditya-us5gj
      @Aditya-us5gj Před 4 lety

      @@gkcs hahaha u r simply amazing !!

  • @Believer01
    @Believer01 Před 5 lety +2

    Very well done
    Nice work

  • @bharatbhushanreddy8185
    @bharatbhushanreddy8185 Před 5 lety +1

    Very interesting gaurav Bhai !

  • @bhanuthapliyal1694
    @bhanuthapliyal1694 Před 5 lety +5

    7:30 nice prank

  • @arglebargle17
    @arglebargle17 Před 3 lety +4

    This past weekend, just to keep myself sharp, I benchmarked a bubble sort, a quick sort and a radix sort on varying numbers of random integers. The bubble sort did as badly as expected. But on 1 million elements, the radix sort was 5 times faster than the quick sort. Essentially you're saying that I can't do ... what I did. Now, you didn't say "fastest comparison sorting algorithm." Arguably the radix sort is in a different class in that it's not comparing the values directly against the others. But that's not the point: the data ends up sorted, and in 1/5 the time of what you're saying is the fastest theoretically possible method.
    Somebody said that it couldn’t be done
    But he with a chuckle replied
    That “maybe it couldn’t,” but he would be one
    Who wouldn’t say so till he’d tried.
    So he buckled right in with the trace of a grin
    On his face. If he worried he hid it.
    He started to sing as he tackled the thing
    That couldn’t be done, and he did it!
    I get a little weary of people who say things can't be done. I've already done so many things that can't be done.

    • @umairkhancis
      @umairkhancis Před 2 lety

      Very True @Argle
      @Gaurav your videos are great but may be this needs a little review because as @Argle mentioned Counting Sort or Radix Sort are on a different model of computation i.e Direct Access Array Model in contrast with comparison model of computation.
      What you mentioned in your video that Counting Sort and Radix Sort due to big range can take effectively NlogN time but representing numbers in tuple format to minimize the range of keys and then sort them using Direct Access Array Paradigm will effectively make it O(N) - in my understanding!
      Multiple passes of sorting tuples is a constant factor because we can know how many passes we will require for the given array.
      I think this topic/claim demands an in-depth follow up video, what do you think?
      Lecture from MIT 6.006 would definitely help here!
      czcams.com/video/yndgIDO0zQQ/video.html

  • @ryankanno2562
    @ryankanno2562 Před 3 lety

    Awesome video! Thank you!

  • @paleflesh3588
    @paleflesh3588 Před 4 lety +1

    is it possible to use quantum computing to create a better sorting algorithm?

  • @ShankhaShubhra
    @ShankhaShubhra Před 5 lety +16

    Did i just watch a 9 min video just to get trolled at the end? ☹️

  • @onpoint1260
    @onpoint1260 Před 5 lety +3

    it was just 13th second and you earned my like, well nice content, thanks for sharing

    • @gkcs
      @gkcs  Před 5 lety

      Thank you!

  • @lalithsamanthapuri2055
    @lalithsamanthapuri2055 Před 5 lety +1

    Wow..! though I am from "VLSI Physical Design" background I like "Algorithms" a lot..Really you are doing a great job bro..
    All the best bro...

    • @gkcs
      @gkcs  Před 5 lety

      Thanks Lalith! 😁

  • @mehulkumar3469
    @mehulkumar3469 Před 4 lety

    It's not a clickbait he really sorted our concepts very fast

  • @T33K3SS3LCH3N
    @T33K3SS3LCH3N Před 3 lety +3

    The true O(1) sort is Intelligent Design Sort: assume the list was created by an almighty creator and is therefore already perfect as it is. Done.

  • @editpapa9671
    @editpapa9671 Před 4 lety +4

    Me : don't even know how to use while loop properly
    Watching ** fastest sorting method**

  • @MrAmitkv
    @MrAmitkv Před 4 lety

    I couldn't understand at 5.25sec.
    When we say - 3 for 1st position, it takes log3 time not log(n) times. Here 3 can be any number and need not be always of n digits. What am I missing?
    Is it like, when we are saying 1st number, we are saying whole list of n numbers along with it?

  • @sreeragmsudheesh
    @sreeragmsudheesh Před 4 lety

    At 7:00 we can only do that if the base of log is 2. But in this case we took it as 10 right? So how is that even possible? Correct me if i'm wrong.

  • @mattzahara9310
    @mattzahara9310 Před 5 lety +25

    You may achieve O(n) time if you do not rely on a comparison based sort. Radix and bucket sort are both O(n) in time complexity. Technically they are
    O(n * log(n)). There is an important distinction to make though. With merge sort and quick sort, the base of the logarithm is 2, while the base of the big O notation described previously is base 10. This is important because in big O analysis, constant factors are essentially ignored. Therefore the log(n), read "log base 10 of n", is ignored in the runtime analysis, giving you a runtime of O(n).
    This is a short explanation, but please to not listen to this man. Runtime analysis always refers to a single processor, ignoring cock speed and core count. This is because these factors will improve all performance across the board, and runtime analysis, specifically big O runtime analysis, is interested in the worst case running time of an algorithm.

    • @itsjustboarsley
      @itsjustboarsley Před 5 lety +5

      Top comment material.

    • @bogdanstankovic3022
      @bogdanstankovic3022 Před 5 lety +14

      Gotta love that "cock speed" and the big O. Otherwise a good comment :).

    • @angelowentzler9961
      @angelowentzler9961 Před 5 lety +1

      log(n), no matter the base, is not a constant, therefore NOT ignored.
      The actual base of the log IS irrelevant, since logA(n) = C*logB(n) for some constant C depending on the values of A and B (eg. log(a) = 2.303*ln(a)).
      When you have polynomial complexity, lower powers than the highest in the polynomial are irrelevant. eg N^3+N^2 boils down to N^3, which is why we speak of O(nlog(n)) rather than O(nlog(n)+n+C).
      Under no circumstance can you say O(nlog(n)) boils down to O(n)

  • @PRIMEVAL543
    @PRIMEVAL543 Před 3 lety +8

    0:07 am I racist or are they all the same guy?
    XDD

    • @gkcs
      @gkcs  Před 3 lety +4

      Hey, that's racist! 😂

  • @siddharthnandava
    @siddharthnandava Před 4 lety

    Hey you are doing well. And I really like this thing. Can you make a video which can explain time complexity and space complexity both?

  • @peddipankaj437
    @peddipankaj437 Před 5 lety

    For the 1st element to be sorted, it takes log(n) time? How? How is it related to number of digits when we r talking about array here..?

  • @sabriath
    @sabriath Před 5 lety +3

    O(n) can be achieved.....a random number generator is created and seeded randomly, using that, the full set of the sort can be unraveled in order based on the seed cycle. Destroy all universes that are wrong, and we're done.

    • @gkcs
      @gkcs  Před 5 lety +1

      That's interesting!
      Of course, only if we can destroy all the universes in O(n) or lesser...

    • @bhaswanthgudimella344
      @bhaswanthgudimella344 Před 4 lety +1

      Didn't get it could please explain it more specifically if possible with a short example

  • @GRBtutorials
    @GRBtutorials Před 4 lety +3

    Next up, do the slowest sorting algorithm ever. Bogosort is slow, but there are slower ones! I've personally designed what I believe is the slowest optimal (without doing unnecessary steps) sorting algorithm ever, QuantumSort:
    1. Check if data is sorted and if it is goto 3.
    2. Goto 1.
    3. Return data and exit.
    How does it work? Simple: over time, random quantum events (such as cosmic rays) will modify the data in RAM until it's sorted.

    • @gkcs
      @gkcs  Před 4 lety

      Interesting. Won't the heat death of the universe happen first?

    • @GRBtutorials
      @GRBtutorials Před 4 lety +2

      Gaurav Sen Most likely yes

  • @meable3763
    @meable3763 Před 3 lety

    Does Bogo sort have a potential of O(1) in the best case scenario or is it still O(n)?

  • @rohitsinghgautam
    @rohitsinghgautam Před 4 lety

    I just wrote a sorting suit and to compare fastest algorithm. Insertion sort is fast only if you avoid one of extra comparison, each comparison matters. Quick sort perform well for sorted reverse sort was slowest, it can be easily to workaround to make it fastest soft, this added one addition comparison in each iteration, but overall performance is excellent.

  • @saurabhsharma7123
    @saurabhsharma7123 Před 5 lety +4

    That Thug Life start was really amusing!