#1 ์๊ณ ๋ฆฌ์ฆ
#1-1

์ด๋ค ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ก์ ๊ฒ์ด๋๋ ์๋ฌด๋๋ ์๊ด์๋ค. ์ฌ๊ธฐ์์๋ ๊ฐ ๋ฐฐ์ด์ ๋งจ ๋ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ก๊ธฐ๋ก ํ๋ค.โจโจํน์๋ผ๋ ๊ธฐ์ค์ ๋๋ต์ ์ผ๋ก๋ผ๋ ์์๋ค์ ํ๊ท ๊ฐ์ ๊ฐ๊น๊ฒ ์ก์ ์ ์๋ ์กฐ๊ฑด์ด ์กด์ฌํ๋ค๋ฉด, ํด๋น ์กฐ๊ฑด์ ์ด์ฉํด ๊ธฐ์ค์ ์ก๊ฒ๋ ์์ฉํ ์๋ ์์ ๊ฒ์ด๋ค. ๊ธฐ์ค์ด ํ๊ท ๊ฐ์ ๊ฐ๊น๊ฒ ์กํ์๋ก, ํต ์ ๋ ฌ์ ์ฑ๋ฅ์ด ์ข์์ง๊ธฐ ๋๋ฌธ์ด๋ค.
#1-2

