Skip to content

Latest commit

ย 

History

History
343 lines (261 loc) ยท 10.3 KB

sort.md

File metadata and controls

343 lines (261 loc) ยท 10.3 KB

์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ข…๋ฅ˜์™€ ๊ฐœ๋…

์ž‘์„ฑ์ž : ์žฅ์ฃผ์„ญ, ์ด์„ธ๋ช…

Table of Contents

Insertion Sort (์‚ฝ์ž…์ •๋ ฌ)

์‚ฝ์ž…์ •๋ ฌ์ด๋ž€, ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋ฐฐ์—ด์˜ ์‹œ์ž‘๋ถ€ํ„ฐ ๋๊นŒ์ง€ ํ˜„์žฌ ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค๊ณผ ๋น„๊ตํ•ด ๊ฐ€๋ฉด์„œ ์ ์ ˆํ•œ ์œ„์น˜์— ์‚ฝ์ž…ํ•˜๋Š” ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

Example

28 13 23 25 19 : ์ดˆ๊ธฐ ๋ฐฐ์—ด

28 13 23 25 19 : 2๋ฒˆ์งธ ์ž๋ฆฌ 13 ๋ถ€ํ„ฐ ์‹œ์ž‘

13 28 23 25 19 : 13์€ ์ ์ ˆํ•œ ์ž๋ฆฌ๋ฅผ ์ฐพ์•„๊ฐ€๊ณ  ๋‹ค์Œ์œผ๋กœ 3๋ฒˆ์งธ ์ž๋ฆฌ 23์„ ๋ณธ๋‹ค.

13 23 28 25 19 : 23๋„ ์ ์ ˆํ•œ ์ž๋ฆฌ๋ฅผ ์ฐพ์•„๊ฐ€๊ณ  ๋‹ค์Œ์œผ๋กœ 4๋ฒˆ์งธ ์ž๋ฆฌ 25๋ฅผ ๋ณธ๋‹ค.

13 23 25 28 19 : 25๋„ ์ ์ ˆํ•œ ์ž๋ฆฌ๋ฅผ ์ฐพ์•„๊ฐ€๊ณ  ๋‹ค์Œ์œผ๋กœ 5๋ฒˆ์จฐ ์ž๋ฆฌ 19๋ฅผ ๋ณธ๋‹ค.

13 19 23 25 28 : ์ •๋ ฌ ์™„๋ฃŒ!

์‹œ๊ฐ„๋ณต์žก๋„

formula

Source Code

void insertionSort(int[] arr)
{

   for(int index = 1 ; index < arr.length ; index++){

      int temp = arr[index];
      int aux = index - 1;

      while( (aux >= 0) && ( arr[aux] > temp ) ) {

         arr[aux+1] = arr[aux];
         aux--;
      }
      arr[aux + 1] = temp;
   }
}
//์ถœ์ฒ˜ ์œ„ํ‚คํ”ผ๋””์•„

Selection Sort (์„ ํƒ์ •๋ ฌ)

์„ ํƒ์ •๋ ฌ์ด๋ž€, ์ฃผ์–ด์ง„ ๋ฐฐ์—ด ์ค‘์— ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ์•„ ์ •๋ ฌ ๋˜์ง€ ์•Š์€ ๋ฐฐ์—ด์˜ ๋งจ์•ž์˜ ๊ฐ’๊ณผ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊พธ์–ด ๋‚˜๊ฐ€๋Š” ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

๋ฐฐ์—ด์˜ ๋งจ์•ž๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ์ •๋ ฌ์ด ๋œ๋‹ค.

Example

28 13 23 25 19 : ์ดˆ๊ธฐ๋ฐฐ์—ด

13 28 23 25 19 : ์ตœ์†Ÿ๊ฐ’ 13์„ ๋งจ ์•ž์˜ ์ˆ˜ 28๊ณผ ์ž๋ฆฌ ๋ฐ”๊พธ๊ธฐ

13 19 23 25 28 : ๋‹ค์Œ ์ตœ์†Ÿ๊ฐ’ 19๋ฅผ ๋งจ ์•ž์˜ ์ˆ˜ 28๊ณผ ์ž๋ฆฌ ๋ฐ”๊พธ๊ธฐ

