Heap's Algorithm in JavaScript - Get All The Permutations of an Array

preview_player
Показать описание
In this video we will learn what permutations are and recreate Heap's Algorithm in Javascript. This is a function where, given an array of elements, it will give you all the permutation of the array. Learn this and level up your coding interview skills!

🗄 Resources:

🔑 Key Concepts:

- Permutations
- Recursion
- Pure Function

#Algorithm #Permutations #JavaScript
Рекомендации по теме
Комментарии
Автор

after 10 videos on this subject, I finally kind of understand

ataboywithana
Автор

thank you so much for the video !!! i really appreciate this and for ppl who wants break down in the text here is my break down based on justin's video

- g(3, [1, 2, 3])
- g(2, [1, 2, 3])
- g(1, [1, 2, 3])
- **Base case**: Push `[1, 2, 3]` to output
- g(2, [1, 2, 3]) // First iteration of the loop
- N === 2, so we swap [1, 2, 3] to [2, 1, 3]
- g(1, [2, 1, 3])
- **Base case**: Push `[2, 1, 3]` to output
- g(3, [1, 2, 3]) // Second iteration of the loop
- N === 3, swap [2, 1, 3] to [3, 1, 2]
- g(2, [3, 1, 2])
- g(1, [3, 1, 2])
- **Base case**: Push `[3, 1, 2]` to output
- N === 2, swap [3, 1, 2] to [1, 3, 2]
- g(1, [1, 3, 2])
- **Base case**: Push `[1, 3, 2]` to output
- g(3, [1, 2, 3]) // Third iteration of the loop
- N === 3, swap [1, 3, 2] to [2, 3, 1]
- g(2, [2, 3, 1])
- g(1, [2, 3, 1])
- **Base case**: Push `[2, 3, 1]` to output
- N === 2, swap [2, 3, 1] to [3, 2, 1]
- g(1, [3, 2, 1])
- **Base case**: Push `[3, 2, 1]` to output

suzu
Автор

dude. yes. this is the JS algos channel i've been looking for lol. just earned a subscriber

yangyu
Автор

Finally, someone explaining it who understands it. So many people act confident but then when it comes to the actual tricky part, they don't explain. Fantastic work.

rolandfisher
Автор

Finally after watching a bunch of videos, this was the only one that made me understand this craziness... just earned a subscriber!

GustavoVarallo
Автор

Delighted to see such a lucid explanation of a complex algorithm. I just started getting familiar with recursion and stumbled across a permutation problem with Heap's algorithm. The explanation here is as simple as it can get. Thank you for this video.

Ankur
Автор

Nice Explanation on Heap Algorithm. All students must watch. Thank you for your time
and good health.

sakarsr
Автор

OMG in Discrete Math, the number of permutations looks kinda easy to grasp, but I've never expected to list them one by one needs such a delicate and clever algo. Thank you for your explanation.

nanayang
Автор

This problem was so complicated. I saw a few explanations but this was the best one yet. The other explanations were in other languages as well so it was hard to follow. Yours was very clear with good examples. Please do more

bd
Автор

Awesome video, I hadn't seen recursion so well explained before :)

oliviameza
Автор

sir Kim this is educational for new comers

salad
Автор

2:15 "We go inside this crazy for lop" HAHAHAHA

j.villasmil
Автор

that was literally just brilliant, I got stuck at one point tho. The keyword 'return' after 'output.push' gives back the newly re-arranged array to the previous function to enter the for loop. I know you've mentioned it briefly in the explanation, but hope this helps anyone who attempt it and got stuck prior watching your explanation! I wonder tho if anyone knows whether this alogrithm relates to the principle of "fist in last out" as that seemed to be how each generate function gets slotted and executed?

LawrenceChung
Автор

I have a question at 2:57, maybe you will cover it later. If I have an array of 1, 2, 3, 4, 5 and I also want to include combinations such as 111 or 121? How come they aren't considered possible combinations here? I'm really new at this, sorry if this is a stupid question. :)

linabeckman
Автор

Is there any way to find all of the strings that are 4 in length with letters A-Z and numbers 0-9?

srdownwards
Автор

May I know, what vs code extension did you use to for a symbol like =>, ====, !== gives such a user-friendly symbol?

sanjaygalami
Автор

Excellent video. I think is the easiest to understand

estebanportillo
Автор

I dont understand how the swapped version of the array persists even after that specific function is closed out. after you make 123 into 213 and push it into the array, you go back to the original call. shouldn't that call still have the original array as 123 ? so it would change to 312 first (0 index and second index)?

johndon
Автор

Is there a way to do this where I can control the site of the sets? For example, have them get in sets of three.

bigk
Автор

Why does it passes the 17th line when n = 1, instead of simply returning from the function?

rahulbiswas