์ •๋ ฌ์„ ๋ฐฐ์šฐ์ž

Stable vs. Unstable

๋„๋Œ€์ฒด Stable์ด ๋ญ์•ผ?

Stable sort๋Š” ๋ฐ˜๋ณต๋œ ์›์†Œ๊ฐ€ ์ •๋ ฌ๋˜๋”๋ผ๋„ ๊ฐ™์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋˜๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค.

๋ฌด์Šจ ์˜๋ฏธ๋ƒ๋ฉด ์ด๋Ÿฐ ์˜๋ฏธ๋‹ค.

image-20191030195710242

๊ทธ๋ ‡๋‹ค๋ฉด Stability๊ฐ€ ์™œ ์ค‘์š”ํ•œ๊ฑธ๊นŒ?

์—ฌ๋Ÿฌ๊ฐ€์ง€ ์†์„ฑ์„ ๊ฐ€์ง„ ํŠœํ”Œ์„ ์ƒ๊ฐํ•ด๋ณด์ž. ์ฒซ ๋ฒˆ์งธ ์†์„ฑ์œผ๋กœ ๊ทธ ํŠœํ”Œ์ด ์ด๋ฏธ ์ •๋ ฌ ๋˜์–ด ์žˆ๊ณ , ์ด์ œ ์‚ฌ์šฉ์ž๋Š” ๋‘ ๋ฒˆ์งธ ์†์„ฑ์œผ๋กœ ๊ทธ ํŠœํ”Œ์„ ์ •๋ ฌํ•˜๊ณ  ์‹ถ๋‹ค. ๊ทผ๋ฐ~ ์ œ์ž๋ฆฌ ์ •๋ ฌ์ด ์•„๋‹ˆ๋ฉด ์ฒซ ๋ฒˆ์งธ ์†์„ฑ์˜ ์ •๋ ฌ์ด ๊นจ์ ธ๋ฒ„๋ฆฌ๋Š” ๊ฒƒ์ด๋‹ค.

Stability๊ฐ€ ๋˜๋Š” ์ •๋ ฌ์˜ ์กฐ๊ฑด์€ ๋ฌด์—‡์ผ๊นŒ?

Insertion sort, merge sort๋Š” Stableํ•œ ์ •๋ ฌ์ด๋‹ค. ๋‹ค๋ฅธ ์ •๋ ฌ์ด๋ž‘ ๋ญ๊ฐ€ ๋‹ค๋ฅด๊ธธ๋ž˜!

  • less than vs less than or equal to

Sort for Linked List

Linked List๋ฅผ ์ •๋ ฌํ•˜๋ ค๋ฉด Merge sort๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ ์ด์œ ๋Š” Linked list๊ฐ€ random access ๋ถˆ๊ฐ€๋Šฅ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

Merge two sorted linked list

Node* merge(Node *a, Node *b) {
  Node *result = nullptr;
  if(a==nullptr)
    return b; 
  else if(b==nullptr) 
    return a; 
  if(a->data <= b->data) {
    result = a; 
    result -> next = merge(a->next, b);
  } else {
    result = b; 
    result -> next = merge(a, b->next);
  }
  return result;
}

์žฌ๊ท€์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ์ด๋ฏธ ์ •๋ ฌ๋œ ๋‘ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ mergeํ•  ์ˆ˜ ์žˆ๋‹ค.

Merge Sort

ํฐ ๋…ธ์ด๋งŒ์— ์˜ํ•ด ๊ฐœ๋ฐœ๋˜์—ˆ๋‹ค. NlogNNlogN ์˜ ์„ฑ๋Šฅ์„ ๋ณด์žฅํ•œ๋‹ค.

๋ถ„ํ• ์ •๋ณต์— ๊ธฐ๋ฐ˜ํ•œ ์ •๋ ฌ๋กœ ๋‹ค์Œ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

  1. ๋ฐฐ์—ด์„ ์ ˆ๋ฐ˜์œผ๋กœ ์ชผ๊ฐ ๋‹ค.
  2. ๊ฐ๊ฐ์„ ์žฌ๊ท€์ ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
  3. ๋‘ ๋ฐฐ์—ด์„ ํ•ฉ์นœ๋‹ค. (์—ฌ๊ธฐ๊ฐ€ ํ•ต์‹ฌ!)

