• 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

  • ์ •๋ ฌ์ด ์ž˜ ์ž‘๋™ํ•˜๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ์˜ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด ํ…Œ์ŠคํŠธํ•ด๋ณธ๋‹ค.
    • ์—ญ์ˆœ์˜ ๋ฐฐ์—ด
    • ๊ฑฐ์˜ ์ •๋ ฌ๋œ ๋ฐฐ์—ด
    • ๋™์ผ ์›์†Œ๋ฅผ ๊ฐ€์ง„ ๋ฐฐ์—ด