13 19 23 25 28 : ๋‹ค์Œ ์ตœ์†Ÿ๊ฐ’์€ 23์ด๋‹ˆ๊น ์ž๋ฆฌ ๊ทธ๋Œ€๋กœ!

13 19 23 25 28 : ๋‹ค์Œ ์ตœ์†Ÿ๊ฐ’์€ 25์ด๋‹ˆ๊น ์ž๋ฆฌ ๊ทธ๋Œ€๋กœ!

13 19 23 25 28 : ์ •๋ ฌ ์™„๋ฃŒ!

์‹œ๊ฐ„๋ณต์žก๋„

formula

Source Code

void selectionSort(int[] list) {
    int indexMin, temp;

    for (int i = 0; i < list.length - 1; i++) {
        indexMin = i;
        for (int j = i + 1; j < list.length; j++) {
            if (list[j] < list[indexMin]) {
                indexMin = j;
            }
        }
        temp = list[indexMin];
        list[indexMin] = list[i];
        list[i] = temp;
    }
}
//์ถœ์ฒ˜ ์œ„ํ‚คํ”ผ๋””์•„

Bubble Sort (๊ฑฐํ’ˆ์ •๋ ฌ)

๊ฑฐํ’ˆ์ •๋ ฌ์ด๋ž€, ์ธ์ ‘ํ•œ ๋‘ ์›์†Œ๋ฅผ ๋น„๊ตํ•ด ๊ฐ€๋ฉด์„œ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊พธ๋Š” ๋ฐฉ์‹์˜ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

๋ฐฐ์—ด์˜ ๋งจ๋’ค๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ์ •๋ ฌ์ด ๋œ๋‹ค.

Example

(28 13) 23 25 19 : 28 > 13 Change!

13 (28 23) 25 19 : 28 > 23 Change!

13 23 (28 25) 19 : 28 > 25 Change!

13 23 25 (28 19) : 28 > 19 Change!

(13 23) 25 19 28 : 28 ์ตœ๋Œ“๊ฐ’์œผ๋กœ ๊ณ ์ •, 13 < 23 Pass!

13 (23 25) 19 28 : 23 < 25 Pass!

13 23 (25 19) 28 : 25 > 19 Change!

(13 23) 19 25 28 : 25 ๋‹ค์Œ ์ตœ๋Œ“๊ฐ’์œผ๋กœ ๊ณ ์ •, 13 < 23 Pass!

13 (23 19) 25 28 : 23 > 19 Change!

(13 19) 23 25 28 : 23 ๋‹ค์Œ ์ตœ๋Œ“๊ฐ’์œผ๋กœ ๊ณ ์ •, 13 < 19 Pass!

13 19 23 25 28 : 19 ๋‹ค์Œ ์ตœ๋Œ“๊ฐ’์œผ๋กœ ๊ณ ์ •, ์ •๋ ฌ ์™„๋ฃŒ!

์‹œ๊ฐ„๋ณต์žก๋„

formula

Source Code

void bubbleSort(int[] arr) {
    int temp = 0;
    for(int i = 0; i < arr.length; i++) {
        for(int j= 1 ; j < arr.length-i; j++) {
            if(arr[j]<arr[j-1]) {
                temp = arr[j-1];
                arr[j-1] = arr[j];
                arr[j] = temp;
            }
        }
    }
    System.out.println(Arrays.toString(arr));
}

Merge Sort (ํ•ฉ๋ณ‘์ •๋ ฌ)