๐Ÿ˜„ ๋‘ ์ •๋ ฌ๋œ ๋ฐฐ์—ด์ด ์žˆ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ํ•ฉ์น  ์ˆ˜ ์žˆ์„๊นŒ? (ใ… ใ… )

E!A!S!Y!

void merge(vector<int>& a, vector<int>& aux, int lo, int mid, int hi) {
  // why copy needed? 
  for (int k=lo; k<=hi; ++k) {
    aux[k] = a[k];
  }
  int i = lo, j = mid+1; 
 	for(int k=lo; k<=hi; ++k) {
    if(i>mid) a[k] = aux[j++]; 
    else if (j>hi) a[k] = aux[i++];
    else if (aux[j]<aux[i]) a[k] = aux[j++];
    else a[k] = aux[i++];
  }
}

void mergeSort(vector<int>& a, vector<int>& aux, int lo, int hi) {
  if (hi<=lo) return; 
  int mid = (lo+hi)/2; 
  mergeSort(a, aux, lo, mid);
  mergeSort(a, aux, mid+1, hi);
  merge(a, aux, lo, mid, hi);
}

๐Ÿ˜„ Merge sort๋Š” ์ •๋ ฌํ•  ๋•Œ N ํฌ๊ธฐ์˜ ๋ฐฐ์—ด์„ ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

์œ„์˜ ์ฝ”๋“œ์—์„œ aux ๋ฐฐ์—ด์ด merge sort๊ฐ€ ๋ฐ”๋กœ ๊ทธ ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฐ์—ด์ด๋‹ค. ๋”ฐ๋ผ์„œ merge sort๋Š” in-place sort๊ฐ€ ์•„๋‹ˆ๋‹ค. merge sort๋Š” in-place๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๋„ˆ๋ฌด ๋ณต์žกํ•˜์—ฌ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

practical improvements

  1. subarray๊ฐ€ ์ถฉ๋ถ„ํžˆ ์ž‘๋‹ค๋ฉด, ์žฌ๊ท€์˜ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด ๋‹ค๋ฅธ ์ •๋ ฌ์„ ํ•˜์ž.

    void mergeSort(vector<int>& a, vector<int>& aux, int lo, int hi) {
     if (hi<=lo + CUTOFF - 1) {
       insertionSort(a, lo, hi);
       return; 
    	}
     int mid = (lo+hi)/2; 
     mergeSort(a, aux, lo, mid);
     mergeSort(a, aux, mid+1, hi);
     merge(a, aux, lo, mid, hi);
    }
    • CUTOFF๋Š” 7์ •๋„๋ฉด ์ ๋‹นํ•˜๋‹ค.
    • 20%์ •๋„ ๋นจ๋ผ์ง„๋‹ค.
  2. ์ •๋ ฌ์ด ๋˜์—ˆ๋‹ค๋ฉด ๋ฉˆ์ถ”์ž.

    void mergeSort(vector<int>& a, vector<int>& aux, int lo, int hi) {
     if (hi<=lo) return; 
     int mid = (lo+hi)/2; 
     mergeSort(a, aux, lo, mid);
     mergeSort(a, aux, mid+1, hi);
     if(a[mid+1]>=a[lo]) 
       return;
     merge(a, aux, lo, mid, hi);
    }
    • ๋‘ ๋ฒˆ์งธ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด ์ฒซ ๋ฒˆ์งธ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๋ฉด ๊ตณ์ด merge๋ฅผ ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
  3. auxiliary array์— ๋ณต์‚ฌํ•˜๋Š” ๊ณผ์ •์„ ์ œ๊ฑฐํ•˜์ž. (์‹œ๊ฐ„์€ ์ ˆ์•ฝํ•˜์ง€๋งŒ ๊ณต๊ฐ„์„ ์ ˆ์•ฝํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹˜์— ์ฃผ์˜)

    void merge(vector<int>& a, vector<int>& aux, int lo, int mid, int hi) {
     int i = lo, j = mid+1; 
    	for(int k=lo; k<=hi; ++k) {
       if(i>mid) a[k] = aux[j++]; 
       else if (j>hi) a[k] = aux[i++];
       else if (aux[j]<aux[i]) a[k] = aux[j++];
       else a[k] = aux[i++];
     }
    }
    
    void mergeSort(vector<int>& a, vector<int>& aux, int lo, int hi) {
     if (hi<=lo) return; 
     int mid = (lo+hi)/2; 
     mergeSort(aux, a, lo, mid);
     mergeSort(aux, a, mid+1, hi);
     merge(a, aux, lo, mid, hi);
    }
    • a์™€ aux๋ฅผ ๋ฐ”๊ฟ”๊ฐ€๋ฉด์„œ ํ•œ๋‹ค.

