Heap Sort is Way Easier Than You Think!

Поделиться
HTML-код
  • Опубликовано: 26 окт 2024

Комментарии • 32

  • @GregHogg
    @GregHogg  3 месяца назад +5

    Master Data Structures & Algorithms For FREE at AlgoMap.io!

  • @daviddong9916
    @daviddong9916 3 месяца назад +16

    I got multiple offers after watching your videos. Now I’m just watching you 😅

  • @geekwithabs
    @geekwithabs 3 месяца назад +13

    "How come someone so smart is also so handsome" - all the chicks 😂

  • @Bajuvare14
    @Bajuvare14 3 месяца назад +2

    I would have needed that 3 Days ago for an exam, youtube algorithm you have failed me

  • @ankitrajxd
    @ankitrajxd 3 месяца назад +1

    "Handsome programmer like myself"🗿

  • @HariHaran-x5r2r
    @HariHaran-x5r2r 3 месяца назад +6

    O(n) is faster than O(n logn ) right...?

    • @SzrIsTaken786
      @SzrIsTaken786 3 месяца назад +3

      yup this video was a bad example for why heap sort is good

    • @abcdE_OO7007
      @abcdE_OO7007 3 месяца назад

      to find the max value of an array using max heap it's O(1) since it's the root element.

    • @SzrIsTaken786
      @SzrIsTaken786 3 месяца назад

      @@abcdE_OO7007 u missed the point. The heapify algorithm (how an array is turned into a heap) is O(n log n) so the big O will be O(n log n)

    • @abcdE_OO7007
      @abcdE_OO7007 3 месяца назад +2

      @@SzrIsTaken786 after that, everytime it would take o(1)[if elements did not change] or O( log n ) , while in ordinary array it would take o(n).
      For repeated action the efficiency is much better.

    • @fahadalam9105
      @fahadalam9105 3 месяца назад

      ​@@abcdE_OO7007once the array is sorted it will only take O(1) to get that element??

  • @rohan637
    @rohan637 8 дней назад

    Why not do a merge sort (O n log n) and take the first element? It's same as heap sort right?

    • @CompPerson
      @CompPerson 4 дня назад

      Time-wise it's the same, but plain merge sort requires O(n) space, and in-place merge sort becomes much more complicated than heap sort to implement and still requires either some extra space or a speed penalty

  • @hlubradio2318
    @hlubradio2318 3 месяца назад +2

    Still has no time

  • @yugjha6798
    @yugjha6798 3 месяца назад +2

    How

  • @amartyapanja2235
    @amartyapanja2235 3 месяца назад +3

    I wonder if merge sort is better than this. Also good work appreciate your knowledge

    • @arenmee540
      @arenmee540 3 месяца назад +2

      they're the same time complexity so in that metric, they're equivalent

    • @amartyapanja2235
      @amartyapanja2235 3 месяца назад +1

      @@arenmee540 right 👍 thanks

    • @adityaanuragi6916
      @adityaanuragi6916 3 месяца назад +1

      They're both n log n so technically they scale the same, but in heap sort we need to do an additional scan through the array which doesn't change much but is a bit of an extra step
      Maybe that's why most languages implement merge sort in their sort functions/methods in their language

  • @tejashsingh2060
    @tejashsingh2060 3 месяца назад +2

    Merge sort also have N*logN

  • @CripsyBanks
    @CripsyBanks 3 месяца назад

    Or I can use the max()

  • @jewsefjones
    @jewsefjones 3 месяца назад

    Question isn't your code o(n) space as you create a new list of size n?

    • @hello_there79
      @hello_there79 3 месяца назад +1

      ya, shouldn't space complexity be O(n) for result array?

    • @alxjones
      @alxjones 3 месяца назад +1

      It says right on the screen, space complexity of his implementation is O(n), but O(1) is _possible_ with a more complex implementation not shown here.

    • @jewsefjones
      @jewsefjones 3 месяца назад

      @@alxjones exactly why talk a o(1) solution then not give it to us :(

    • @D0CTOR-ZED
      @D0CTOR-ZED 3 месяца назад +1

      You can just use the space the array is already using if you want O(1) space. You can treat a flat array as a heap with a root at 0 and child nodes at 2n+1 and 2n+2.

    • @alxjones
      @alxjones 3 месяца назад

      @@jewsefjones The O(1) solution is just the implementation of a heap on the existing array. To show that implementation would not really be possible in a short, but if you write your own version of heapify and heappop to happen in place, his solution becomes the O(1) one.

  • @nirmalgurjar8181
    @nirmalgurjar8181 3 месяца назад

    How its O(1) ?

    • @Ibra_
      @Ibra_ 3 месяца назад

      You're not using extra space it's the same array even if it looks like a tree you can actually make it an array where node k has 2k and 2k + 1 as its children and node j has j/2 as its parent