๐ŸŒป JAVA

๐ŸŒป JAVA/์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ž๋ฃŒ๊ตฌ์กฐ

์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์ด๋ถ„ ํƒ์ƒ‰ (Binary Search)

์•ˆ๋…•ํ•˜์„ธ์š”, ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ •๋ฆฌํ•˜๋Š” ํฌ์ŠคํŒ…์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ฐธ๊ณ ํ•˜์‹œ๋ ค๋ฉด ํ•ด๋‹น ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์ฃผ์„ธ์š”. ๐Ÿ˜Š '๐ŸŒป JAVA/์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ž๋ฃŒ๊ตฌ์กฐ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก ๊ณต๋ถ€ํ•œ ๊ฒƒ๋“ค ์ •๋ฆฌํ•œ ๋‚ด์šฉ์„ ํฌ์ŠคํŒ…ํ•ฉ๋‹ˆ๋‹ค. iseunghan.tistory.com ์ด๋ถ„ ํƒ์ƒ‰ (Binary Search) ์ด๋ถ„ ํƒ์ƒ‰์€ ํƒ์ƒ‰ ๋ฒ”์œ„๋ฅผ ๋‘๊ฐœ๋กœ ๋‚˜๋ˆ ์„œ ํƒ์ƒ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ›จ์”ฌ ๋น ๋ฅธ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ์ฒ˜์Œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ํƒ์ƒ‰ : O(N) ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(log N) ๊ณผ์ • 0. ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฐ์—ด, ์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฐ’ : 9 (์ดํ•˜ Target) 1. ๋จผ์ € ๋ฐฐ์—ด์„ ์ •๋ ฌ์‹œํ‚ต๋‹ˆ๋‹ค. 2. ์ธ๋ฑ์Šค L, R, MID๋ฅผ ์ง€์ •ํ•ด์ค๋‹ˆ๋‹ค. 3. if (Target == arr[M]) if(target == arr[M]) { return M; } 4. el..

๐ŸŒป JAVA/์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ž๋ฃŒ๊ตฌ์กฐ

์•Œ๊ณ ๋ฆฌ์ฆ˜ - ๋ฒ„๋ธ” ์ •๋ ฌ (Bubble Sort)

์•ˆ๋…•ํ•˜์„ธ์š”, ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ •๋ฆฌํ•˜๋Š” ํฌ์ŠคํŒ…์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ฐธ๊ณ ํ•˜์‹œ๋ ค๋ฉด ํ•ด๋‹น ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์ฃผ์„ธ์š”. ๐Ÿ˜Š '๐ŸŒป JAVA/์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ž๋ฃŒ๊ตฌ์กฐ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก ๊ณต๋ถ€ํ•œ ๊ฒƒ๋“ค ์ •๋ฆฌํ•œ ๋‚ด์šฉ์„ ํฌ์ŠคํŒ…ํ•ฉ๋‹ˆ๋‹ค. iseunghan.tistory.com ๋ฒ„๋ธ” ์ •๋ ฌ (Bubble Sort) ๋ฒ„๋ธ” ์ •๋ ฌ์€ ์ธ์ ‘ํ•œ ๋‘ ์›์†Œ๋ฅผ ๋น„๊ตํ•ด ์ž๋ฆฌ๋ฅผ ๋ฐ”๊ฟ”๋‚˜๊ฐ€๋ฉด์„œ ์ •๋ ฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(N^2) ๊ณต๊ฐ„ ๋ณต์žก๋„ : O(N) ์•ˆ์ • ์ •๋ ฌ ๊ณผ์ • ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ์›์†Œ๊นŒ์ง€ ์ฐจ๋ก€๋Œ€๋กœ ์ธ์ ‘ํ•œ ์›์†Œ๋ฅผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค. ์„œ๋กœ ๋น„๊ตํ•˜์—ฌ ์•ž ์›์†Œ๊ฐ€ ๋” ํฌ๋‹ค๋ฉด ์„œ๋กœ ์œ„์น˜๋ฅผ ๊ตํ™˜ํ•ด์ค๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•œ ์‚ฌ์ดํด์ด ์™„๋ฃŒ๊ฐ€ ๋˜๋ฉด, ํ•ด๋‹น ์‚ฌ์ดํด์— ๋งˆ์ง€๋ง‰์— ์œ„์น˜์‹œํ‚จ ์›์†Œ๋Š” ์ •๋ ฌ์ด ์™„๋ฃŒ๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์ค‘์š” : ์•„๋ž˜์—์„œ ์„ค๋ช…) Code ..

๐ŸŒป JAVA/์ •๋ฆฌ์ •๋ฆฌ์ •๋ฆฌ

[JAVA] ๋ฌธ์ž์—ด ์—ฌ๋Ÿฌ ๊ฐœ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๊ธฐ (์ •๊ทœํ‘œํ˜„์‹)