ํ•ฉ๋ณ‘์ •๋ ฌ์€ ๋ฌธ์ œ๋ฅผ ๋ถ„๋ฆฌํ•˜๊ณ  ๊ฐ๊ฐ์„ ํ•ด๊ฒฐํ•œ ํ›„ ๋‹ค์‹œ ํ•ฉ์น˜๋Š” Divide & Concuer ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค.

  • Divide(๋ถ„ํ• ) : ์ดˆ๊ธฐ ๋ฐฐ์—ด์„ 2๊ฐœ์˜ ๋ฐฐ์—ด๋กœ ๋ถ„ํ• ํ•œ๋‹ค.
  • Conquer(์ •๋ณต) : ๊ฐ ๋ถ€๋ถ„๋ฐฐ์—ด์„ ์žฌ๊ท€์ ์œผ๋กœ ๋ณ‘ํ•ฉ์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ •๋ ฌํ•œ๋‹ค.
  • Combine(๊ฒฐํ•ฉ) : ๋ถ€๋ถ„๋ฐฐ์—ด์„ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด๋กœ ๊ฒฐํ•ฉํ•œ๋‹ค.

Example

์‹œ๊ฐ„๋ณต์žก๋„

  • Avaerage Case : formula
  • Worst Case : formula

Source Code

void mergeSort(int[] arr, int start, int end) {
  if (start < end) {
    int mid = (start + end) / 2;
    mergeSort(arr, start, mid);
    mergeSort(arr, mid + 1, end);
    merge(arr, start, mid, end);
  }
}

void merge(int[] arr, int start, int mid, int end) {
  int i = start; // ์™ผ์ชฝ ๋ฐฐ์—ด์˜ ์‹œ์ž‘
  int j = mid + 1; // ์˜ค๋ฅธ์ชฝ ๋ฐฐ์—ด์˜ ์‹œ์ž‘
  int k = 0; // ๋ณ‘ํ•ฉ๋œ ๋ฐฐ์—ด์˜ ์‹œ์ž‘

  int[] temp = new int[end - start + 1];
  while (i <= mid && j <= end) {
    if (arr[i] < arr[j]) {
      temp[k++] = arr[i++];
    } else {
      temp[k++] = arr[j++];
    }
  }
  // ๋‚จ์€ ๊ฐ’ ๋ณต์‚ฌ
  if (i > mid) { // ์™ผ์ชฝ ๋ฐฐ์—ด ๊ฐ’ ๋‹ค ์‚ฌ์šฉํ•จ , ์˜ค๋ฅธ์ชฝ ๋‹ค ๋ณต์‚ฌ
    for (int idx = j; idx <= end; idx++, k++) {
      temp[k] = arr[idx];
    }
  } else { // ์˜ค๋ฅธ์ชฝ ๋ฐฐ์—ด ๊ฐ’ ๋‹ค ์‚ฌ์šฉํ•จ, ์™ผ์ชฝ ๋‹ค ๋ณต์‚ฌ
    for (int idx = i; idx <= mid; idx++, k++) {
      temp[k] = arr[idx];
    }
  }
  // ์ž„์‹œ ๋ฐฐ์—ด -> ์›๋ž˜ ๋ฐฐ์—ด
  for (int num : temp) {
    arr[start++] = num;
  }
}

์žฅ์ 

  • ์•ˆ์ •์ ์ธ ์ •๋ ฌ ๋ฐฉ๋ฒ•์ด๋‹ค. ์ž…๋ ฅ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ์— ์ƒ๊ด€์—†์ด formula ๋ฅผ ์œ ์ง€ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹จ์ 

  • ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋ฉด ์ž„์‹œ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
  • ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ์ปค์ง€๋ฉด ๋ฐ์ดํ„ฐ์˜ ์ด๋™ ํšŸ์ˆ˜๊ฐ€ ๋งŽ์•„์ ธ ์‹œ๊ฐ„์ด ์ปค์งˆ ์ˆ˜ ์žˆ๋‹ค.

Quick Sort (ํ€ต์ •๋ ฌ)

