Quick sort in 4 minutes

Quick sort in 4 minutes

Michael Sambol

8 лет назад

2,206,038 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@WhisperingWalnuts
@WhisperingWalnuts - 05.04.2022 06:05

This is great video! but, I feel the algo provided in the end is not the same as the way he was explaining.. I went ahead and wrote my code for it same way he explained:
```
class Solution:
def sortArray(self, nums: List[int]) -> List[int]:

def quicksort(nums, lo, hi):
if lo < hi:
partition_resting_point = partition(nums, lo, hi)
quicksort(nums, lo, partition_resting_point - 1)
quicksort(nums, partition_resting_point + 1, hi)

def partition(nums, lo, hi):
pivotIdx = random.randint(lo, hi)
nums[pivotIdx], nums[hi] = nums[hi], nums[pivotIdx]
pivot = nums[hi]

l_idx = lo
r_idx = hi-1
while l_idx <= r_idx:
if nums[l_idx] <= pivot:
l_idx+=1
elif nums[r_idx] >= pivot:
r_idx-=1
else:
nums[l_idx], nums[r_idx] = nums[r_idx], nums[l_idx]
l_idx+=1
r_idx-=1

nums[l_idx], nums[hi] = nums[hi], nums[l_idx]
return l_idx

quicksort(nums, 0, len(nums)-1)
return nums
```

Ответить
@呆頭-b4w
@呆頭-b4w - 05.02.2025 12:05

dude thx ur so good quick sort explain,i watch so many video but i still cant understand, ur detail explain is amazing , thx

Ответить
@SaiAbitathaDUCIC
@SaiAbitathaDUCIC - 28.01.2025 07:43

Bogo Sort is simplier.

Ответить
@ilyasiskander9325
@ilyasiskander9325 - 18.01.2025 08:40

my code will not work!!!!!!!

Ответить
@MauricioVonB
@MauricioVonB - 01.01.2025 16:13

I liked it, a clear and complete video. Thanks a lot :)

Ответить
@liormoreh8313
@liormoreh8313 - 30.12.2024 18:52

Best Quick Sort video ive ever seen!

Ответить
@dienosorpo
@dienosorpo - 21.12.2024 03:17

when you have a truly suffled list, i see no point in averaging the first, middle and last element. No difference between doing that and just averaging the first three elements. I would just pick the one that already on the right

Ответить
@noahnguyen3317
@noahnguyen3317 - 19.12.2024 04:07

Time for me to put the fries in the bag

Ответить
@dicebot9617
@dicebot9617 - 12.12.2024 22:40

That was pretty quick

Ответить
@tomekk.1889
@tomekk.1889 - 03.12.2024 17:11

Maybe this isn't the sort of thing you can explain in 4 minutes. Only someone who already understand this will know what you're talking about. Too confusing and too many steps omitted

Ответить
@Ms.maryam_7
@Ms.maryam_7 - 25.11.2024 15:35

What kind of course it’s really confusing 🚶🏻‍♀️‍➡️🧎🏻‍♀️‍➡️

Ответить
@GhostGaming-tt9tu
@GhostGaming-tt9tu - 25.11.2024 01:51

That pseudocode was very confusing

Ответить
@csstudent7175
@csstudent7175 - 22.11.2024 09:23

I wish the video was longer… You explained quite well, but I would love to learn more about the details that you didn’t get a chance to cover.

Ответить
@ebullshit
@ebullshit - 22.11.2024 00:46

I think we should pick the pivot and start comparing , value at i and pivot value not pivot is shifted at last.index and keep swaping based on element values

Pseudo Code :
QUICKSORT(A)
1: if A is empty then
2: return A
3: last← (length(A) − 1).index
4: pivot ← A[last] // Take last element as pivot
5: less ← {A[i] | A[i] ≤ pivot, i != last}
6: more ← {A[i] | A[i] > pivot, i != last}
7: return (QUICKSORT(less), pivot, QUICKSORT(more))

Ответить
@trustoryz8399
@trustoryz8399 - 21.11.2024 14:09

yea nvm

Ответить
@matedominguez2883
@matedominguez2883 - 19.11.2024 03:54

Great series man! This week is my Algorithms II exam and these helped me. Keep it up :)

Ответить
@farooqahmedkhan
@farooqahmedkhan - 11.11.2024 09:37

Let's make it simple

* - if array has single or no element, return it as already sorted
1 - select a pivot in array (best is to choose last element)
2 - maintain two set (less, greater)
3 - loop through the array till 2nd last index and move lesser or greater than pivot to respective sets
4 - return recursive(left), append pivot & append recursive(right)