Bottom-up mergesort

  1. array๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๋จผ์ € ํฌ๊ธฐ๊ฐ€ 1์ธ ์„œ๋ธŒ์–ด๋ ˆ์ด๋“ค์„ ํ•ฉ์นœ๋‹ค.
  2. ์ด๊ฑธ ํฌ๊ธฐ 2, 4, 8, 16, โ€ฆ ์œผ๋กœ ๋ฐ˜๋ณตํ•œ๋‹ค.

No recursion!!!

void merge(vector<int>& a, int lo, int mid, int hi) {
  ...
}

void mergeSort(vector<int>& a) {
  int N = a.size(); 
  vector<int> aux(N); 
  for(int sz=1; sz<N; sz+=sz) {
    for(int lo=0; lo<N-sz; lo+=2*sz) {
      merge(a, lo, lo+sz-1, min(lo+2*sz-1, N-1));
    }
  }
}

Quick Sort

๋Œ€๋ถ€๋ถ„์˜ ์‹œ์Šคํ…œ์—์„œ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ 1980๋…„์— ๊ฐœ๋ฐœ๋˜์—ˆ๋‹ค.

  1. Partition!

    • a[j]๋ฅผ ๊ธฐ์ค€์œผ๋กœ
    • j์˜ ์™ผ์ชฝ์—๋Š” a[j]๋ณด๋‹ค ์ž‘์€ ์›์†Œ๋ฅผ
    • j์˜ ์˜ค๋ฅธ์ชฝ์—๋Š” a[j]๋ณด๋‹ค ํฐ ์›์†Œ๋ฅผ ๋†“๋Š”๋‹ค.
  2. ์žฌ๊ท€์ ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.

์ž˜ ์ƒ๊ฐํ•ด๋ณด๋ฉด partition์ธ a[j]๋Š” parition ๊ณผ์ •์—์„œ ์ž๊ธฐ ์ž๋ฆฌ๋ฅผ ์ฐพ๊ฒŒ ๋œ๋‹ค.

โญ Partitioning โญ

  1. i ์™€ j ๊ฐ€ ์—‡๊ฐˆ๋ฆด ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.

    • a[lo] ๋ณด๋‹ค ํฐ ์›์†Œ๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ i++
    • a[lo] ๋ณด๋‹ค ์ž‘์€ ์›์†Œ๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ j++
    • i>=j ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด swap(a[i], a[j])
int partition(vector<int> &a, int lo, int hi) {
  int i = lo, j = hi+1;
  while(true) {
    while(a[++i] < a[lo])
      if(i == hi) break; 
    while(a[lo] < a[--j])
      if(j == lo) break; 
    if(i>=j) break; 
    swap(a[i], a[j]);
  }
  swap(a[lo], a[j]);
  return j;
}

Sort

partition์„ ๊ธฐ์ค€์œผ๋กœ left์™€ right๋ฅผ ์ •๋ ฌํ•œ๋‹ค.

