This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void bucketSort(int[] a, int maxVal) { | |
int [] bucket=new int[maxVal+1]; | |
for (int i=0; i<bucket.length; i++) { | |
bucket[i]=0; | |
} | |
for (int i=0; i<a.length; i++) { | |
bucket[a[i]]++; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void radixSort(int[] arr) { | |
int[] result = arr ; | |
for (int place = 1; place <= 1000000000; place *= 10) { | |
result = countingSort(result, place); | |
} | |
for (int i = 0; i < arr.length; i++) { | |
arr[i] = result[i]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void countingSort(int[] arr) { | |
int n = arr.length; | |
int output[] = new int[n]; // ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ ์์ ๋ฐฐ์ด | |
int count[] = new int[256]; // arr ์ ์ ์ ์ ๋ ฅ๊ฐ์ค์ 255 ๋ฅผ ๋์ด๊ฐ๋ ๊ฑด ์ ๋ ฌ๋์ง ์์ต๋๋ค... | |
for (int i = 0; i < n; ++i) | |
++count[arr[i]]; // ๊ณ์ ๋ฅผ ์ธก์ ํฉ๋๋ค. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* heap ์ ๋ ฌ์ ํฉ๋๋ค. | |
* @param arr | |
*/ | |
void heapSort(int[] arr) { | |
for (int i = (arr.length - 1) / 2 ; i >= 0; i--) | |
heapify(arr, i, arr.length); // ๋ชจ๋ ๋ด๋ถ node๋ฅผ index๊ฐ ํฐ node ๋ถํฐ root node(arr[0])๊น์ง ํํํํฉ๋๋ค. | |
// arr[0](root node)์๋ ์ต๋๊ฐ์ด ๋ค์ด๊ฐ๊ณ ๋ชจ๋ ์๋ธํธ๋ฆฌ๊ฐ heap ์ด๋ฉ๋๋ค. | |
for (int j = arr.length - 1; j > 0; j--) { | |
swap(arr, 0, j); // 0๋ฒ ์์์ j๋ฒ ์์๋ฅผ ๊ตํํฉ๋๋ค. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ๋น์ํ ๋๋ผ์ด๋ฒ method | |
* @param arr | |
*/ | |
void quickSort(int[] arr) { | |
quickSort(arr,0,arr.length); | |
} | |
/** | |
* ํต ์ ๋ ฌ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ๋น์ํ ๋๋ผ์ด๋ฒ method | |
* @param arr | |
*/ | |
void mergeSort(int[] arr) { | |
mergeSort(arr,0,arr.length); | |
} | |
/** | |
* ํฉ๋ณ ์ ๋ ฌ - ๋ถํ ํ๋ ๋ก์ง์ด ๋ค์ด์๊ณ , ๋ค๋ฅธ ํฉ๋ณ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ์ ๊ฐ์ง๊ณ ์๋ ํจ์๋ฅผ ์คํํฉ๋๋ค. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ์ ธ ์ ๋ ฌ์ ์ํด ๋๋์ด์ง ๋ถ๋ถ ๋ฐฐ์ด์ ์ ๋ ฌํ ์ฝ์ ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ | |
* @param array ๋ฐฐ์ด | |
* @param startIndex ์์ ์ธ๋ฑ์ค | |
* @param gap ์ฆ๋ถ | |
*/ | |
void customInsertionSort(int[] arr, int startIndex, int gap) { | |
for(int i = startIndex + gap; i < arr.length; i+=gap) { | |
// ์ฝ์ ์ ๋ ฌ๊ณผ ๋ค๋ฅด๊ฒ i๊ฐ startIndex + gap์์ ์์ํ๊ณ ์ฆ๋ถ์ด 1์ด ์๋๊ณ , gap์ด ๋๋ค. | |
int ai = arr[i]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void insertionSort(int[] arr){ | |
for(int i = 1; i < arr.length; i++) { | |
// i๊ฐ 0์ด ์๋๋ผ 1๋ถํฐ ์์ํฉ๋๋ค. i๊ฐ 0์ธ loop ๋ ๋น๊ตํ ์์๊ฐ ์์ต๋๋ค. | |
int ai = arr[i]; // ์ ๋ ฌ๋ ์ ๋ฐฐ์ด์ ์ฝ์ ๋ ์์, ์์น๋ฅผ ์ฐพ์ ๋๊น์ง ์์์ ์ฅ. | |
int j; // ๋น๊ต ์ฐ์ฐ ๋ฐ ์ด๋ ์ฐ์ฐ์ ํ ์์ ๋ณ์ | |
for (j = i; j > 0 && arr[j-1] > ai; j--) // ์ ๋ ฌ๋ ๋ฐฐ์ด์์ ai ๋ณด๋ค ํฐ ๊ฐ์ด ๋์ฌ ๋๊น์ง j ๊ฐ์ | |
arr[j] = arr[j - 1]; // ์์๊ฐ ํ ์นธ์ฉ ์ฐ์ธก์ผ๋ก ์ด๋ | |
arr[j] = ai; // ๋ง์ง๋ง์ผ๋ก ๋์จ j์ ai ๋ฅผ ์ฝ์ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void selectionSort(int[] arr){ | |
for(int i = arr.length - 1; i > 0; i --) { | |
int maxIndex = 0 ; // ๋ฐฐ์ด ์์์ค max ๊ฐ์ index๋ฅผ ์ ์ฅํ ๋ณ์ | |
for(int j = 1; j <= i ; j++) | |
if(arr[j] > arr[maxIndex]) maxIndex = j; | |
swap(arr, maxIndex, i); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void bubbleSort(int[] arr){ | |
for(int i = arr.length - 1; i > 0; i --)// main loop, i ๋ (๋ฐฐ์ด์ ๊ธธ์ด - 1) ์์ 1๊น์ง 1์ฉ ๊ฐ์ | |
for(int j =0; j < i ; j++) // j ๋ 0๋ถํฐ i ๊น์ง 1์ฉ ์ฆ๊ฐํ๋ฉฐ ๋ฐ๋ณต | |
if(arr[j] > arr[j+1]) swap(arr, j, j+1); // index ๊ฐ ์์ ์์๊ฐ ํด๊ฒฝ์ฐ ๊ตํ | |
} |