Skip to content

Latest commit

ย 

History

History
151 lines (96 loc) ยท 3.26 KB

QuickSort.md

File metadata and controls

151 lines (96 loc) ยท 3.26 KB

์•ˆ์ „ ์ •๋ ฌ : ๋™์ผํ•œ ๊ฐ’์— ๊ธฐ์กด ์ˆœ์„œ๊ฐ€ ์œ ์ง€ (๋ฒ„๋ธ”, ์‚ฝ์ž…)

๋ถˆ์•ˆ์ • ์ •๋ ฌ : ๋™์ผํ•œ ๊ฐ’์— ๊ธฐ์กด ์ˆœ์„œ๊ฐ€ ์œ ์ง€X (์„ ํƒ,ํ€ต)

ํ€ต์†ŒํŠธ


ํ€ต์†ŒํŠธ๋Š” ์ตœ์•…์˜ ๊ฒฝ์šฐ O(n^2), ํ‰๊ท ์ ์œผ๋กœ ฮ˜(nlogn)์„ ๊ฐ€์ง

public void quickSort(int[] array, int left, int right) {
    
    if(left >= right) return;
    
    int pi = partition(array, left, right);
    
    quickSort(array, left, pi-1);
    quickSort(array, pi+1, right);
    
}

ํ”ผ๋ฒ— ์„ ํƒ ๋ฐฉ์‹ : ์ฒซ๋ฒˆ์งธ, ์ค‘๊ฐ„, ๋งˆ์ง€๋ง‰, ๋žœ๋ค

(์„ ํƒ ๋ฐฉ์‹์— ๋”ฐ๋ผ ์†๋„๊ฐ€ ๋‹ฌ๋ผ์ง€๋ฏ€๋กœ ์ค‘์š”ํ•จ)

public int partition(int[] array, int left, int right) {
    int pivot = array[left];
    int i = left, j = right;
    
    while(i < j) {
        while(pivot < array[j]) {
            j--;
        }
        while(i<j && pivot >= array[i]){
            i++;
        }
        swap(array, i, j);
    }
    array[left] = array[i];
    array[i] = pivot;
    
    return i;
}
  1. ํ”ผ๋ฒ— ์„ ํƒ
  2. ์˜ค๋ฅธ์ชฝ(j)์—์„œ ์™ผ์ชฝ์œผ๋กœ ๊ฐ€๋ฉด์„œ ํ”ผ๋ฒ—๋ณด๋‹ค ์ž‘์€ ์ˆ˜ ์ฐพ์Œ
  3. ์™ผ์ชฝ(i)์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ฐ€๋ฉด์„œ ํ”ผ๋ฒ—๋ณด๋‹ค ํฐ ์ˆ˜ ์ฐพ์Œ
  4. ๊ฐ ์ธ๋ฑ์Šค i, j์— ๋Œ€ํ•œ ์š”์†Œ๋ฅผ ๊ตํ™˜
  5. 2,3,4๋ฒˆ ๊ณผ์ • ๋ฐ˜๋ณต
  6. ๋”์ด์ƒ 2,3๋ฒˆ ์ง„ํ–‰์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉด, ํ˜„์žฌ ํ”ผ๋ฒ—๊ณผ ๊ตํ™˜
  7. ์ด์ œ ๊ตํ™˜๋œ ํ”ผ๋ฒ— ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ์—” ํ”ผ๋ฒ—๋ณด๋‹ค ์ž‘์€ ๊ฐ’, ์˜ค๋ฅธ์ชฝ์—” ํฐ ๊ฐ’๋“ค๋งŒ ์กด์žฌํ•จ

๋ฒ„๋ธ”์ •๋ ฌ์€ ๋ชจ๋“  ๋ฐฐ์—ด์˜ ์š”์†Œ์— ๋Œ€ํ•œ ์ธ๋ฑ์Šค๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ์ฆ๊ฐ€ํ•˜๋ฉฐ ๋น„๊ตํ•ด๋‚˜๊ฐ€๋Š” O(n^2)

ํ€ต์ •๋ ฌ์˜ ๊ฒฝ์šฐ ์ธ์ ‘ํ•œ ๊ฒƒ์ด ์•„๋‹Œ ์„œ๋กœ ๋จผ ๊ฑฐ๋ฆฌ์— ์žˆ๋Š” ์š”์†Œ๋ฅผ ๊ตํ™˜ํ•˜๋ฉด์„œ ์†๋„๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Œ

But, ํ”ผ๋ฒ— ๊ฐ’์ด ์ตœ์†Œ๋‚˜ ์ตœ๋Œ€๊ฐ’์œผ๋กœ ์ง€์ •๋˜์–ด ํŒŒํ‹ฐ์…˜์ด ๋‚˜๋ˆ„์–ด์ง€์ง€ ์•Š์•˜์„ ๋•Œ O(n^2)์— ๋Œ€ํ•œ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง

ํ€ต์†ŒํŠธ O(n^2) ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•


์ด๋Ÿฐ ์ƒํ™ฉ์—์„œ๋Š” ํ€ต์†ŒํŠธ ์žฅ์ ์ด ์‚ฌ๋ผ์ง€๋ฏ€๋กœ, ํ”ผ๋ฒ—์„ ์„ ํƒํ•  ๋•Œ ์ค‘๊ฐ„ ์š”์†Œ๋กœ ์„ ํƒํ•˜๋ฉด ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•จ

public int partition(int[] array, int left, int right) {
    int mid = (left + right) / 2;
    swap(array, left, mid);
    ...
}

์ด๋Š” ๋‹ค๋ฅธ O(nlogn) ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„ ์†ŒํŠธ๋“ค๋ณด๋‹ค ๋น ๋ฅด๋‹ค๊ณ  ์•Œ๋ ค์ ธ์žˆ์Œ

๋จผ๊ฑฐ๋ฆฌ ๊ตํ™˜ ์ฒ˜๋ฆฌ + ์บ์‹œ ํšจ์œจ(ํ•œ๋ฒˆ ์„ ํƒ๋œ ๊ธฐ์ค€์€ ์ œ์™ธ์‹œํ‚ด)

private void solve() {
    int[] array = { 80, 70, 60, 50, 40, 30, 20 };
    quicksort(array, 0, array.length - 1);
 
    for (int v : array) {
        System.out.println(v);
    }
}
 
public static int partition(int[] array, int left, int right) {
    int mid = (left + right) / 2;
    swap(array, left, mid);
 
    int pivot = array[left];
    int i = left, j = right;
 
    while (i < j) {
        while (pivot < array[j]) {
            j--;
        }
 
        while (i < j && pivot >= array[i]) {
            i++;
        }
        swap(array, i, j);
    }
    array[left] = array[i];
    array[i] = pivot;
    return i;
}
 
public static void swap(int[] array, int a, int b) {
    int temp = array[b];
    array[b] = array[a];
    array[a] = temp;
}
 
public static void quicksort(int[] array, int left, int right) {
    if (left >= right) {
        return;
    }
 
    int pi = partition(array, left, right);
 
    quicksort(array, left, pi - 1);
    quicksort(array, pi + 1, right);
}