void sort(vector<int> &a, int lo, int hi) {
  if(hi<=lo) return; 
  int j = partition(a, lo, hi);
  sort(a, lo, j-1);
  sort(a, j+1, hi);
  // ์ด๋ฏธ a[j]๋Š” ์ž๊ธฐ ์ž๋ฆฌ๋ฅผ ์ฐพ์•˜์œผ๋ฏ€๋กœ lo~j-1๊ณผ j+1~hi๋ฅผ ์ •๋ ฌํ•œ๋‹ค. 
}

๐Ÿ˜„ ํ€ต์†ŒํŠธ์˜ ์ตœ๋Œ€ ์žฅ์ 

: in-place ์ •๋ ฌ์ด๋‹ค. ์ฆ‰, ์ •๋ ฌ์„ ์œ„ํ•œ ์ถ”๊ฐ€ ๊ณต๊ฐ„์ด ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค.

Quick sort์—์„œ ๊ณ ๋ คํ•ด์•ผํ•  ์ 

  1. Partitioning in-place

    : ์ถ”๊ฐ€ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•ด์„œ partitioning์„ ์‰ฝ๊ณ  ์ด๋ฅผ ํ†ตํ•ด, stableํ•œ ์ •๋ ฌ์„ ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ํ€ต์†ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ์ œ์ž๋ฆฌ์ •๋ ฌ์ด๋ผ๋Š” ์ ์ด ํฌ๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด

  2. Terminatin the loop

    : ์–ธ์ œ loop๊ฐ€ ๋๋‚˜๋Š”์ง€ ์ฃผ์˜ ๊นŠ๊ฒŒ ๋ด์•ผํ•จ (ํŠนํžˆ ์ค‘๋ณต๋œ ํ‚ค๊ฐ€ ์กด์žฌํ•  ๋•Œ)

  3. Staying in bounds
  4. Preserving randomness
  5. Equal keys

    : ์ค‘๋ณต ํ‚ค๊ฐ€ ์กด์žฌํ•  ๋•Œ ์ฃผ์˜ํ•  ์ ๋“ค์ด ์žˆ์Œ (์ถ”ํ›„์— ์„ค๋ช…)

โ“ ํ€ต์†ŒํŠธ๊ฐ€ ๋จธ์ง€์†ŒํŠธ๋ณด๋‹ค ๋น ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์™œ ๋น ๋ฅผ๊นŒ?

์ฆ๋ช…๊ณผ์ •์€ ์ƒ๋žตํ–ˆ์ง€๋งŒ ๋น„๊ต ํšŸ์ˆ˜๊ฐ€ ํ€ต์†ŒํŠธ๋ณด๋‹ค ๋จธ์ง€์†ŒํŠธ๊ฐ€ ๋” ๋งŽ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์™œ ๋น ๋ฅด์ง€? ์ด๋Š” ๋จธ์ง€ ์†ŒํŠธ๋Š” ๊ฐ’์˜ ๋ณต์‚ฌ ๊ณผ์ •์˜ ๋น„์šฉ๋•Œ๋ฌธ์— ํ€ต ์†ŒํŠธ๋ณด๋‹ค ๋Š๋ฆฌ๋‹ค.

practical improvements

  1. subarray๊ฐ€ ์ถฉ๋ถ„ํžˆ ์ž‘๋‹ค๋ฉด insertion sort ํ•œ๋‹ค.

    • n = 10 ์ •๋„๋ฉด insertion sort๋กœ ์ฒ˜๋ฆฌํ•˜์ž.
    • 20%์ •๋„ ๋น ๋ฅด๋‹ค.
  2. Median of sample

    • ์ข‹์€ pivot์„ ๊ณ ๋ฅด๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค
    • ํ‘œ๋ณธ์˜ median์„ ์ทจํ•˜์—ฌ ์‹ค์ œ median์„ ์ถ”์ •ํ•œ๋‹ค.
    • sample์˜ ํฌ๊ธฐ๋ฅผ 3์œผ๋กœ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์‚ฌ์šฉ๋œ๋‹ค. median(a[lo], a[mid], a[hi])
    void sort(vector<int> &a, int lo, int hi) {
     if(hi<=lo) return;
     int m = median(a[lo], a[(lo+hi)/2], a[hi]);
     swap(a[lo], a[m]);
    	int j = partition(a, lo, hi);
     sort(a, lo, j-1);
     sort(a, j+1, hi);
    }

