Home /Algorithm/ ๐Ÿ’ฌ ์ •๋ ฌ
Post
Cancel

/Algorithm/ ๐Ÿ’ฌ ์ •๋ ฌ



1. ์ •๋ ฌ์•Œ๊ณ ๋ฆฌ์ฆ˜



์ •๋ ฌ


  • ์•ˆ์ •๋œ ์ •๋ ฌ
    ํ‚ค๊ฐ’์ด ๊ฐ™์€ ์š”์†Œ์˜ ์ˆœ์„œ๊ฐ€ ์ •๋ ฌ ์ „ํ›„์—๋„ ์œ ์ง€๋˜๋Š” ๊ฒƒ
  • ์•ˆ์ •๋˜์ง€ ์•Š์€ ์ •๋ ฌ
    ํ‚ค๊ฐ’์ด ๊ฐ™์€ ์š”์†Œ์˜ ์ˆœ์„œ๊ฐ€ ์ •๋ ฌ ์ „ํ›„์—๋„ ์œ ์ง€๋˜์ง€ ์•Š๋Š” ๊ฒƒ


  • ๋‚ด๋ถ€ ์ •๋ ฌ
    ์ •๋ ฌํ•  ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด์— ์ €์žฅํ•  ์ˆ˜ ์žˆ์„ ๋•Œ์— ์‚ฌ์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ์™ธ๋ถ€ ์ •๋ ฌ
    ์ •๋ ฌํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์„œ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด์— ์ €์žฅํ•  ์ˆ˜ ์—†์„ ๋•Œ์— ์‚ฌ์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜


  • ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ•ต์‹ฌ ์š”์†Œ
    ๊ตํ™˜, ์„ ํƒ, ์‚ฝ์ž…
    ๋Œ€๋ถ€๋ถ„์˜ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ด 3๊ฐ€์ง€ ์š”์†Œ๋ฅผ ์‘์šฉํ•œ ๊ฒƒ



2. ๋ฒ„๋ธ” ์ •๋ ฌ


bubble sort

  • ๋‹จ์ˆœ ๊ตํ™˜ ์ •๋ ฌ(straight exchange sort)
  • ์ด์›ƒํ•œ ๋‘ ์š”์†Œ์˜ ๋Œ€์†Œ ๊ด€๊ณ„๋ฅผ ๋น„๊ตํ•˜๊ณ  ํ•„์š”์— ๋”ฐ๋ผ ๊ตํ™˜์„ ๋ฐ˜๋ณตํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜


แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ



๋ฒ„๋ธ” ์ •๋ ฌ ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ


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
// ๋ฒ„๋ธ” ์ •๋ ฌ(๋‹จ์ˆœ ๊ตํ™˜ ์ •๋ ฌ) (๋ฒ„์ „ 1)

import java.util.Scanner;

class BubbleSort {

	/*
	๋ฒ„๋ธ”์ •๋ ฌ
	 */
	static void bubbleSort(int[] a, int n) {
		// ์ˆซ์ž๋Š” n๊ฐœ, ํŒจ์Šค ํšŸ์ˆ˜๋Š” n-1๋ฒˆ
		// ์ฒซ ๋ฒˆ์งธ ํŒจ์Šค์˜ ๋น„๊ต ํšŸ์ˆ˜๋Š” n - 1, ๋‘ ๋ฒˆ์งธ ํŒจ์Šค์˜ ๋น„๊ต ํšŸ์ˆ˜๋Š” n - 2ํšŒ ...
		// a[i], a[i + 1], โ€ฆ, a[n - 1]
		for (int i = 0; i < n - 1; i++) // ์ธ๋ฑ์Šค i ~ n-1๊นŒ์ง€ ๋ฐ˜๋ณต
			// ํŒจ์Šค : ๋‘๊ฐœ์”ฉ ๋น„๊ตํ•ด ํ•œ์ค„ ํ†ต๊ณผํ•  ๋•Œ
			for (int j = n - 1; j > i; j--) // ๋งจ๋’ค ์ธ๋ฑ์Šค n-1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์•ž์œผ๋กœ 2๊ฐœ์”ฉ ๋น„๊ต
				if (a[j - 1] > a[j])
					swap(a, j - 1, j);
	}

	/*
	๋ฐฐ์—ด ์š”์†Œ a[idx1]์™€ a[idx2]์˜ ๊ฐ’ ๊ตํ™˜
	 */
	static void swap(int[] a, int idx1, int idx2) {
		int t = a[idx1]; a[idx1] = a[idx2]; a[idx2] = t;
	}

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);

		System.out.println("๋ฒ„๋ธ” ์ •๋ ฌ(๋ฒ„์ „ 1)");
		System.out.print("์š”์†Ÿ์ˆ˜: ");
		int nx = stdIn.nextInt();
		int[] x = new int[nx];

		for (int i = 0; i < nx; i++) {
			System.out.print("x[" + i + "]: ");
			x[i] = stdIn.nextInt();
		}

		bubbleSort(x, nx);                // ๋ฐฐ์—ด x๋ฅผ ๋‹จ์ˆœ๊ตํ™˜์ •๋ ฌ

		System.out.println("์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ–ˆ์Šต๋‹ˆ๋‹ค.");
		for (int i = 0; i < nx; i++)
			System.out.println("x[" + i + "]=" + x[i]);
	}
}



์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐœ์„ ํ•˜๊ธฐ 1




์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐœ์„ ํ•˜๊ธฐ2