ํ•ฉ๋ณ‘์ •๋ ฌ์€ ๋ฌธ์ œ๋ฅผ ๋ถ„๋ฆฌํ•˜๊ณ  ๊ฐ๊ฐ์„ ํ•ด๊ฒฐํ•œ ํ›„ ๋‹ค์‹œ ํ•ฉ์น˜๋Š” Divide & Concuer ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค.

  1. ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์—์„œ pivot์„ ์„ ์ •ํ•œ๋‹ค.(๋ฐ‘์— ์†Œ์Šค์ฝ”๋“œ์—์„  ๋งจ ์•ž์˜ ๊ฐ’์œผ๋กœ ํ•จ)
  2. pivot์„ ๊ธฐ์ค€์œผ๋กœ ์ž‘์€ ๊ฐ’๋“ค์ด ๋ชจ์ธ ๋ฐฐ์—ด๊ณผ ํฐ ๊ฐ’๋“ค์ด ๋ชจ์ธ ๋ฐฐ์—ด๋กœ ๋น„๊ท ๋“ฑํ•˜๊ฒŒ ๋ฐฐ์—ด์„ ๋‘ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆˆ๋‹ค.
  3. ํ•ด๋‹น pivot์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐฐ์—ด์„ ๋‚˜๋ˆ„๊ณ  ๋‚˜๋ฉด pivot์€ ๊ณ ์ • ๋œ๋‹ค. (์žฌ๊ท€ ํ˜ธ์ถœ๋งˆ๋‹ค pivot์˜ ์œ„์น˜๋Š” ๋งค๋ฒˆ ๊ณ ์ • ๋˜๋ฏ€๋กœ ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋ฐ˜๋“œ์‹œ ๋๋‚˜๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•œ๋‹ค.)
  4. ๋ถ„ํ• ๋œ ๋‘ ๋ฐฐ์—ด์—์„œ ์žฌ๊ท€์ ์œผ๋กœ ์ด ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

Example

3 7 6 5 1 4 2 : ์ดˆ๊ธฐ๋ฐฐ์—ด

pivot = 3

3 7 6 5 1 4 2 : low = 1, high = 6, swap!

3 2 6 5 1 4 7 : low = 2, high, = 5

3 2 6 5 1 4 7 : low = 2, high = 4, swap!

3 2 1 5 6 4 7 : low = 3, high = 3

3 2 1 5 6 4 7 : low = 3, high = 2 (์„œ๋กœ ์ง€๋‚˜์นจ) => high ์™€ pivot swap!

1 2 3 5 6 4 7

pivot = 1

...

pivot = 5

...

์‹œ๊ฐ„๋ณต์žก๋„

  • Average Case : formula
  • Worst Case : formula

Source Code

void quickSort(int[] arr, int start, int end) {
  if (start < end) { // ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ์ถฉ๋ถ„ํžˆ ์ž‘์•„ ์งˆ ๋•Œ ๊นŒ์ง€ ๋‚˜๋ˆ”
    int p = partition(arr, start, end); // ํŒŒํ‹ฐ์…˜์„ ์ ์šฉ ํ–ˆ์„ ๋•Œ ํ”ผ๋ด‡์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ตฌํ•จ

    quickSort(arr, start, p - 1); // ์ฒ˜์Œ ๋ถ€ํ„ฐ ํ”ผ๋ด‡ ์ „,
    quickSort(arr, p + 1, end); // ํ”ผ๋ด‡ ํ›„ ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๊นŒ์ง€ ๋‹ค์‹œ ํ€ต์†ŒํŠธ๋ฅผ ํ•จ
  }
}

int partition(int[] arr, int start, int end) {
  int low = start + 1; // pivot์„ ๋งจ ์™ผ์ชฝ ๊ฐ’์œผ๋กœ ํ• ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ๋‹ค์Œ ๊ฐ’๋ถ€ํ„ฐ ํ™•์ธ
  int high = end;
  int pivot = arr[start]; // ๊ฐ€์žฅ ์™ผ์ชฝ ๊ฐ’์„ pivot์œผ๋กœ ์„ค์ •

  while (low <= high) { // ์–‘์ชฝ์—์„œ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๋‘˜์ด ๊ฒน์ณ์ ธ ์ง€๋‚˜์น ๋•Œ ๊นŒ์ง€ ํ•œ๋‹ค.
    while (low <= end && arr[low] < pivot) { // ์•ž์—์„œ ๋ถ€ํ„ฐ ๋น„๊ต์ค‘ pivot ๋ณด๋‹ค ํฌ๋ฉด stop
      low++;
    }
    while (high >= start && arr[high] > pivot) { // ๋’ค์—์„œ ๋ถ€ํ„ฐ ๋น„๊ต์ค‘ pivot ๋ณด๋‹ค ์ž‘์œผ๋ฉด stop
      high--;
    }
    if (low < high) { // low , high ๊ฐ€ ๊ฒน์ณ์ ธ ์ง€๋‚˜์นœ๊ฒŒ ์•„๋‹ˆ๋ฉด ๋‘˜์„ ๋ฐ”๊ฟ”์คŒ
      swap(arr, low, high);
    }
  }
  swap(arr, start, high); // ๋งˆ์ง€๋ง‰์œผ๋กœ pivot๊ณผ high index์˜ ๊ฐ’์„ ๋ฐ”๊พธ๋ฉด high index ๊ฐ€ pivot์˜ index๊ฐ€ ๋จ
  return high; // pivot ์œ„์น˜ ๋ฐ˜ํ™˜
}

