• 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

  • 정렬이 잘 μž‘λ™ν•˜λŠ”μ§€λ₯Ό ν™•μΈν•˜κΈ° μœ„ν•΄μ„œλŠ” λ‹€μŒμ˜ 배열을 생성해 ν…ŒμŠ€νŠΈν•΄λ³Έλ‹€.
    • μ—­μˆœμ˜ λ°°μ—΄
    • 거의 μ •λ ¬λœ λ°°μ—΄
    • 동일 μ›μ†Œλ₯Ό κ°€μ§„ λ°°μ—΄