์„œ๋ก  ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€๋‹ค๊ฐ€, 2๊ฐœ ์ด์ƒ์˜ ์—ฌ๋Ÿฌ ๊ฐœ ๊ณต๋ฐฑ์„ ํ•˜๋‚˜์˜ ๊ณต๋ฐฑ์œผ๋กœ ๋ฐ”๊ฟ”์•ผ ํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋Š”๋ฐ, ์ด ์ฐธ์— ์ •๋ฆฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ œ "๋‚ด์šฉ์—์„œ 2๊ฐœ ์ด์ƒ์˜ ๊ณต๋ฐฑ์„ ํ•˜๋‚˜์˜ ๊ณต๋ฐฑ์œผ๋กœ ๋ฐ”๊ฟ” ์ฃผ์„ธ์š”." ์›ํ•˜๋Š” ๊ฒฐ๊ณผ "๋‚ด์šฉ์—์„œ 2๊ฐœ ์ด์ƒ์˜ ๊ณต๋ฐฑ์„ ํ•˜๋‚˜์˜ ๊ณต๋ฐฑ์œผ๋กœ ๋ฐ”๊ฟ” ์ฃผ์„ธ์š”." ์ •๊ทœํ‘œํ˜„์‹ String str = "๋‚ด์šฉ์—์„œ 2๊ฐœ ์ด์ƒ์˜ ๊ณต๋ฐฑ์„ ํ•˜๋‚˜์˜ ๊ณต๋ฐฑ์œผ๋กœ ๋ฐ”๊ฟ” ์ฃผ์„ธ์š”."; String result = str.replaceAll("\\s+", " "); System.out.print(result); ์‹คํ–‰ ๊ฒฐ๊ณผ ์•ž์œผ๋กœ ์ •๊ทœ ํ‘œํ˜„์‹์— ๋Œ€ํ•ด์„œ ์ž˜ ์•Œ๊ณ ์žˆ์œผ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๐ŸŒป JAVA/์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ž๋ฃŒ๊ตฌ์กฐ

์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์‚ฝ์ž… ์ •๋ ฌ (Insertion Sort)

์•ˆ๋…•ํ•˜์„ธ์š”, ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ •๋ฆฌํ•˜๋Š” ํฌ์ŠคํŒ…์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ฐธ๊ณ ํ•˜์‹œ๋ ค๋ฉด ํ•ด๋‹น ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์ฃผ์„ธ์š”. ๐Ÿ˜Š '๐ŸŒป JAVA/์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ž๋ฃŒ๊ตฌ์กฐ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก ๊ณต๋ถ€ํ•œ ๊ฒƒ๋“ค ์ •๋ฆฌํ•œ ๋‚ด์šฉ์„ ํฌ์ŠคํŒ…ํ•ฉ๋‹ˆ๋‹ค. iseunghan.tistory.com ์‚ฝ์ž… ์ •๋ ฌ (Insertion Sort) ์‚ฝ์ž… ์ •๋ ฌ์€ 2 ๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ์›์†Œ๊นŒ์ง€ ์ˆœ์ฐจ์ ์œผ๋กœ, ํ•ด๋‹น ์›์†Œ๋ถ€ํ„ฐ 1 ๋ฒˆ์งธ ์›์†Œ๊นŒ์ง€ ๋น„๊ต๋ฅผ ํ•˜๋ฉฐ ์‚ฝ์ž…ํ•ด ๋‚˜๊ฐ€๋Š” ์ •๋ ฌ์ž…๋‹ˆ๋‹ค. ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(N^2) ๊ณต๊ฐ„ ๋ณต์žก๋„ : O(N) ์•ˆ์ • ์ •๋ ฌ ๊ณผ์ • 2 ๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ๋งˆ์ง€๋ง‰ ์›์†Œ๊นŒ์ง€ ์ˆœ์ฐจ์ ์œผ๋กœ ์ •๋ ฌ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 1๋ฒˆ ๊ณผ์ •์—์„œ i ๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ–ˆ๋‹ค๋ฉด, i ๋ฒˆ์งธ๋ถ€ํ„ฐ 1 ๋ฒˆ์งธ ์›์†Œ๊นŒ์ง€ ๋น„๊ต๋ฅผ ํ•˜๋ฉฐ ์‚ฝ์ž…ํ•ด ๋‚˜์•„๊ฐ€๋Š” ์ •๋ ฌ์ž…๋‹ˆ๋‹ค. ์•ž์—์„œ ์ด๋ฏธ ์ •๋ ฌ์„ ์™„๋ฃŒํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ..

๐ŸŒป JAVA/์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ž๋ฃŒ๊ตฌ์กฐ

์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„ ํƒ ์ •๋ ฌ (Selection Sort)

