Skip to content

Instantly share code, notes, and snippets.

View keehyun2's full-sized avatar
๐Ÿ˜ช
zzzz

Keehyun Park keehyun2

๐Ÿ˜ช
zzzz
View GitHub Profile
@keehyun2
keehyun2 / bucketSort.java
Created January 24, 2018 12:06
bucketSort
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]]++;
}
@keehyun2
keehyun2 / radixSort.java
Created January 24, 2018 12:05
radixSort ๊ธฐ์ˆ˜ ์ •๋ ฌ
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];
}
}
@keehyun2
keehyun2 / countingSort.java
Created January 24, 2018 12:05
countingSort
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]]; // ๊ณ„์ˆ˜ ๋ฅผ ์ธก์ •ํ•ฉ๋‹ˆ๋‹ค.
@keehyun2
keehyun2 / heapSort.java
Created January 24, 2018 12:04
heapSort
/**
* 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๋ฒˆ ์›์†Œ๋ฅผ ๊ตํ™˜ํ•ฉ๋‹ˆ๋‹ค.
@keehyun2
keehyun2 / quickSort.java
Created January 24, 2018 12:04
quickSort
/**
* ๋น„์ˆœํ™˜ ๋“œ๋ผ์ด๋ฒ„ method
* @param arr
*/
void quickSort(int[] arr) {
quickSort(arr,0,arr.length);
}
/**
* ํ€ต ์ •๋ ฌ
@keehyun2
keehyun2 / mergeSort.java
Created January 24, 2018 12:04
mergeSort
/**
* ๋น„์ˆœํ™˜ ๋“œ๋ผ์ด๋ฒ„ method
* @param arr
*/
void mergeSort(int[] arr) {
mergeSort(arr,0,arr.length);
}
/**
* ํ•ฉ๋ณ‘ ์ •๋ ฌ - ๋ถ„ํ• ํ•˜๋Š” ๋กœ์ง์ด ๋“ค์–ด์žˆ๊ณ , ๋‹ค๋ฅธ ํ•ฉ๋ณ‘์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
/**
* ์…ธ ์ •๋ ฌ์— ์˜ํ•ด ๋‚˜๋ˆ„์–ด์ง„ ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•  ์‚ฝ์ž… ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜
* @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];
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 ๋ฅผ ์‚ฝ์ž…
}
}
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);
}
}
@keehyun2
keehyun2 / bubbleSort.java
Last active January 24, 2018 12:02
bubbleSort
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 ๊ฐ€ ์ž‘์€ ์›์†Œ๊ฐ€ ํด๊ฒฝ์šฐ ๊ตํ™˜
}