- toc {:toc}
Sorting
- ์ ๋ ฌ : ์ ํ, ๋ฒ๋ธ, ์ฝ์ , ์ด์ง, ํต, ํฉ๋ณ
Selection Sort
-
๊ฐ pass ๋ง๋ค, ๊ฐ์ฅ ์์ ๊ฐ์ ์ฐพ๊ณ ๋งจ ์์ผ๋ก ์ ์กํ๋ค.
-
Linear Search๋ก ์ํํ๋ฉด์ ๊ฐ์ฅ ์์ ๊ฐ์ ์ฐพ๊ณ ์์ผ๋ก ๋ณด๋ธ๋ค.
-
์๊ฐ ๋ณต์ก๋ = (n-1) + (n-2) + โฆ + 2 + 1 = O(N^2)
template <class ItemType>
void SelectionSort (ItemType values[ ], int numValues ){
int endIndex = numValues - 1 ;
for (int current = 0 ; current < endIndex; current++)
Swap (values[current],
values[MinIndex(values, current, endIndex)]);
}
template <class ItemType>
int MinIndex(ItemType values[ ], int start, int end){
int indexOfMin = start ;
for(int index = start + 1 ; index <= end ; index++)
if (values[ index] < values [indexOfMin])
indexOfMin = index ;
return indexOfMin; // ์ต์๊ฐ์ ๊ฐ๋ ์ธ๋ฑ์ค๋ฅผ ๋ฐํ
}Bubble Sort
- ๊ฐ pass ๋ง๋ค, ๋์์๋ถํฐ ์์ผ๋ก ์์ฐจ์ ์ผ๋ก ๋น๊ตํ๋ฉด์ ์ ์กํ๋ค.
- ์๊ฐ ๋ณต์ก๋ : (n-1) + (n-2) + โฆ + 2 + 1 = O(N^2) (ํญ์ ๊ทธ๋ฐ ๊ฒ์ ์๋๋ค.)
template<class ItemType>
void BubbleSort(ItemType values[], int numValues){
int current = 0;
while (current < numValues - 1){
BubbleUp(values, current, numValues-1);
current++;
}
}
template<class ItemType>
void BubbleUp(ItemType values[], int startIndex, int endIndex){
for (int index = endIndex; index > startIndex; index--)
if (values[index] < values[index-1])
Swap(values[index], values[index-1]);
}Insertion Sort
- ๊ฐ pass ๋ง๋ค, ํค๊ฐ์ ํ๋ ์ ํํ๊ณ ํค๊ฐ์ด ์ฌ๋ฐ๋ฅธ ์์น๋ก ์ด๋ํ๋๋ก ๋ง๋ ๋ค.
- ์๊ฐ ๋ณต์ก๋ : O(N^2)
template <class ItemType>
void InsertionSort (ItemType values[ ], int numValues )
{
for (int count = 0 ; count < numValues; count++)
InsertItem (values , 0 , count);
}
template <class ItemType>
void InsertItem (ItemType values[ ], int start, int end){
bool finished = false;
int current = end;
bool moreToSearch = (current != start);
while (moreToSearch && !finished){
if (values[current] < values[current - 1]){
Swap(values[current], values[current - 1);
current--;
moreToSearch = ( current != start );
}
else
finished = true ;
}
}Heap Sort
- ํ ์ ๋ ฌ
- ์ ๋ ฌ๋์ง ์์ ํธ๋ฆฌ๋ฅผ ํ ์์ฑ์ด ๋ง์กฑํ๋๋ก ์ ๋ ฌํ๋ค.
- ๋ฃจํธ๋ฅผ ๋นผ๊ณ ํ ์์ฑ์ ๋ง์กฑํ๋๋ก ์ ๋ ฌํ๋ ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค.
- ์๊ฐ ๋ณต์ก๋ : (N/2) x O(logN) + (N-1) x O(logN) = O(NlogN)
template <class ItemType>
void HeapSort (ItemType values[ ], int numValues ){
int index ;
for (index = numValues/2 - 1; index >= 0; index--)
ReheapDown ( values , index , numValues - 1 ) ;
// Sort the array.
for (index = numValues - 1; index >= 1; index--){
Swap (values [0] , values[index]);
ReheapDown (values , 0 , index - 1);
}
}Quick Sort
- ๊ธฐ์ค์ ์ค์ ํ๊ณ ๊ธฐ์ค๊ณผ ๋น๊ตํด ์์ ๊ฒ, ํฐ ๊ฒ์ ๋ถ๋ฅํ๋ค.
- ์ผ์ชฝ์์, ์ค๋ฅธ์ชฝ์์ ๊ธฐ์ค์ ์ค์ ํ๊ณ ๋ถ๋ฅํ๋ ์์ ์ ๊ณ์ํ๋ค.
- ์๊ฐ ๋ณต์ก๋
- ๊ธฐ์ค์ ๊ณ์ํด์ ์ค๊ฐ ๊ฐ์ ๊ฐ๋ ๊ฒฝ์ฐ : O(NlogN)
- ๊ธฐ์ค์ด ํ์ชฝ ๋์ผ๋ก ๋ชฐ๋ฆด ๊ฒฝ์ฐ, ์ต์ : O(N^2)
template <class ItemType>
void QuickSort (ItemType values[ ], int first, int last )
ascending order
{
if (first < last){
int splitPoint ;
Split (values, first, last, splitPoint);
QuickSort(values, first, splitPoint - 1);
QuickSort(values, splitPoint + 1, last);
}
}Merge Sort
- ํฉ๋ณ ์ ๋ ฌ : ๊ณ์ ๋ถํ ํ ํ ํฉ์น ๋ ๋น๊ตํ์ฌ ์ ๋ ฌํ๋ค.
- ์๊ฐ ๋ณต์ก๋ : O(NlogN)
template <class ItemType>
void MergeSort (ItemType values[ ], int first, int last )
{
if (first < last){
int middle = ( first + last ) / 2;
MergeSort(values, first, middle);
MergeSort(values, middle + 1, last);
Merge(values, first, middle, middle + 1, last);
}
}Testing
- ์ ๋ ฌ์ด ์ ์๋ํ๋์ง๋ฅผ ํ์ธํ๊ธฐ ์ํด์๋ ๋ค์์ ๋ฐฐ์ด์ ์์ฑํด ํ
์คํธํด๋ณธ๋ค.
- ์ญ์์ ๋ฐฐ์ด
- ๊ฑฐ์ ์ ๋ ฌ๋ ๋ฐฐ์ด
- ๋์ผ ์์๋ฅผ ๊ฐ์ง ๋ฐฐ์ด