Post

๐Ÿข 5. Sorting

๐Ÿซง ๋ฒ„๋ธ” ์ •๋ ฌ (Bubble Sort)

: ๋ฐ”๋กœ ๊ฐ€๊นŒ์ด์— ์žˆ๋Š” ๋‘ ์ˆซ์ž๋ผ๋ฆฌ ๋น„๊ต๋ฅผ ํ•ด์„œ ๋‹น์žฅ
๋” ์ž‘์€ ์ˆซ์ž๋ฅผ ์•ž์œผ๋กœ ๋ณด๋‚ด์ฃผ๋Š” ๊ฒƒ์„ ๋ฐ˜๋ณต
๊ตฌํ˜„์€ ์‰ฝ์ง€๋งŒ ๊ฐ€์žฅ ๋น„ํšจ์œจ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜
์‹คํ–‰ ์ˆ˜ํ–‰์‹œ๊ฐ„ ๊ฐ€์žฅ ๋Š๋ฆฌ๋‹ค

๋ฒ„๋ธ” ์ •๋ ฌ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” N^2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

int main(void) {
	int i, j, temp;
	int array[10] = {1, 10, 5, 8, 7, 6, 4, 3, 2, 9};
	for (i = 0; i< 10; i ++) {
		for (j = 0 ; j< 10; j++){
			if (array[j] > array[j+1]) {
				temp = array[j];
				array[j] = array[j+1];
				array[j+1] = temp;
			}
		}
}
	for (i = 0; i<10; i++) {
		printf("%d " , array[i]);
	}
	return 0;
}
// 1 2 3 4 5 6 7 8 9 10

๐Ÿซง ๋ณ‘ํ•ฉ ์ •๋ ฌ (Merge Sort)

: ๋ถ„ํ•  ์ •๋ณต ๋ฐฉ๋ฒ•์„ ์ฑ„ํƒํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜
ํ€ต ์ •๋ ฌ๊ณผ ๋™์ผํ•œ ์‹œ๊ฐ„ ๋ณต์žก๋„ ๊ฐ€์ง
ํ€ต ์ •๋ ฌ์€ ํ”ผ๋ฒ— ๊ฐ’์— ๋”ฐ๋ผ ํŽธํ–ฅ๋˜๊ฒŒ ๋ถ„ํ• ๋จ
๋ณ‘ํ•ฉ ์ •๋ ฌ์€ ์ •ํ™•ํžˆ ๋ฐ˜์ ˆ์”ฉ ๋‚˜๋ˆˆ๋‹ค๋Š” ์ ์—์„œ ์ตœ์•…์˜ ๊ฒฝ์šฐ์—๋„ N*logN ๋ณด์žฅ
ํ”ผ๋ฒ— ๊ฐ’์ด ์—†๊ณ  ํ•ญ์ƒ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆˆ๋‹ค
์ด ๋‹จ๊ณ„์˜ ํฌ๊ธฐ๊ฐ€ logN์ด ๋˜๋„๋ก ๋งŒ๋“ค์–ด์คŒ
ํ•ฉ์น˜๋Š” ์ˆœ๊ฐ„์— ์ •๋ ฌ ์ˆ˜ํ–‰ํ•˜๊ธฐ
๋„ˆ๋น„๊ฐ€ N๋ฒˆ, ๋†’์ด๊ฐ€ logN
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>

int number = 8;
int sorted[8]; // ์ •๋ ฌ ๋ฐฐ์—ด์€ ๋ฐ˜๋“œ์‹œ ์ „์—ญ๋ณ€์ˆ˜๋กœ ์„ ์–ธ

void merge(int a[], int m, int middle, int n) {
	int i = m;
	int j = middle + 1;
	int k = m;
	// ์ž‘์€ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์—ด์— ์‚ฝ์ž…
	while (i <= middle && j <= n)  {
		if (a[i] <= a[j]) {
			sorted[k] = a[i];
			i++;
		}
		else {
			sorted[k] = a[j];
			j ++;
		}
		k++;
	}
	// ๋‚จ์€ ๋ฐ์ดํ„ฐ ์‚ฝ์ž…
	if (i > middle) {
		for (int t =j; t<=n;t++) {
			sorted[k] = a[t];
			k++;
		}
	} else {
		for (int t = i; t <= middle; t++) {
			sorted[k] = a[t];
			k++;
		}
	}
	// ์ •๋ ฌ๋œ ๋ฐฐ์—ด ์‚ฝ์ž…
	for (int t =m; t <=n; t++) {
		a[t] = sorted[t];
	}
}

void mergeSort(int a[], int m, int n) {
	// ํฌ๊ธฐ๊ฐ€ 1 ๋ณด๋‹ค ํด ๊ฒฝ์šฐ
	if (m <n) {
		int middle = (m + n) /2;
		mergeSort(a, m, middle);
		mergeSort(a, middle +1, n);
		merge(a, m ,middle, n);
	}
}

int main(void) {
	int array[number] = {7,6,5,8,3,5,9,1};
	mergeSort(array, 0, number-1);
	for (int i=0; i <= number; i ++) {
		printf("%d ", array[i]);
	}
}
// 1 3 5 5 6 7 8 9

๐Ÿซง ํ€ต ์ •๋ ฌ (Quick Sort)

: ํŠน์ •ํ•œ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ํฐ ์ˆซ์ž์™€ ์ž‘์€ ์ˆซ์ž๋ฅผ ์„œ๋กœ ๊ตํ™˜ํ•œ ๋’ค์— ๋ฐฐ์—ด์„ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆˆ๋‹ค
ํ€ต ์ •๋ ฌ์—๋Š” ๊ธฐ์ค€๊ฐ’์ด ์žˆ๋‹ค -> ํ”ผ๋ฒ—(Pivot)
์ฒซ๋ฒˆ์งธ ์›์†Œ๋ฅผ ํ”ผ๋ฒ— ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ์‚ฌ์šฉ

