๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Data Structure๐Ÿงถ

์ •๋ ฌ - ์„ ํƒ, ๋ฒ„๋ธ”, ์‚ฝ์ž…, ๋ณ‘ํ•ฉ, ํž™, ํ€ต

by Jouureee 2021. 2. 19.

1. ์„ ํƒ ์ •๋ ฌ(Selection sort) -> O(n^2)

 

์— ๋Œ€ํ•ด i = 0 k= n -1, min ๊ฐ’์„ ์ฐพ์•„ i ์ž๋ฆฌ์™€ swap 

๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๊นŒ์ง€ ๋Œ๋ฉด์„œ ๋น„๊ต 

 

์ฝ”๋“œ : 

void selectionSort(int arr[], int size){
int minIndex;
int i, j;
for(int i = 0; i < size - 1; i++){
    minIndex = i;
    for(int j = i + 1; j < size; j++){
        if(arr[j] < arr[minIndex]){
            minIndex = j;
            }
    }
    swqp(&arr[i], &arr[minIndex]);
}

2. ๋ฒ„๋ธ” ์ •๋ ฌ(Bubble sort) -> O(n^2)

 

ํ˜„์žฌ ๋ฐฐ์—ด ์š”์†Œ์™€ ๋‹ค์Œ ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ๋น„๊ตํ•˜๊ณ  ๊ตํ™˜ํ•˜๋Š” ์‹์˜ ์ •๋ ฌ

 

์ฝ”๋“œ : 

void bubbleSort(int arr[], int size){
    int i, j;
    for(i = size - 1; i > 0; i--){
        for(j = 0; j < i; j++){
            if(arr[j] < arr[j + 1]{
                swap(&arr[j], &arr[j + 1]);
            }
        }
    }

 


3. ์‚ฝ์ž… ์ •๋ ฌ(Insertion sort) -> O(n^2), ์ตœ์ ์˜ ๊ฒฝ์šฐ O(n) (์ •๋ ฌ๋œ ๊ฒฝ์šฐ) 

 

 

key๋ฅผ ์•Œ๋งž์€ ์ž๋ฆฌ์— ์‚ฝ์ž…ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

key ๋ณด๋‹ค ํฐ ๊ฐ’์€ ํ•˜๋‚˜์”ฉ ๋ฐ€์–ด๋ฒ„๋ฆฌ๊ณ  key ๋ณด๋‹ค ์ž‘์€ ๊ฐ’์„ ๋งŒ๋‚˜๋ฉด ๋’ท์ž๋ฆฌ์— ์‚ฝ์ž…ํ•œ๋‹ค. 

key์ธ 8์„ ๋งจ ์•ž์— ์‚ฝ์ž…ํ•˜๊ณ  i ๊ฐ€ ํ•˜๋‚˜ ์ฆ๊ฐ€ํ•˜์—ฌ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค. key๋Š” 1์ด ๋œ๋‹ค.

 

์ฝ”๋“œ :

void insertionSort(int arr[], int size){
    int i, j, key;
    for(i = 1; i < size; i++){
        key = arr[i];
        j = i - 1;
        while(j >= 0 && arr[j] > key){
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = key;
    } 
}

4. ๋ณ‘ํ•ฉ ์ •๋ ฌ (Merge sort)

๋‘ ๊ฐ€์ง€ ์ž‘์—…์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.

 

1. ๋ถ„ํ• 

void mergeSort(int arr[], int left, int right){
    if (left == right) return; // ํ•˜๋‚˜์˜ ์›์†Œ๋งŒ ๋‚จ์œผ๋ฉด return 
    int mid;
    
    mid = (left + right ) /2;
    mergeSort(arr, left, right);
    mergeSort(arr, mid + 1, right);
}

2. ๋ณ‘ํ•ฉ

void merge(int arr[], int left, int right){
    int L, R, k, a;
    int mid = (left + right) / 2;
    L = left;
    R = mid + 1;
    k = left;
    while(L <= mid && R <= right){
        tmp[k++] = arr[L] < arr[R] ? arr[L++] : arr[R++];
    }
    
    if(L > mid){
        for(a = R; a <= rifht; a++){
            tmp[k++] = arr[a];
        }
    else{
        for( a = L; a <= mid; a++){
            tmp[k++] = arr[a];
        }
    }
    for(a = left; a <=right; a++){
        arr[a] = tmp[a];
    }

 

์™ผ์ชฝ ๋ฐฐ์—ด, ์˜ค๋ฅธ์ชฝ ๋ฐฐ์—ด ํ•˜๋‚˜๋ผ๋„ ์ „๋ถ€ ๋ฐ˜๋ณต์ด ๋˜์—ˆ๋‹ค๋ฉด ํƒˆ์ถœํ•œ๋‹ค.

 

 

์™ผ์ชฝ ๋ถ€๋ถ„ ๋ฐฐ์—ด๊ณผ ์˜ค๋ฅธ์ชฝ ๋ถ€๋ถ„ ๋ฐฐ์—ด ์›์†Œ๋ฅผ ๋น„๊ตํ•ด์„œ ๋” ์ž‘์€ ๊ฐ’์ด tmp๋กœ ๋ณต์‚ฌ๋œ๋‹ค. 

 

< ์ „์ฒด ์ฝ”๋“œ >

void mergeSort(int arr[], int left, int right) {
  if (left == right) return;
  int mid;
  mid = (left + right) / 2;
  mergeSort(arr, left, mid); 
  mergeSort(arr, mid + 1, right); 
  merge(arr, left, right); 
}

 


5. ํž™ ์ •๋ ฌ (heap sort)

  • ์ตœ์†Œ ํž™(min heap) ?

์ž์‹์€ ์ž์‹ ๋ณด๋‹ค ํฌ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค. ์™ผ์ชฝ ์˜ค๋ฅธ์ชฝ ๋”ฐ์งˆ ํ•„์š” ์—†์ด ์ž์‹์œผ๋กœ ๋„ฃ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค. 

์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ ๊ทœ์น™ ๊ทธ๋Œ€๋กœ ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค. 

ํž™์„ ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.

 

์ž์‹์€ leftChild = parent*2, rightChild = parent*2+1

๋ถ€๋ชจ๋Š” parent = child/2

๋กœ ํ•˜๋ฉด ๋œ๋‹ค. 

 

heap struct pointer ์ฝ”๋“œ :

typedef struct heap {
  int arr[MAX_N];
  int size;
} heap;

 

insert ์‹œ O(nlog(n))

void insert(heap * hp, int data){
  int here = ++hp-> size;
  while((here!=1) && (data < hp->arr[here/2]){
    hp->arr[here] = hp->arr[here/2];
    here/=2;
  }
  hp->arr[here] = data;
}

 

delete ์‹œ O(nlog(n))

int delete(heap *hp){
  if(hp -> size == 0) return -1;
  int ret = hp -> arr[1];
  hp->arr[1] = hp->arr[hp->size--];
  int parent = 1;
  int child;
  while (1) {
    child = parent * 2;
    if (child + 1 <= hp->size && hp->arr[child]>hp->arr[child + 1]){
    	child++;
    }
    if (child>hp->size||hp->arr[child] > hp->arr[parent]) break;
    swap(&hp->arr[parent], &hp->arr[child]);
    parent = child;
  }
	return ret;
}

 

 

heap์„ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜ -> heapify

void heapify(int arr[], int here, int size){
  int left = here * 2 + 1;
  int right = here * 2 + 2;
  int max = here;
  if(left < size && arr[left] > arr[max]){
  	max = left;
  }
  if(right < size && arr[right] > arr[max]){
  	max = right;
  }
  if(max != here){
  	swap(&arr[here], &arr[max]);
  	heapify(arr, max, size);
  }
}

 

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ตœ๋Œ€ ํž™์€ ์•„๋‹ˆ๋‹ค. 4๊ฐ€ ๊ฐ€์žฅ ํฐ ๊ฐ’์ด ์•„๋‹ˆ๋ฏ€๋กœ 

๋”ฐ๋ผ์„œ ์ž์‹ ๋…ธ๋“œ๋ฅผ ๊ฐ€์ง„ ๋…ธ๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด heapify๋ฅผ ์—ญ์ˆœ์œผ๋กœ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด๋‹ค. -> buildHeap

void buildHeap(int arr[], int size) {
    int i,j;
    for (i = size / 2 - 1; i >= 0; i--) { //์ž์‹ ๊ฐ€์ง„ ๋ถ€๋ชจ๋…ธ๋“œ์— ํ•ด๋Œ€ 
        heapify(arr, i, size);
        for (j = 0; j < size; j++)
            printf("%d ", arr[j]);
        printf("\n\n");
    }
}

 

heap Sort ์ „์ฒด ์ฝ”๋“œ :

void heapSort(int arr[],int size) {
    int treeSize;
    buildHeap(arr, size);
    for (treeSize = size - 1; treeSize >= 0; treeSize--) {
        swap(&arr[0], &arr[treeSize]);
        heapify(arr, 0,treeSize);
    }
}

6. ํ€ต์ •๋ ฌ (quick sort) -> O(nlogn)

 

pivot์„ ๊ธฐ์ค€์œผ๋กœ ๋น„๋Œ€์นญ ๋ถ„ํ•  

pivot ๋ณด๋‹ค ์ž‘์€ ๊ทธ๋ฃน๊ณผ ํฐ ๊ทธ๋ฃน์œผ๋กœ ๋ถ„ํ• ํ•œ๋‹ค. ์›์†Œ๊ฐ€ 1๊ฐœ๊ฐ€ ๋˜์–ด ๋”์ด์ƒ ๋ถ„ํ•  ํ•  ์ˆ˜ ์—†์„ ๋Œ€๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๊ณ  ๋ถ„ํ•  ์™„๋ฃŒ ์‹œ ์ •๋ ฌ๋„ ์™„๋ฃŒ๊ฐ€ ๋œ๋‹ค.

 

์ฝ”๋“œ :

void quickSort(int arr, int left, int right){
	int pivot, i, j, temp;
    if(left < right){
    i = left;
    j = right + 1;
    pivot = arrp[left];
    
    do {
    	do { i++; } while (arr[i] < pivot);
        do { j--; } while (arr[i] > pivot);
        if(i < j){
        	temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    } while( i< j);
    temp = arr[left];
    arr[left] = arr[j];
    arr[j] = temp;
    
    quickSort(arr, left, j -i);
    quickSort(arr, j + 1, right);
    }
}

 

๋Œ“๊ธ€