์ด๋ฒ์๋ QuickSort Interview Question์ ๋ํ ๋ด์ฉ ์
๋๋ค.
์์ผ๋ก๋ ์ฝ๋ฉ ์ธํฐ๋ทฐ ๋ฌธ์ ๋ ํ๋ฌธ์ ์ฉ ๊ฐ์ด ํ์ด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค. ์ ์ผ ์๋์ ๊ธฐ์ถ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ๊ฐ์ด ํ์ด๋ด์!
์์ด ๊ณต๋ถ๋ ์๋๋ ์์ต๋๋ค. ๋งค์ผ ๊พธ์คํ ๋ฃ๊ณ ๋งํ๊ธฐ ์ฐ์ต ํ๋ ๊ฒ์ด ๊ฐ์ฅ ์ค์ํฉ๋๋ค.
๊ฐ๋ฐ์๋งค์ผ์์ด๋ ๋น๋ถ๊ฐ์ Cracking Coding Interview์ ์ ์๋ก ์ ๋ช
ํ Gayle Laakmann McDowell์ ์์ ๊ฐ์ข๋ฅผ ์ง์์ ์ผ๋ก ๊ณต๋ถํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
์ ์ฒด ๋ถ๋์ ๋๋ฌด ๊ธธ์ด์ ์ฃผ์๋ถ๋ถ ํ ๋ ๊ตฐ๋ฐ๋ง 1๋ถ ์ดํ๋ก ๋ฐ์ทํ์ฌ mp3ํ์ผ๋ก ๋ง๋ค๊ณ ์์ต๋๋ค.
์ฆ ํ ์ฃผ์ ๋น 1๋ถ ์ดํ ๋ถ๋์ mp3ํ์ผ์ด ํ ๋๊ฐ์ฉ ์ ๊ณต๋๊ฒ ์ต๋๋ค. ๋๋จธ์ง ๋ถ๋ถ์ ๋ฆฌ์ค๋ ์ฐ์ต ํ์๋ฉด ๋๊ฒ ์ต๋๋ค.
์ด๋ฒ์๋ ๋ฐ๋ผํ๊ธฐ ์ฝ๋๋ก ์ต๋ํ ์งง๊ฒ ์๋์ต๋๋ค.
๊ณต๋ถํ๋ 3๋จ๊ณ ๋ฐฉ๋ฒ์ ์๋์ ๊ฐ์ต๋๋ค.
1๋ถ ๋ถ๋์ ๋ฒ์ญ
์ ์ฒด ๋ฃ๊ธฐ ๋๋ฒ
๋ฌธ์ฅ ๋ฃ๊ณ ๋ฐ๋ผ ๋งํ๊ธฐ ๋๋ฒ
ํ๊ตญ๋ง๋ก ๋ฃ๊ณ ์์ด๋ก ๋งํ๊ธฐ
ํ๋ฃจ์ ํ์๊ฐ ์ด์ ๋ค์ผ๋ฉด์ ๋งํ๊ธฐ ์ฐ์ตํ๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.
*** ๊ฐ๋ฐ์ ๋งค์ผ ์์ด๋ ์ ๊ฐ ๊ฐ์ธ์ ์ผ๋ก ๊ณต๋ถํ๊ธฐ ์ํด ๋ง๋ mp3ํ์ผ์ ํน์ ๋ค๋ฅธ ๋ถ์๊ฒ๋ ๋์์ด ๋ ๊น ํด์ ๊ณต์ ํ๊ณ ์๋ ๊ฒ์
๋๋ค. ๋ถ์กฑํ ์์ด ์ค๋ ฅ์ผ๋ก ๋ฒ์ญํ ๊ฒ์ด๋ผ ์๋ชป๋์์ ์๋ ์์ต๋๋ค. ์ด์ํ ๋ถ๋ถ์ ๋๊ธ๋ก ์๋ ค ์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค. ***
mp3 ํ์ผ ๋ค์ด๋ก๋: https://drive.google.com/open?id=15vhlLRvQJPH7jLWTOPwZSwbl_8g9ieeb
์๋ณธ:
----- mp3 script -----
So now the next question is, how efficient is
this sorting algorithm? Well in an ideal world in quicksort we're dividing the
array in half each time. We pick a great pivot that really is roughly the median
and then half the elements get pivoted to one side of the array and half of them get pivoted
to the other, and then we just apply quicksort to each half. In that case we
get an n log n runtime. One quick and dirty way of seeing why this is n log n
in the good case is that each element is in, gets quicksort called on it,
log n times, and each one of those was one swap, so there's n elements and they
go through log n swaps, then I'll take n log n time overall. However in the bad
case let's imagine what happens here. We pick a really bad pivot, like every time
we pick the pivot element it happens
to be the very first element in the array or the very lowest element in that
subarray. Then we actually have n squared calls to quicksort and therefore our
runtime degenerates to O of n squared. But as long as we're smart about how we pick
the pivot element we can get a pretty efficient runtime and that's
why we typically implement quicksort in the real world. So now that you've seen
how quick sort works at a high level, let's turn to the implementation.
----- ๋ฒ์ญ ์ -----
์ด์ ๋ค์ ์ง๋ฌธ์ ์ด ์ํ
์๊ณ ๋ฆฌ์ฆ์ด ์ผ๋ง๋ ํจ์จ์ ์ด๋์
๋๋ค. ์ด์์ ์ธ ํต์ํธ๋ ๋ฐฐ์ด์ ๋งค๋ฒ ๋ฐ์ฉ ๋๋ ์ ์์ต๋๋ค. ๋๋ต ์ค๊ฐ์ ํด๋นํ๋ ์ค์ฌ๊ฐ์ ์ ํํ๊ณ ๋ฐฐ์ด์ ๋ฐ์ฉ ๋๋๊ณ ๋ ๊ฐ๊ฐ์ ๋ฐ์ ํต์ํธ๋ฅผ ์ ์ฉํ๋ฉด ๋ฉ๋๋ค. ์ด ๊ฒฝ์ฐ ์ฐ๋ฆฌ๋ n log n์ ์คํ์๊ฐ์ ์ป์ ์ ์์ต๋๋ค. ์ n log n์ธ์ง ์ ๊น ๋ณด๋ฉด ๊ฐ๊ฐ์ ์์์ ํต์ํธ๋ฅผ ์ ์ฉํ๋ฉด log n์ด๊ณ ๊ฐ๊ฐ ํ๋ฒ์ ๊ตํ์ด ํ์ํ์ฌ n ์์๋ค์ log n์ ๊ตํ์ ํ๊ณ ์ ์ฒด์ ์ผ๋ก n log n์ด ๊ฑธ๋ฆด ๊ฒ์
๋๋ค.
๋์ ๊ฒฝ์ฐ๋ฅผ ์๊ฐํด๋ด
์๋ค. ๋งค๋ฒ ์ฒซ๋ฒ์งธ ์์๋ ํ์๋ฐฐ์ด์ ์์ฃผ ๋ฎ์ ๋์ ์ค๊ฐ๊ฐ์ ์ ํํฉ๋๋ค. ๊ฒฐ๊ตญ n์ ๊ณฑ๋ฒ ํ์ํธ๋ฅผ ํธ์ถํ์ฌ O n ์ ๊ณฑ์์ด๋ผ๋ ๋์ ์คํ์๋๊ฐ ๋ฉ๋๋ค. ๊ทธ๋ ์ง๋ง ์ ์ข์ ์ค๊ฐ๊ฐ์ ๋ฝ๋๋ค๋ฉด ๊ฝค ์ข์ ํจ์จ์ ์คํ์๊ฐ์ ์ป์ ์ ์๊ณ ์ค์ ์ฐ๋ฆฌ๊ฐ ํต์ํธ๋ฅผ ๊ทธ๋ ๊ฒ ๊ตฌํํ๊ณ ์์ต๋๋ค. ์ ์ฌ๋ฌ๋ถ์ ์ด๋ป๊ฒ ํต์ํธ๊ฐ ๋์ํ๋์ง ๋๋ฝ๋ดค์ต๋๋ค. ํ๋ฒ ๊ตฌํํด ๋ด
์๋ค.
----- ์ด์ฃผ์ Interview Question (๊ฐ์ด ํ์ด ๋ด์!) -----
Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
Example 1:
Input: [3,2,1,5,6,4] and k = 2
Output: 5
Example 2:
Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4
Note:
You may assume k is always valid, 1 โค k โค array's length.
์๋ณธ๋ฌธ์ ๋งํฌ: https://leetcode.com/problems/kth-largest-element-in-an-array/description/