์ด '๋ถํ (partition)'์ ๊ฒฐ๊ณผ๋ก ๊ธฐ์ค ์์์ ์๋ฆฌ๊ฐ ์ต์ข ์ ์ผ๋ก ๊ฒฐ์ ๋๋ฉด, ๊ทธ ์์๋ ์์ผ๋ก์ ๋ชจ๋ qucikSort() ์ฌ๊ท ํธ์ถ์์ ์ฌ์ฉ๋์ง ์๊ณ ์์ํ ๊ทธ ์๋ฆฌ๊ฐ ๊ณ ์ ๋๋ค. ์ฆ, ๋ถํ ํ ๋ฒ์ ์์ ํ๋๊ฐ ์ ๋ ฌ๋๋ ๊ฒ์ด๋ค.
#1-3
ํต ์ ๋ ฌ - ์ํค๋ฐฑ๊ณผ, ์ฐ๋ฆฌ ๋ชจ๋์ ๋ฐฑ๊ณผ์ฌ์
์ํค๋ฐฑ๊ณผ, ์ฐ๋ฆฌ ๋ชจ๋์ ๋ฐฑ๊ณผ์ฌ์ . ํต ์ ๋ ฌ(Quicksort)์ ์ฐฐ์ค ์คํฐ๋ ๋ฆฌ์ฒ๋ ํธ์ด๊ฐ ๊ฐ๋ฐํ ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ์ด๋ค. ๋ค๋ฅธ ์์์์ ๋น๊ต๋ง์ผ๋ก ์ ๋ ฌ์ ์ํํ๋ ๋น๊ต ์ ๋ ฌ์ ์ํ๋ค. ํต ์ ๋ ฌ์ n๊ฐ์ ๋ฐ
ko.wikipedia.org
ํต ์ ๋ ฌ์ ์ด ๋น๊ต ํ์ ๊ณ์ฐ์ ํด๋น ๋งํฌ๋ก ๋์ฒดํ๋ค.
#2 ์ฝ๋
#2-1 ์๋ฐ
public static void quickSort(int[] array, int startIndex, int endIndex) {
// (startIndex < endIndex)์ ์๋ฏธ: ๋ฐฐ์ด์ ํฌ๊ธฐ๊ฐ 2์ด์์ด๋ค.
if(startIndex < endIndex) {
int pivotIndex = partitionAroundPivot(array, startIndex, endIndex);
quickSort(array, startIndex, (pivotIndex - 1));
quickSort(array, pivotIndex, endIndex);
}
}
// endIndex๊ฐ pivot(๊ธฐ์ค)์ ์ญํ ์ํ
private static int partitionAroundPivot(int[] array, int startIndex, int endIndex) {
int endOfLower = startIndex - 1;
for(int i = startIndex; i < endIndex; i++) {
if(array[i] <= array[endIndex]) {
// array[endIndex]๋ณด๋ค lower์ธ ๊ฐ ์ ์ฅํ ๊ณต๊ฐ ํ๋ณด๋ฅผ ์ํด endOfLower++
endOfLower++;
// array[i]์ array[endOfLower]์ ๊ฐ ์๋ก ๊ตํ
int temp = array[endOfLower];
array[endOfLower] = array[i];
array[i] = temp;
}
}
// array[endOfLower] ๋ฐ๋ก ๋ค์ array[endIndex] (= ๊ธฐ์ค) ๊ฐ์ด ์ค๊ฒ ํ๊ธฐ ์ํด์ endOfLower++
endOfLower++;
// array[endIndex]๊ณผ array[endOfLower]์ ๊ฐ ์๋ก ๊ตํ
int temp = array[endOfLower];
array[endOfLower] = array[endIndex];
array[endIndex] = temp;
return endOfLower;
}
#2-2 ์ฝํ๋ฆฐ
fun quickSort(array: Array<Int>, startIndex : Int, endIndex : Int) {
// (startIndex < endIndex)์ ์๋ฏธ: ๋ฐฐ์ด์ ํฌ๊ธฐ๊ฐ 2์ด์์ด๋ค.
if(startIndex < endIndex) {
val pivotIndex = partitionAroundPivot(array, startIndex, endIndex)
quickSort(array, startIndex, (pivotIndex - 1))
quickSort(array, pivotIndex, endIndex)
}
}
// endIndex๊ฐ pivot(๊ธฐ์ค)์ ์ญํ ์ํ
fun partitionAroundPivot(array: Array<Int>, startIndex : Int, endIndex: Int) : Int {
var endOfLower = startIndex - 1
for (i: Int in startIndex..<endIndex) {
if (array[i] <= array[endIndex]) {
// array[endIndex]๋ณด๋ค lower์ธ ๊ฐ ์ ์ฅํ ๊ณต๊ฐ ํ๋ณด๋ฅผ ์ํด endOfLower++
endOfLower++
// array[i]์ array[endOfLower]์ ๊ฐ ์๋ก ๊ตํ
val temp = array[endOfLower]
array[endOfLower] = array[i]
array[i] = temp
}
}
// array[endOfLower] ๋ฐ๋ก ๋ค์ array[endIndex] (= ๊ธฐ์ค) ๊ฐ์ด ์ค๊ฒ ํ๊ธฐ ์ํด์ endOfLower++
endOfLower++
// array[endIndex]๊ณผ array[endOfLower]์ ๊ฐ ์๋ก ๊ตํ
val temp = array[endOfLower]
array[endOfLower] = array[endIndex]
array[endIndex] = temp
return endOfLower
}
#3 ์์ฝ
ํต ์ ๋ ฌ์ ํ๋ฒ ์ํ๋ ๋๋ง๋ค ๊ธฐ์ค ์์ ํ๋๋ฅผ ์ ๋ ฌํ๋ค.
#4 ์ด ๊ฐ๋ ์ด ์ฌ์ฉ๋ ๊ธ
28417 - ์ค์ผ์ดํธ๋ณด๋
#1 ์๊ณ ๋ฆฌ์ฆ ํต ์ ๋ ฌ (Quick Sort) #1 ์๊ณ ๋ฆฌ์ฆ ์ด๋ค ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ก์ ๊ฒ์ด๋๋ ์๋ฌด๋๋ ์๊ด์๋ค. ์ฌ๊ธฐ์์๋ ๊ฐ ๋ฐฐ์ด์ ๋งจ ๋ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ก๊ธฐ๋ก ํ๋ค. ํน์๋ผ๋ ๊ธฐ์ค์ ๋๋ต์ ์ผ๋ก๋ผ๋
kenel.tistory.com
'๊นจ์ ๊ฐ๋ ๐ > ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๊ทธ๋ํ - ๊ทธ๋ํ์ ์ข ๋ฅ์ ํํ (0) | 2024.01.09 |
---|---|
๋์ ํ๋ก๊ทธ๋๋ฐ (Dynamic Programming), ์ํฅ์(Bottom-up) ๋ฐ ํํฅ์(Top-down) ์ ๊ทผ (0) | 2024.01.04 |
์ ๋ ฌ - ์ฝ์ ์ ๋ ฌ (Insertion Sort) (0) | 2023.11.29 |
์ ๋ ฌ - ํ ์ ๋ ฌ (Heap Sort) (0) | 2023.11.20 |
์ ๋ ฌ - ๋ณํฉ ์ ๋ ฌ (Merge Sort) (1) | 2023.11.14 |