-
[์๊ณ ๋ฆฌ์ฆ] 07. Sort๐ณDev/Algorithm 2021. 11. 25. 16:35
Insertion Sort
์ฝ์ ์ ๋ ฌ
: ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์์์๋ถํฐ ์ฐจ๋ก๋ก ์ด๋ฏธ ์ ๋ ฌ๋ค ๋ถ๋ถ๊ณผ ๋น๊ตํ์ฌ, ์์ ์ ์์น๋ฅผ ์ฐพ์ ์ฝ์ ํ๋ ์๊ณ ๋ฆฌ์ฆ
boolean sort(E[] aList){ if(aList.length <= 1) return false; int minLoc = 0; for(int i = 1; i < aList.length; i++) if(this.compare(aList[minLoc], aList[i]) > 0) minLoc =i; this.swap(aList,minLoc,0); for(int i = 2;i < aList.length; i++){ E insertedElement = aList[i]; int j = i-1; while(this.compare(aList[j], insertedElement) > 0){ aList[j+1] = aList[j]; j--; } aList[j+1] = insertedElement; } return true; }
- Worst ์๊ฐ ๋ณต์ก๋ : O(n^2)
Quick Sort
ํต์ ๋ ฌ
: pivot์ ๊ธฐ์ค์ผ๋ก ํ์ฌ, ๋๊ฐ์ ๋น๊ท ๋ฑํ ํฌ๊ธฐ๋ก ๋ถํ ํ ํ ์ ๋ ฌ
: ์ด๋ฅผ ๋ฐ๋ณตํ์ฌ ์ ์ฒด์ ์ผ๋ก ์ ๋ ฌ๋ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ ๋ค- ํผ๋ด์ ๊ธฐ์ค์ผ๋ก ํผ๋ด๋ณด๋ค ์์ ๊ฐ์ ์ผ์ชฝ์, ํฐ ๊ฐ์ ์ค๋ฅธ์ชฝ์ผ๋ก swap
- ํผ๋ด์ ๊ธฐ์ค์ผ๋ก ๋๊ฐ์ ๋ฆฌ์คํธ๋ก ๋ถํ ํ ํ, ์์ ๊ณผ์ ์ ๋ฐ๋ณต
- ๋ฆฌ์คํธ์ ํฌ๊ธฐ๊ฐ 0์ด๋ 1์ด ๋ ๋๊น์ง ๋ฐ๋ณต
- Worst ์๊ฐ ๋ณต์ก๋ : O(n^2)
ํ๊ท ์ ์ผ๋ก๋ O(NlogN)์ผ๋ก ๊ฐ์ ์๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋ ๋ค๋ฅธ ์๊ณ ๋ฆฌ์ฆ๊ณผ ๋น๊ตํ ๋, ๊ฐ์ฅ ๋น ๋ฅด๊ณ ์ถ๊ฐ์ ์ธ ๋ฉ๋ชจ๋ฆฌ๋ ํ์๋ก ํ์ง ์๋๋ค. ์ ๋ ฌ๋์ด์๋ ๊ฒฝ์ฐ์ ์ํ์๊ฐ์ด ๋ ๊ฑธ๋ฆฐ๋ค. ๋ฐ๋ผ์ pivot์ ์ค๊ฐ๊ฐ์ผ๋ก ์ค์ ํ๋ ๊ฒ์ด ์ข๋ค.
void quickSort(int[] aList, int left, int right) { if(left<right){ int pivot = this.pivot(aList,left,right); this.swap(aList,left,pivot); E pivotElement = aList[left]; int toRight = left; int toLeft = right+1; do{ do{ toRight++;} while(this.compare(aList[toRight],pivotElement)<0); do{ toLeft--;} while(this.compare(aList[toLeft],pivotElement)>0); if(toRight<toLeft) this.swap(aList,toRight,toLeft); }while (toRight < toLeft); this.swap(aList,left,toLeft); quickSort(aList, left, toLeft - 1); quickSort(aList, toLeft + 1, right); } }
Heap Sort
ํ ์ ๋ ฌ
: ์ต๋ ํ ๋๋ ์ต์ ํ์ ๋ง๋ค์ด ์ ๋ ฌ
- ์ ๋ ฌํด์ผ ํ n๊ฐ์ ์์๋ฅผ ์ต๋ ํ(๋ด๋ฆผ์ฐจ์)์ ๋ง๋ฌ
- ํ์์ ์์๋ฅผ ํ๋์ฉ ๊บผ๋ด ๋ฐฐ์ด์ ๋ค๋ถํฐ ์ ์ฅ
- ์ต๋ ๊ฐ๋ถํฐ ์ญ์ ๋๋ฏ๋ก ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
- Heap์ ์์ ์ด์งํธ๋ฆฌ์ด๋ฉฐ, 1์ฐจ์ ๋ฐฐ์ด๋ก ๊ตฌํ
- ์ ๋ ฌ์ ์ํด ๋ฃจํธ ๊ฐ์ ์ญ์ ํ๊ณ , ํด๋น ์๋ฆฌ์ ๋ง์ง๋ง ๋ ธ๋์ ๊ฐ์ ๊ฐ์ ธ์ค๊ณ ๋ค์ ํ์ ์ฌ๊ตฌ์ฑ
- Worst ์๊ฐ ๋ณต์ก๋ : O(NlogN)
๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ์ ์ต๋ ํ, ์ค๋ฆ์ฐจ์ ์ ๋ ฌ์ ์ต์ ํ์ ๊ตฌ์ฑํ๋ค. ์ ์ฒด ์ ๋ ฌ์ด ์๋๊ณ , ๊ฐ์ฅ ํฐ ๊ฐ์ด ๋ช ๊ฐ๋ง ํ์ํ ๋ ์ฌ์ฉํ๊ธฐ ์ข๋ค.
private void adjust(E[] aList,int root, int endOfHeap){ int parent = root; E rootElement = aList[root]; while((parent*2) <= endOfHeap){ int biggerChild = parent*2; int rightChild = biggerChild+1; if((rightChild <= endOfHeap)&&(this.compare(aList[biggerChild], aList[rightChild]) < 0)) biggerChild = rightChild; if(this.compare(rootElement, aList[biggerChild]) >= 0) break; aList[parent] = aList[biggerChild]; parent = biggerChild; } aList[parent] = rootElement; } private void makeInitHeap(E[] aList){ for(int rootOfSubtree = (aList.length-1)/2; rootOfSubtree >= 1; rootOfSubtree--){ this.adjust(aList, rootOfSubtree, aList.length-1); } } private E removeMax(E[] aList, int heapSize){ E removedElement = aList[HeapSort.HEAP_ROOT]; aList[HeapSort.HEAP_ROOT] = aList[heapSize]; this.adjust(aList, HeapSort.HEAP_ROOT, heapSize-1); return removedElement; } @Override public boolean sort(E[] aList){ if(aList.length <= 1) return false; int minLoc = 0; for(int i = 1; i < aList.length; i++) if(this.compare(aList[minLoc], aList[i]) > 0) minLoc = i; this.swap(aList,minLoc,0); this.makeInitHeap(aList); for(int heapSize = aList.length-1; heapSize > 1; heapSize--){ E maxElement = this.removeMax(aList, heapSize); aList[heapSize] = maxElement; } return true; }
'๐ณDev > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๊ณ ๋ฆฌ์ฆ] 08. Search (0) 2021.11.25 [์๊ณ ๋ฆฌ์ฆ] 06. Topological Sort (0) 2021.11.24 [์๊ณ ๋ฆฌ์ฆ] 05. Shortest Path (0) 2021.11.24 [์๊ณ ๋ฆฌ์ฆ] 04. Min-Cost Spanning Tree (0) 2021.11.23 [์๊ณ ๋ฆฌ์ฆ] 03. Union-Find (0) 2021.11.22