3. ๋‹จ์ˆœ ์„ ํƒ ์ •๋ ฌ




4. ๋‹จ์ˆœ ์‚ฝ์ž… ์ •๋ ฌ




๋‹จ์ˆœ ์ •๋ ฌ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„




5. ์…ธ ์ •๋ ฌ




๋‹จ์ˆœ์‚ฝ์ž… ์ •๋ ฌ์˜ ํŠน์ง•




์…ธ ์ •๋ ฌ




์ฆ๋ถ„๊ฐ’(h๊ฐ’) ์„ ํƒํ•˜๊ธฐ




6. ํ€ต ์ •๋ ฌ




ํ€ต ์ •๋ ฌ ์‚ดํŽด๋ณด๊ธฐ




๋ฐฐ์—ด์„ ๋‘ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ




ํ€ต ์ •๋ ฌ ๊ตฌํ˜„ํ•˜๊ธฐ




ํ€ต ์ •๋ ฌ์—์„œ ๋ฐฐ์—ด์„ ๋‚˜๋ˆ„๋Š” ๊ณผ์ • ์ถœ๋ ฅํ•˜๊ธฐ




๋น„์žฌ๊ท€์ ์ธ ํ€ต ์ •๋ ฌ ๊ตฌํ˜„ํ•˜๊ธฐ




์Šคํƒ์˜ ํฌ๊ธฐ ๊ตฌํ•˜๊ธฐ


๋ฐฉ๋ฒ•1. ์š”์†Ÿ์ˆ˜๊ฐ€ ๋งŽ์€ ๊ทธ๋ฃน์„ ๋จผ์ € ํ‘ธ์‹œํ•˜๋Š” ๊ฒฝ์šฐ

๋ฐฉ๋ฒ• 2. ์š”์†Ÿ์ˆ˜๊ฐ€ ์ ์€ ๊ทธ๋ฃน์„ ๋จผ์ € ํ‘ธ์‹œํ•˜๋Š” ๊ฒฝ์šฐ



ํ”ผ๋ฒ— ์„ ํƒํ•˜๊ธฐ




ํ€ต ์ •๋ ฌ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„ ๊ตฌํ•˜๊ธฐ




7. ๋ณ‘ํ•ฉ ์ •๋ ฌ



7-1. ์ •๋ ฌ์„ ๋งˆ์นœ ๋‘ ๋ฐฐ์—ด์˜ ๋ณ‘ํ•ฉ ์‚ดํŽด๋ณด๊ธฐ




7-2. ๋ณ‘ํ•ฉ ์ •๋ ฌ ๊ตฌํ˜„ํ•˜๊ธฐ




๋ณ‘ํ•ฉ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜




7-3. Arrays.sort๋กœ ํ€ต ์ •๋ ฌ๊ณผ ๋ณ‘ํ•ฉ ์ •๋ ฌํ•˜๊ธฐ




๊ธฐ๋ณธ ์ž๋ฃŒํ˜• ๋ฐฐ์—ด์˜ ์ •๋ ฌ(ํ€ต ์ •๋ ฌ)




ํด๋ž˜์Šค ๊ฐ์ฒด ๋ฐฐ์—ด์˜ ์ •๋ ฌ(๋ณ‘ํ•ฉ์ •๋ ฌ)




์ž์—ฐ๊ทธ๋Ÿฌ์šด ์ˆœ์„œ๋ฅผ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š์€ ๋ฐฐ์—ด์˜ ์ •๋ ฌ ๋งŒ๋“ค๊ธฐ




8. ํž™ ์ •๋ ฌ



8-1. ํž™์ด๋ž€?




ํŠธ๋ฆฌ




8-2. ํž™ ์ •๋ ฌ ์•Œ์•„๋ณด๊ธฐ




๋ฃจํŠธ๋ฅผ ์—†์• ๊ณ  ํž™ ์ƒํƒœ ์œ ์ง€ํ•˜๊ธฐ




ํž™ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ดํŽด๋ณด๊ธฐ




8-3. ๋ฐฐ์—ด์„ ํž™์œผ๋กœ ๋งŒ๋“ค๊ธฐ




downHeap ๋ฉ”์„œ๋“œ




heapSort ๋ฉ”์„œ๋“œ




ํž™ ์ •๋ ฌ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„




9. ๋„์ˆ˜ ์ •๋ ฌ



9-1. ๋„์ˆ˜์ •๋ ฌ ์•Œ์•„๋ณด๊ธฐ




1๋‹จ๊ณ„: ๋„์ˆ˜๋ถ„ํฌํ‘œ ๋งŒ๋“ค๊ธฐ




2๋‹จ๊ณ„: ๋ˆ„์ ๋ถ„ํฌํ‘œ ๋งŒ๋“ค๊ธฐ




3๋‹จ๊ณ„: ๋ชฉํ‘œ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ




4๋‹จ๊ณ„: ๋ฐฐ์—ด ๋ณต์‚ฌํ•˜๊ธฐ





(์ฐธ๊ณ )



๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ์—ฌ๋Ÿฌ๊ธ€๊ณผ ์ฑ… ์ฝ์€ ๋‚ด์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ ์ •๋ฆฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
์ข‹์€ ๊ธ€๋กœ ์ €์˜ ๊ณต๋ถ€์— ๋„์›€์„ ์ฃผ์‹œ๋Š” ๋ถ„๋“ค๊ป˜ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

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

/Web/ JWT

โœจTIL - ๊ฐœ์ธํ”„๋กœ์ ํŠธ ์‹œ์ž‘