Selection

N๊ฐœ์˜ ํ•ญ๋ชฉ์—์„œ k๋ฒˆ์งธ๋กœ ํฐ ์ˆ˜๋ฅผ ์ฐพ์•„๋ผ

  • NlogNNlogN ์— ๊ตฌํ•˜์ž โ†’\rightarrow ์ •๋ ฌํ•˜๋ฉด ๋
  • k=1,2,3k=1,2,3 ์ด๋ผ๋ฉด NN ์— ๊ตฌํ•˜์ž โ†’\rightarrow N * K ์ด๊ธฐ ๋•Œ๋ฌธ์— K๊ฐ€ ์ž‘์œผ๋ฉด ๋งค์šฐ ์‰ฌ์›€
  • NN ์— ๊ตฌํ•˜์ž ๐Ÿ˜ฆ ! ์ด์ œ ์„ค๋ช…ํ•ด๋ณด๊ฒ ์Œ

์ด ๋ฌธ์ œ๋Š” 60~70๋…„๋Œ€์— ํ™œ๋ฐœํ•˜๊ฒŒ ๋…ผ์˜๋˜์—ˆ๋˜ ์ฃผ์ œ์ด๋‹ค.

Quick-select

Hoare๋Š” 1961๋…„ partitioning์— ๊ธฐ๋ฐ˜ํ•œ ํ•ด๊ฒฐ์ฑ…์„ ์ œ์•ˆํ•˜์˜€๋‹ค.

partition์˜ ํŠน์ง•์— ๊ธฐ๋ฐ˜ํ•œ๋‹ค.

  • a[j] ๋Š” ์ž๊ธฐ ์ž๋ฆฌ์— ์žˆ๋‹ค.
int select(vector<int> &a, int k) {
  int lo = 0, hi = a.size() - 1; 
  while(lo<hi) {
    int j = partition(a, lo, hi); 
    if(j<k) lo = j+1;
    else if(j>k) hi = j-1; 
    else return k; 
  }
  return a[k];
}

Duplicate key

quicksort์—์„œ ์ค‘๋ณตํ‚ค๊ฐ€ ๋งŽ์œผ๋ฉด ์–ด๋–กํ•˜์ง€?

mergesort๋Š” ๋ฐฐ์—ด์„ ์ •ํ™•ํžˆ ์ ˆ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ํ˜„์ƒ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค. ํ•˜์ง€๋งŒ quicksort๋Š” ์ค‘๋ณต ํ‚ค๊ฐ€ ๋งŽ์„ ๋•Œ ์ค‘๋ณต ํ‚ค์— ๋Œ€ํ•ด ํŒŒํ‹ฐ์…”๋‹์„ ์ค‘๋‹จํ•˜์ง€ ์•Š์œผ๋ฉด ์„ฑ๋Šฅ์ด N2N^2 ์ด ๋œ๋‹ค. 1990๋…„ C ์‚ฌ์šฉ์ž๊ฐ€ qsort() ์—์„œ ์ด ๋ฌธ์ œ์ ์„ ๋ฐœ๊ฒฌํ•˜์˜€๋‹ค. ์™œ ๊ทธ๋Ÿด๊นŒ?

Coursera Alogorithm

image-20191104195046442

์ฒซ ๋ฒˆ์งธ ๋ฌธ์ œ์ ์€

3-way partitioning

์„ ํƒ๋œ pivot์„ ๊ธฐ์ค€์œผ๋กœ ์ค‘๋ณต๊ฐ’์„ ๊ฐ€์šด๋ฐ, ์ž‘์€๊ฐ’์„ ์™ผ์ชฝ, ํฐ ๊ฐ’์„ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋‘๋Š” ๋ฐฉ๋ฒ•.