pseudo logic
==========
function quickSort(arr) {
left =[], right =[]
pivot = arr[arr.length-1];
for(i=0;i<arr.length-1;i++){
if(arr[i]<pivot){
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return quickSort(left).concat([pivot]).concat(quickSort(right));
}

Ответить
@Nemesis-007
@Nemesis-007 - 07.11.2024 04:55

Ain't nothing quick about this sort.... DAMM

Ответить
@powergaming2883
@powergaming2883 - 29.10.2024 20:50

There was quiet a bit of sigma in this video wouldn't you say my fellow skibidis?

Ответить
@saharshsamir8301
@saharshsamir8301 - 25.10.2024 15:34

Why do we move the pivot to the end in the beginning?

Ответить
@saranyabalasubramanian445
@saranyabalasubramanian445 - 21.10.2024 21:13

My prof taught me this is in like 1 hour(nothing reached to my braincells)but this 4 minutes taught me well ☝️

Ответить
@difforno
@difforno - 16.10.2024 06:13

I feel like this requires some hands on experience with recursion to understand properly. If anyone doesn't, please try to do a few binary tree problems on leetcode, makes a world of difference in understanding recurson based algorithms

Ответить
@bombrman1994
@bombrman1994 - 11.10.2024 07:02

these videos are all good, but I feel like it needs to connect the dots for some of us slow brains it feels like its throws at me dots that are not full circled.

Ответить
@Bigleyp
@Bigleyp - 05.10.2024 20:00

Thanks. Pretty simple explanation.

Ответить
@andrefigueiroa3794
@andrefigueiroa3794 - 01.10.2024 02:10

If anyone is interested in the algorithm of quicksort written in javascript, i've just coded it myself when practicing the concepts of this video

```
function quickSort(arr) {
if (arr.length <= 1) return arr;
else if (arr.length === 2) {
if (arr[1] > arr[0]) return [arr[0], arr[1]];
else return [arr[1], arr[0]];
}

const pivotPosition = Math.floor(arr.length / 2);

// Swap pivot position with the end of the array
let tmp = arr[pivotPosition];
arr[pivotPosition] = arr[arr.length - 1];
arr[arr.length - 1] = tmp;

// Create left and right pointers
let left = 0;
let right = arr.length - 2;

// move left and right pointers
while (left < right) {
while (arr[left] <= arr[arr.length - 1]) {
left += 1;
}
while (arr[arr.length - 1] <= arr[right]) {
right -= 1;
}

if (left < right) {
tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
}
}

// swap pivot with left
tmp = arr[left];
arr[left] = arr[arr.length - 1];
arr[arr.length - 1] = tmp;

const sortedLeft = quickSort(arr.slice(0, left));
const sortedRight = quickSort(arr.slice(left + 1));

return [...sortedLeft, arr[left], ...sortedRight];
}
```
I've focused on didactic instead of performance:
- I have just choosed the middle element of the array as the pivot (for simplicity)
- I wasn't worrier about memory optimization, so i allowed myself to create copies of the array

feel free to rewrite the code using a better algorithm to choose the pivot, and optimize the memory usage

Ответить
@JeevaJeeva-lh3ps
@JeevaJeeva-lh3ps - 27.09.2024 08:59

bro big fan

Ответить
@Woopinah
@Woopinah - 26.09.2024 16:39

Very very good video, thank you! I really love how you never stutter over your words, and never say uhm or uhhhh. That makes this very easy to watch.

Ответить
@Garfield_Minecraft
@Garfield_Minecraft - 20.09.2024 02:59

Now this one better!

Ответить
@Shay-mw1hh
@Shay-mw1hh - 02.09.2024 23:19

I don't get it. What if the pivot has no greater than or less than itself? What numbers can it compare to?

Ответить
@GkjcAmpj-s2g
@GkjcAmpj-s2g - 02.09.2024 11:58

Alan Turnpike

Ответить
@atmajoburman7335
@atmajoburman7335 - 28.08.2024 22:20

better and much easier algo than any standard quicksort algo available in the books

Ответить
@baribari600
@baribari600 - 15.08.2024 08:00

"let's let recursion handle the rest" > "here's the rest of the owl"

Ответить
@BigSmoke-r9w
@BigSmoke-r9w - 24.07.2024 16:36

Pivot is reference. You just got to put all the values that is larger than the pivot to the right and values smaller than the pivot to the left. Thats all it is. Why are the comments overreacting??

Ответить
@CaptainMeowVlogs
@CaptainMeowVlogs - 24.07.2024 09:46

This is great for revision

Ответить
@fatjawns3671
@fatjawns3671 - 23.07.2024 19:05

In your head don't move the pivot to the end and think of it more like one big tree.

Ответить
@FirstNameLastName-zh6ud
@FirstNameLastName-zh6ud - 15.07.2024 06:30

Bro Tamil la sollunga

Ответить
@prohakerofficial
@prohakerofficial - 06.07.2024 20:02

Very good video, I learned quick sort easily thanks to this. Although, I did have to rewatch the "median-of-three" explanation.

Ответить
@Bibbatron
@Bibbatron - 04.07.2024 01:12

I remember i wrote a quicksort program understood fully the concept and 6 months later i dont even remember the concept. Quicksort is such a weird algorithm

Ответить
@palmossi
@palmossi - 25.06.2024 13:09

1,794,000th view!!!!! :D

Ответить
@vishalsubhanje490
@vishalsubhanje490 - 23.06.2024 21:47

thank you sir give me ur no i will pay 100 bucks

Ответить
@orionthegreat7858
@orionthegreat7858 - 23.06.2024 15:43

That’s a pretty slow quicksort

Ответить
@naming_is_harddd
@naming_is_harddd - 15.06.2024 12:09

Wait in (nlogn), is the log in base ten or base e?

Ответить
@srikarthickk9708
@srikarthickk9708 - 03.06.2024 21:12

I thought my dumbass couldn't understand it, then I read the comments and realised that I lack basic understanding and must work on quicksort properly. This video is just like last minute revision for us I guess.
Btw, ur videos help me understand a lot better and make me code the algorithms myself after learning the concept except this one
😅

Ответить
@KuvitusAcadamy
@KuvitusAcadamy - 01.06.2024 17:29

this made it more confusing , i thought i was close smh

Ответить
@TheoWasHere2
@TheoWasHere2 - 31.05.2024 22:18

this ain't how they explained it to us... Like, how many different quicksort are there that the way it's shown are that different from each other...

Ответить
@YashRaj-zs1oo
@YashRaj-zs1oo - 25.05.2024 19:32

How do you make these animations for your videos?

Ответить