ํ€ต ์ •๋ ฌ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” N*logN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>

int number = 10;
int data[10] = {1, 10, 5, 8, 7, 6, 4, 3, 2, 9};

void quickSort(int* data, int start, int end) {
	if (start >= end) {		// ์›์†Œ๊ฐ€ 1๊ฐœ์ธ ๊ฒฝ์šฐ ๊ทธ๋Œ€๋กœ ๋‘๊ธฐ
		return;
	}

	int key = start; // ํ‚ค๋Š” ์ฒซ๋ฒˆ์งธ ์›์†Œ (pivot)
	int i = start + 1;
	int j = end;
	int temp;

	while (i <= j)  {	// ์—‡๊ฐˆ๋ฆด ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
		while (i <=end && data[i] <= data[key]) {	// ํ‚ค ๊ฐ’๋ณด๋‹ค ํฐ ๊ฐ’์„ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€
			i++;
		}
		while ( j > start && data[j] >= data[key]) {		// ํ‚ค ๊ฐ’๋ณด๋‹ค ์ž‘์€ ๊ฐ’์„ ๋งŒ๋‚  ๋–„๊นŒ์ง€
			j--;
		}
		if (i > j) {	// ํ˜„์žฌ ์—‡๊ฐˆ๋ฆฐ ์ƒํƒœ๋ฉด ํ‚ค ๊ฐ’๊ณผ ๊ต์ฒด
			temp = data[j];
			data[j] = data[key];
			data[key] = temp;
		}
		else {	// ์—‡๊ฐˆ๋ฆฌ์ง€ ์•Š์•˜๋‹ค๋ฉด i์™€ j ๊ต์ฒด
			temp = data[j];
			data[j] = data[i];
			data[i] = temp;
		}
	}
	quickSort(data, start, j -1);
	quickSort(data, j+1, end);
}

int main(void) {
	quickSort(data, 0, number - 1);
	for (int i = 0; i< number; i++) {
		printf("%d ", data[i]);
	}
}
// 1 2 3 4 5 6 7 8 9 10

๐Ÿซง ๊ธฐ์ˆ˜ ์ •๋ ฌ (radix)

: ์ฃผ์–ด์ง„ ์ˆ˜ ๋“ค๊ฐ„์˜ ๋น„๊ต๋ฅผ ํ•˜์ง€ ์•Š๊ณ  ๋ฒ„ํ‚ท์„ ์‚ฌ์šฉํ•ด ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ, ๋‚ฎ์€ ์ž๋ฆฌ(1์˜ ์ž๋ฆฌ)์—์„œ ๋†’์€ ์ž๋ฆฌ(10^n ์ž๋ฆฌ) ์ˆœ์œผ๋กœ ๋ฒ„ํ‚ท์— ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.

์›์†Œ๊ฐ’์ด ๋ชจ๋‘ k ์ž๋ฆฟ์ˆ˜ ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ธ ํŠน์ˆ˜ํ•œ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋ ฌ๋ฐฉ๋ฒ•์ด๋‹ค.

  1. ๊ฐ€์žฅ ๋‚ฎ์€ 1์˜ ์ž๋ฆฟ์ˆ˜๋งŒ ๊ฐ€์ง€๊ณ  ๋ชจ๋“  ์ˆ˜๋ฅผ ์ •๋ ฌํ•œ๋‹ค.
  2. ๊ทธ ๋‹ค์Œ์œผ๋กœ ๋‚ฎ์€ 10์˜ ์ž๋ฆฟ์ˆ˜๋งŒ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
  3. ์ด ๊ณผ์ •์„ ๋งˆ์ง€๋ง‰ ์ž๋ฆฟ์ˆ˜๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from collections import deque

def radixSort():
    nums = list(map(int, input().split(' ')))
    buckets = [deque() for _ in range(10)]

    max_val = max(nums)
    queue = deque(nums)
    digit = 1 # ์ž๋ฆฟ์ˆ˜

    while (max_val >= digit): # ๊ฐ€์žฅ ํฐ ์ˆ˜์˜ ์ž๋ฆฟ์ˆ˜์ผ ๋•Œ ๊นŒ์ง€๋งŒ ์‹คํ–‰
        while queue:
            num = queue.popleft()
            buckets[(num // digit) % 10].append(num) # ๊ฐ ์ž๋ฆฌ์˜ ์ˆ˜(0~9)์— ๋”ฐ๋ผ ๋ฒ„ํ‚ท์— num์„ ๋„ฃ๋Š”๋‹ค.

        # ํ•ด๋‹น ์ž๋ฆฟ์ˆ˜์—์„œ ๋ฒ„ํ‚ท์— ๋‹ค ๋„ฃ์—ˆ์œผ๋ฉด, ๋ฒ„ํ‚ท์— ๋‹ด๊ฒจ์žˆ๋Š” ์ˆœ์„œ๋Œ€๋กœ ๊บผ๋‚ด์™€ ์ •๋ ฌํ•œ๋‹ค.
        for bucket in buckets:
            while bucket:
                queue.append(bucket.popleft())
        print(digit,"์˜ ์ž๋ฆฟ ์ˆ˜ ์ •๋ ฌ: ", list(queue))
        digit *= 10 # ์ž๋ฆฟ์ˆ˜ ์ฆ๊ฐ€์‹œํ‚ค๊ธฐ

    print(list(queue))
This post is licensed under CC BY 4.0 by the author.