Post

๐ŸฆŠ 5. Sorting

5. Sorting

๋ฒ„๋ธ” ์ •๋ ฌ (bubble sort)

  • ํ‰๊ท  ๋ฐ ์ตœ์•…์˜ ๊ฒฝ์šฐ O(n^2)
  • ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ O(1)

๋ฐฐ์—ด์˜ ์ฒซ ์›์†Œ๋ถ€ํ„ฐ ์ˆœ์ฐจ ์‹คํ–‰.

ํ˜„์žฌ ์›์†Œ๊ฐ€ ๊ทธ๋‹ค์Œ ์›์†Œ์˜ ๊ฐ’๋ณด๋‹ค ํฌ๋ฉด ๋‘ ์›์†Œ๋ฅผ ๋ฐ”๊พผ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
void bubbleSort(int[] arr) {
  int temp = 0;
	for(int i = 0; i < arr.length - 1; 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;
			}
		}
	}
}

์„ ํƒ ์ •๋ ฌ (selection sort)

  • ํ‰๊ท  ๋ฐ ์ตœ์•…์˜ ๊ฒฝ์šฐ O(n^2
  • ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ O(1)

๋ฐฐ์—ด์„ ์„ ํ˜• ํƒ์ƒ‰ํ•˜๋ฉฐ ๊ฐ€์žฅ ์ž‘์€ ์›์†Œ๋ฅผ ๋ฐฐ์—ด์˜ ๋งจ ์•ž์œผ๋กœ ๋ณด๋‚ธ๋‹ค.

๊ทธ ๋‹ค์Œ์—๋Š” ๋‘ ๋ฒˆ์งธ ์ž‘์€ ์›์†Œ๋ฅผ ์ฐพ์•„ 2๋ฒˆ์งธ ์ž๋ฆฌ๋กœ ๋ณด๋‚ธ๋‹ค.

์ด๋ฅผ ๋ชจ๋“  ์›์†Œ๊ฐ€ ์ •๋ ฌ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
    }
}

๋ณ‘ํ•ฉ ์ •๋ ฌ (merge sort)

  • ํ‰๊ท  ๋ฐ ์ตœ์•…์˜ ๊ฒฝ์šฐ O(n log n)
  • ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฆ„, ๋˜๋Š” O(n)

๋ฐฐ์—ด์„ ์ ˆ๋ฐ˜์”ฉ ๋‚˜๋ˆ„์–ด ๊ฐ๊ฐ์„ ์ •๋ ฌํ•œ ๋‹ค์Œ ์ด ๋‘˜์„ ํ•ฉ์ณ์„œ ๋‹ค์‹œ ์ •๋ ฌํ•œ๋‹ค.

๋‚˜๋ˆˆ ์ ˆ๋ฐ˜์„ ์ •๋ ฌํ•  ๋•Œ๋„ ๊ฐ™์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์‚ฌ์šฉ๋˜๊ณ  ๊ฒฐ๊ตญ์—๋Š” ์›์†Œ ํ•œ ๊ฐœ์งœ๋ฆฌ ๋ฐฐ์—ด ๋‘ ๊ฐœ๋ฅผ ๋ณ‘ํ•ฉํ•œ๋‹ค.

๋ณ‘ํ•ฉ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ๋ณต์žกํ•˜๋‹ค.

๋ณ‘ํ•ฉ ๋Œ€์ƒ์ด ๋˜๋Š” ๋ฐฐ์—ด์˜ ๋‘ ๋ถ€๋ถ„์„ ์ž„์‹œ ๋ฐฐ์—ด helper ์— ๋ณต์‚ฌํ•˜๊ณ 

์™ผ์ชฝ ์ ˆ๋ฐ˜์˜ ์‹œ์ž‘ ์ง€์  helperLeft ์™€ ์˜ค๋ฅธ์ชฝ ์ ˆ๋ฐ˜์˜ ์‹œ์ž‘ ์ง€์  helperRight ๋ฅผ ์ถ”์ ํ•œ๋‹ค.

์ดํ›„ helper ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ๋‘ ๋ฐฐ์—ด์—์„œ ๋” ์ž‘์€ ๊ฐ’์˜ ์›์†Œ๋ฅผ ๊บผ๋‚ด์–ด ์›๋ž˜ ๋ฐฐ์—ด์— ๋ณต์‚ฌํ•ด ๋„ฃ๋Š”๋‹ค.

๋‘ ๋ฐฐ์—ด ์ค‘ ํ•œ ๋ฐฐ์—ด์— ๋Œ€ํ•œ ์ˆœํšŒ๊ฐ€ ๋๋‚œ ๊ฒฝ์šฐ, ๋‹ค๋ฅธ ๋ฐฐ์—ด์˜ ๋‚จ์€ ๋ถ€๋ถ„์„ ์›๋ž˜ ๋ฐฐ์—ด์— ๋‚จ๊น€์—†์ด ๋ณต์‚ฌํ•ด ๋„ฃ๊ณ  ์ž‘์—…์„ ๋งˆ์นœ๋‹ค.

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
void mergesort(int[] array) {
	int[] helper = new int[array.length];
	mergesort(array, helper, 0, array.length - 1);
}

void mergesort(int[] array, int[] helper, int low, int high) {
	if (low < high) {
		int middle = (low + high) / 2;
		mergesort(array, helper, low, middle); // ์™ผ์ชฝ ์ ˆ๋ฐ˜ ์ •๋ ฌ
		mergesort(array, helper, middle + 1, high); //์˜ค๋ฅธ์ชฝ ์ ˆ๋ฐ˜ ์ •๋ ฌ
		sort(array, helper, low, middle, high); //๋ณ‘ํ•ฉ
	}
}