void swap(int[] arr, int i, int j) {
  int temp = arr[j];
  arr[j] = arr[i];
  arr[i] = temp;
}

์žฅ์ 

  • ์†๋„๊ฐ€ ๋งค์šฐ ๋น ๋ฅด๋‹ค
  • ์ถ”๊ฐ€์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ํ•„์š”๋กœ ํ•˜์ง€ ์•Š๋Š”๋‹ค

๋‹จ์ 

  • ๋ถˆ๊ท ํ˜• ๋ถ„ํ• ์ด ๋งŽ์•„์งˆ ๊ฒฝ์šฐ ์ตœ์•…์˜ ๊ฒฝ์šฐ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ๋‹ค.
    • ๋ถˆ๊ท ํ˜• ๋ถ„ํ• ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์„ธ ๊ฐ’์˜ ์ค‘์œ„๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ ํ”ผ๋ฒ—์„ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.

Quick Sort vs Merge Sort

๋‘ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋‹ค average case formula ์ด๊ณ  ์‹ฌ์ง€์–ด Quick Sort๋Š” worst case formula์ด๋‹ค. ํ•˜์ง€๋งŒ ๋ณดํ†ต ์ผ๋ฐ˜์ ์œผ๋กœ ํ€ต์†ŒํŠธ๊ฐ€ ๋น ๋ฅธ๊ฒƒ์œผ๋กœ ์•Œ๊ณ  ์žˆ๋‹ค.

์ผ๋‹จ Quick Sort ์˜ worst case ๋Š” (๋งจ์•ž์˜ ์ˆ˜๋ฅผ pivot์œผ๋กœ ๊ฐ€์ •) ์•„์ด๋Ÿฌ๋‹ˆ ํ•˜๊ฒŒ ์ •๋ ฌ์ด ์ž˜ ๋˜์–ด ์žˆ๋Š” ๋ฐฐ์—ด์—์„œ ๋‚˜์˜จ๋‹ค.

์ด๋ ‡๊ฒŒ Quick Sort์˜ worst case ์™€ ํ‰๊ท  ์ ์ธ Merge Sort ๋ฅผ ๋น„๊ตํ•ด ๋ณด์•„๋„ Quick Sort๊ฐ€ ๋” ๋น ๋ฅด๊ฒŒ ๋‚˜์˜จ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์‹ค์ œ ์‹œ๊ฐ„ ์ •๋ ฌ ๋˜๋Š” ์ค‘ Merge Sort๋Š” ๋ถ„ํ•  ๊ณผ์ •์—์„œ ์ถ”๊ฐ€์ ์ธ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ณผ์ •์—์„œ ๊ณ„์†์ ์œผ๋กœ Delay ๊ฐ€ ์ƒ๊ธฐ๋‹ค ๋ณด๋‹ˆ ๊ฒฐ๊ณผ์ ์œผ๋กœ Quick Sort๊ฐ€ ๋”์šฑ ๋น ๋ฅด๊ฒŒ ์ •๋ ฌ์ด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.


Heap, Radix, Counting Sort

์•„๋ž˜์˜ ์ž๋ฃŒ์—์„œ ์ž์„ธํ•œ ์„ค๋ช…๊ณผ ์ฝ”๋“œ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.