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
μ λ ¬μ΄ μ μλνλμ§λ₯Ό νμΈνκΈ° μν΄μλ λ€μμ λ°°μ΄μ μμ±ν΄ ν
μ€νΈν΄λ³Έλ€.
μμμ λ°°μ΄
κ±°μ μ λ ¬λ λ°°μ΄
λμΌ μμλ₯Ό κ°μ§ λ°°μ΄