void merge(int[] array, int[] helper, int low, int middle, int high) {
	/** ์ ˆ๋ฐ˜์งœ๋ฆฌ ๋‘ ๋ฐฐ์—ด์„ helper ๋ฐฐ์—ด์— ๋ณต์‚ฌํ•œ๋‹ค */
	for (int i = low; i <= high; i++) {
		helper[i] = array[i];
	}

	int helperLeft = low;
	int helperRight = middle + 1;
	int current = low;

	while (helperLeft <= middle && helperRight <= high) {
		if (helper[helperLeft] <= helper[helperRight]) {
			array[current] = helper[helperLeft];
			helperLeft++;
		} else {
			array[current] = helper[helperRight];
			helperRight++;
		}
		current++;
	}

	int remaining = middle - helperLeft;
	for (int i = 0; i <= remaining; i++) {
		array[current + i] = helper[helperLeft + i];
	}
}

์˜ค๋ฅธ์ชฝ ์ ˆ๋ฐ˜์— ๋Œ€ํ•œ ๋‚จ์€ ์›์†Œ๋“ค์„ ์›๋ž˜ ๋ฐฐ์—ด๋กœ ๋‹ค์‹œ ์˜ฎ๊ธฐ๋Š” ์ฝ”๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋ฐ,

๊ทธ ์ด์œ ๋Š” ์˜ค๋ฅธ์ชฝ ์›์†Œ๋“ค์€ ์ด๋ฏธ ์›๋ž˜ ๋ฐฐ์—ด์˜ ๊ทธ ์ž๋ฆฌ์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

ํ€ต ์ •๋ ฌ (quick sort)

  • ํ‰๊ท  O(n log n)์ตœ์•… O(n^2)
  • ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ O(log n)

๋ฌด์ž‘์œ„๋กœ ์„ ์ •๋œ ํ•œ ์›์†Œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์„ ๋ถ„ํ• ํ•œ๋‹ค.

์„ ์ •๋œ ์›์†Œ๋ณด๋‹ค ์ž‘์€ ์›์†Œ๋“ค์€ ์•ž์œผ๋กœ, ํฐ ์›์†Œ๋“ค์€ ๋’ค๋กœ ๋ณด๋‚ธ๋‹ค.

๋ฐฐ์—ด๊ณผ ๊ทธ ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ๋ถ„ํ• ํ•ด ๋‚˜๊ฐ€๋ฉด ๊ฒฐ๊ตญ ๋ฐฐ์—ด์€ ์ •๋ ฌ๋œ ์ƒํƒœ์— ๋„๋‹ฌํ•œ๋‹ค.

๋ฐฐ์—ด ๋ถ„ํ• ์— ์‚ฌ์šฉ๋˜๋Š” ์›์†Œ๊ฐ€ ์ค‘๊ฐ„๊ฐ’ medium ์— ๊ฐ€๊นŒ์šด ๊ฐ’์ด ๋˜๋ฆฌ๋ผ๋Š” ๋ณด์žฅ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์—

์ตœ์•…์˜ ๊ฒฝ์šฐ ์ˆ˜ํ–‰ ์‹œ๊ฐ„์ด O(n^2) ๊ฐ€ ๋˜๊ธฐ๋„ ํ•œ๋‹ค.

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
void quickSort(int[] arr, int left, int right) {
	int index = partition(arr, left, right);
	if (left < index - 1) { //์™ผ์ชฝ ์ ˆ๋ฐ˜ ์ •๋ ฌ
		quickSort(arr, left, index - 1);
	}
	if (index < right) { //์˜ค๋ฅธ์ชฝ ์ ˆ๋ฐ˜ ์ •๋ ฌ
		quickSort(arr, index, right);
	}
}

int partition(int[] arr, int left, int right) {
	int pivot = arr[(left + right) / 2]; // ๋ถ„ํ•  ๊ธฐ์ค€ ์›์†Œ ์„ ์ •
	while (left <= right) {
		while (arr[left] < pivot) left++; //์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์˜ฎ๊ฒจ์•ผ ํ•˜๋Š” ์›์†Œ
		while (arr[right] > pivot) right--; //์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ์˜ฎ๊ฒจ์•ผ ํ•˜๋Š” ์›์†Œ

		// ์›์†Œ๋ฅผ ์Šค์™‘
		if (left <= right) {
			swap(arr, left, right); // ์Šค์™‘
			left++;
			right--;
		}
	}
	return left;
}

๊ธฐ์ˆ˜ ์ •๋ ฌ (radix sort)

  • ์‹คํ–‰ ์‹œ๊ฐ„ O(kn)

๊ธฐ์ˆ˜ ์ •๋ ฌ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์ •์ˆ˜์ฒ˜๋Ÿผ ์œ ํ•œ ๋น„ํŠธ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

๊ฐ ์ž๋ฆฟ์ˆ˜๋ฅผ ์ˆœํšŒ์— ๋‚˜๊ฐ€๋ฉฐ ๊ฐ ์ž๋ฆฌ์˜ ๊ฐ’์— ๋”ฐ๋ผ ๊ทธ๋ฃน์„ ๋‚˜๋ˆˆ๋‹ค.

๊ธฐ์ˆ˜ ์ •๋ ฌ์€ O(kn) ์˜ ๋ณต์žก๋„๋ฅผ ์ง€๋‹Œ๋‹ค. (n : ์›์†Œ์˜ ๊ฐœ์ˆ˜, k : ์ž๋ฆฟ์ˆ˜์˜ ๊ฐœ์ˆ˜)

This post is licensed under CC BY 4.0 by the author.