image-20191104200349956

์œ„์™€ ๊ฐ™์ด ํŒŒํ‹ฐ์…”๋‹ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ–ˆ์„ ๋•Œ ์ข‹์€ ์ ์€ ๋ญ˜๊นŒโ€ฆ

1990๋…„๋Œ€๊นŒ์ง€๋Š” ์ด ๋ฐฉ๋ฒ•์ด ๊ทธ๋ ‡๊ฒŒ ํšจ์œจ์ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์ง€ ์•Š์•˜๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋‹ค์ต์ŠคํŠธ๋ผ๊ฐ€ Dutch national flag promblem์„ ๊ณ ์•ˆํ•˜๋ฉด์„œ 3-way partitioning์„ O(n)O(n) ์˜ ๋น„์šฉ์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

๐Ÿ˜„ pseudo code

1. a[lo]์˜ ๊ฐ’ v๋ฅผ pivot์œผ๋กœ ์‚ผ๋Š”๋‹ค. 
2. ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ i๋ฅผ ์Šค์บ”ํ•˜์ž. 
    - if (a[i] < v): swap(a[lt], a[i]) and lt++; i++; 
    - if (a[i] > v): swap(a[gt], a[i]) and gt--; 
    - if (a[i] == v): i++;

๐Ÿ˜„ code

void sort(vector<int> &a, int lo, int hi) {
  if(hi<=lo)
    return; 
  int lt = lo, gt = hi; 
  int v = a[lo];
  int i = lo; 
  while(i<=gt) {
    if(a[i] < v) {
      swap(a[lt], a[i]); 
      lt++; i++;
    } else if (a[i] > v) {
      swap(a[gt], a[i]); 
      gt--;
    }
  }
  sort(a, lo, lt-1);
  sort(a, gt+1, hi);
}

System sort

โ“ JAVA์—์„œ sort๋Š” quickSort or mergeSort?

Array.sort()๋Š” primitive type์— ๋Œ€ํ•ด์„œ๋Š” quicksort๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , object์— ๋Œ€ํ•ด์„œ๋Š” mergesort๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์™œ ๋‹ค๋ฅธ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ• ๊นŒ? ๊ทธ ์ด์œ ๋Š” 1. stability 2. nlognnlogn ๋ณด์žฅ ์ด๋‹ค.

System sort๋Š” ๋Œ€๋ถ€๋ถ„ quick sort ๋ฅผ ๊ธฐ๋ณธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ตœ์ ํ™”๋ฅผ ์ถ”๊ฐ€๋กœ ํ•ด์ค€๋‹ค.

  • cutoff to insertion sort for small arrays
  • 3-way partitioning
  • partitioning item (pivot)

    • small arrays: middle entry
    • medium arrays: median of 3
    • large arrays: Tukeyโ€™s ninther

Tukeyโ€™s ninther

Median of the median of 3 samples, each of 3 entries

  • ํฌ๊ธฐ 9์˜ ์ค‘์•™๊ฐ’์„ ๊ทผ์‚ฌํ•œ๋‹ค.
  • ์ตœ๋Œ€ 12๋ฒˆ์˜ ๋น„๊ต๋ฅผ ํ•œ๋‹ค.

๋žœ๋ค์ด ์•„๋‹Œ ๋ฐฐ์—ด์— ๋Œ€ํ•ด ํ€ต์†ŒํŠธ๋Š” NlogNNlogN ์˜ ์„ฑ๋Šฅ์„ ๋ณด์žฅํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ์ •๋ ฌ ์ „์— ๋ณดํ†ต random shuffling์„ ์ง„ํ–‰ํ•œ๋‹ค. (์ง„์ง ๊ฐ€?)

Radix Sort

๊ฐ๊ฐ์˜ ์ž๋ฆฟ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค. O(n+k)O(n+k)

stable sort


[jungin]
Written by@[jungin]
์•ˆ๋…•ํ•˜์„ธ์š”

GitHub