์•ˆ๋…•ํ•˜์„ธ์š”, ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ •๋ฆฌํ•˜๋Š” ํฌ์ŠคํŒ…์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ฐธ๊ณ ํ•˜์‹œ๋ ค๋ฉด ํ•ด๋‹น ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์ฃผ์„ธ์š”. ๐Ÿ˜Š '๐ŸŒป JAVA/์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ž๋ฃŒ๊ตฌ์กฐ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก ๊ณต๋ถ€ํ•œ ๊ฒƒ๋“ค ์ •๋ฆฌํ•œ ๋‚ด์šฉ์„ ํฌ์ŠคํŒ…ํ•ฉ๋‹ˆ๋‹ค. iseunghan.tistory.com ์„ ํƒ ์ •๋ ฌ (Selection Sort) ์ •๋ ฌํ•˜์—ฌ ์›์†Œ๋ฅผ ๋ฐฐ์น˜ํ•  ์ž๋ฆฌ๋ฅผ ๋ฏธ๋ฆฌ ์ •ํ•ด๋†“๊ณ , ํ•ด๋‹น ์ž๋ฆฌ์— ๋„ฃ์„ ์›์†Œ๋ฅผ ์„ ํƒํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ๊ฐœ์ธ์ ์œผ๋กœ ์‚ฝ์ž… ์ •๋ ฌ๊ณผ ํ—ท๊ฐˆ๋ฆฐ ์ •๋ ฌ,, ์„ ํƒ ์ •๋ ฌ์€ ๋„ฃ์„ ์ž๋ฆฌ๋ฅผ ๋ฏธ๋ฆฌ ์ง€์ •ํ•˜๊ณ , ์‚ฝ์ž… ์ •๋ ฌ์€ 1 ๋ฒˆ์งธ๋ถ€ํ„ฐ n๋ฒˆ์งธ ์›์†Œ๋ฅผ ์•ž์˜ ์›์†Œ์™€ ๋น„๊ตํ•˜๋ฉฐ ์‚ฝ์ž…ํ•ด ๋‚˜๊ฐ€๋Š” ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค. (ํ—ท๊ฐˆ๋ฆผ..) ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(N^2) ๊ณต๊ฐ„ ๋ณต์žก๋„ : O(N) ๋ถˆ์•ˆ์ • ์ •๋ ฌ ๊ณผ์ • ๋จผ์ € 1 ๋ฒˆ์งธ ์ž๋ฆฌ์— ์˜ฌ ์›์†Œ๋ฅผ ์„ ํƒํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰..

๐ŸŒป JAVA/์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ž๋ฃŒ๊ตฌ์กฐ

์•Œ๊ณ ๋ฆฌ์ฆ˜ - ํ€ต ์ •๋ ฌ (Quick-Sort)

์•ˆ๋…•ํ•˜์„ธ์š”, ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ •๋ฆฌํ•˜๋Š” ํฌ์ŠคํŒ…์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ฐธ๊ณ ํ•˜์‹œ๋ ค๋ฉด ํ•ด๋‹น ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์ฃผ์„ธ์š”. ๐Ÿ˜Š '๐ŸŒป JAVA/์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ž๋ฃŒ๊ตฌ์กฐ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก ๊ณต๋ถ€ํ•œ ๊ฒƒ๋“ค ์ •๋ฆฌํ•œ ๋‚ด์šฉ์„ ํฌ์ŠคํŒ…ํ•ฉ๋‹ˆ๋‹ค. iseunghan.tistory.com Quick Sort Quick Sort๋Š” real-world ๋ฐ์ดํ„ฐ์—์„œ ๋น ๋ฅด๋‹ค๊ณ  ์•Œ๋ ค์ ธ ๊ฐ€์žฅ ๋งŽ์ด ์“ฐ๋Š” ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. pivot์ด๋ผ๋Š” ๊ฒƒ์„ ์ง€์ •ํ•˜์—ฌ pivot ์™ผ์ชฝ์œผ๋กœ๋Š” pivot๋ณด๋‹ค ์ž‘์€ ๊ฐ’๋“ค์„, ์˜ค๋ฅธ์ชฝ์—๋Š” pivot๋ณด๋‹ค ํฐ ๊ฐ’๋“ค์„ ์žฌ๋ฐฐ์น˜ํ•˜๋ฉฐ, ๊ณ„์†ํ•˜์—ฌ ๋ถ„ํ• ํ•˜์—ฌ ์ •๋ ฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. Quick Sort๋Š” ๋ถ„ํ•  ์ •๋ณต ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. Merge Sort์™€ ๋‹ฌ๋ฆฌ Quick Sort๋Š” ๋น„๊ท ๋“ฑํ•˜๊ฒŒ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ๋ถˆ์•ˆ์ • ์ •๋ ฌ์— ์†ํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ฐ„ ๋ณต์žก๋„ : ..

iseunghan
'๐ŸŒป JAVA' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (3 Page)