Merge Algorithms - 2-Way and K-Way Merge

Say in a programming interview situation, you are given two sorted arrays of integers and asked to merge them. What would you do? I'm pretty sure my first instinct would be to append them. Which then I would realize that the new array would be no longer be properly sorted. The correct approach to this problem is to use a 2-way merge.
2-way merge is quite simple. You take two sorted arrays. You compare the first element in each array and then take the smaller one and put it into a separate union array. And you keep doing this until you have fully merged the two arrays. This will end up having a nice O(n) time complexity, as you only do one comparison for each element.
Now imagine that instead of two arrays, you are given six arrays. Enter k-way merge. Merging k sorted arrays using an optimal approach like using a heap is called k-way merge. However, you can use tournament trees for optimum time, which will result in Θ(n logk) time complexity. If you want to see full visualization of 2-way and k-way merge in action, check out the video. The entire video is animated, as I believe that animations are the best way to comprehend sorting and merging algorithms.
Outline of this video:
* 0:00 Overview
* 0:22 2-Way Merge
* 0:50 K-Way Merge
* 2:44 Merge Algorithms Time and Space Complexities
* 4:04 Relationship of K-Way Merge and Distributed Computing
* 5:33 What Are the Caveats of K-Way Merge w/ Tournament Trees?
* 5:51 Tips!
Merge algorithm source code:
* github.com/soygul/QuanticDev/tree/master/algorithms/merge
Further reading:
* en.wikipedia.org/wiki/Merge_algorithm
* en.wikipedia.org/wiki/K-way_merge_algorithm
* www.geeksforgeeks.org/tournament-tree-and-binary-heap/
If you want to read or contribute, you can find this guide on:
* quanticdev.com/algorithms/merge
My Algorithms Playlist:
* kzread.info/head/PLlPRnMzqjADqDZFBqdwzbIjf71h8xYs4x
- - - - - - - - - - -
quanticdev
quantic_dev
quanticdev.com
- - - - - - - - - - -
Next Video:
K-way merge using tournament trees is one of the fundamentals of streaming data processing and distributed computing. For instance, distributed sorting (external sorting). In my next algorithm video, I will solve an interview question asked by Google during a Senior Reliability Engineering interview. In that question, you are given a 1 TB of data and 1000 computer nodes with only 1 GB of RAM each. And you are asked to sort this data as fast as possible while utilizing the given computer nodes as efficiently as possible. There are several ways to solve this problem, and k-way merge using tournament trees is one of the optimal solutions. In that video, I will go over all possible solutions. If you don't want to miss it when it is out, don't forget to sub.

Пікірлер: 20

  • @QuanticDev
    @QuanticDev3 жыл бұрын

    If you prefer fully animated algorithm videos over classroom style of explanations, let me know and I will do future algo videos accordingly.

  • @snehgupta1814

    @snehgupta1814

    3 жыл бұрын

    Please sir do this for Graph Tree too... and recursive based algos.. Thanks!!!

  • @QuanticDev

    @QuanticDev

    3 жыл бұрын

    @@snehgupta1814 I will definitely do. I'm currently writing a React.js based visualizer tool, after that I should be able to visualize most algorithms with little effort.

  • @cissecuba
    @cissecuba3 жыл бұрын

    Amazing Video. Helped me a lot!

  • @nataliskvortsova4592
    @nataliskvortsova45923 жыл бұрын

    thank you! It is very good visualization!

  • @aytunch
    @aytunch3 жыл бұрын

    Great explanation and choice of subject. Please make more viedos like this with animations.

  • @QuanticDev

    @QuanticDev

    3 жыл бұрын

    Animations is the way. Much easier to explain algos this way, and much easier to understand. I will do all my algo videos using animations from now on.

  • @sanjayroutela
    @sanjayroutela2 жыл бұрын

    Great video. Can you please add one more video covering k-way merge but without heap and tournament trees.

  • @todaysmotivation9692
    @todaysmotivation96922 жыл бұрын

    awesome

  • @user-oy4kf5wr8l
    @user-oy4kf5wr8l3 жыл бұрын

    omg.......i watched ur video to solve one problem...then one comes to another... another lol........this is infinitive........

  • @QuanticDev

    @QuanticDev

    3 жыл бұрын

    Algo questions always have that fun factor in them. Edutainment in short.

  • @user-oy4kf5wr8l

    @user-oy4kf5wr8l

    3 жыл бұрын

    @@QuanticDev ur video about sliding windows is the best on KZread, great thanks! If it's possible, could you please do some interviews for different levels of people, like 1 - 2 years of experience, 3+.years . I'm gonna have a interview soon. I am so nervous... 😭😭😭😭

  • @QuanticDev

    @QuanticDev

    3 жыл бұрын

    @@user-oy4kf5wr8l First 3-4 rounds of interviews will be the same for everyone. Seniors will get to endure systems design, network security, etc. sort of specialized extra rounds of interviews. So the general advice about interviews will apply to everyone. Just continue your algo practice every day. Good luck!

  • @AAa57295
    @AAa572952 жыл бұрын

    thanks a lot

  • @QuanticDev

    @QuanticDev

    Жыл бұрын

    Happy to help

  • @3x6249
    @3x6249 Жыл бұрын

    if those arrays are not sorted, why not just combine all of them and apply quick sort?

  • @user-oy4kf5wr8l
    @user-oy4kf5wr8l3 жыл бұрын

    Buddy, thank you. maybe more videos about interview. Thank u in advance!

  • @QuanticDev

    @QuanticDev

    3 жыл бұрын

    I will recreate some of the interviews from top companies in the future, including all the questions. Thanks for the suggestion!

  • @user-oy4kf5wr8l

    @user-oy4kf5wr8l

    3 жыл бұрын

    @@QuanticDev one more question plz, u mentioned that it's a good idea to write some tests during the interview. How? On the whiteboard, I just write sth like AssertThat( method (112)). isEqualsTo(5)?

  • @QuanticDev

    @QuanticDev

    3 жыл бұрын

    @@user-oy4kf5wr8l Yes pretty much, especially for corner cases. Then you should actually verify that assertion is correct. This shows that you care about tests, and might help you find a bug.

Келесі