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

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

ArrayList ๋‚ด๋ถ€๊ตฌํ˜„ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ดํŽด๋ณด๊ธฐ (feat. Array)

๊ฐœ์š” ArrayList๋ฅผ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํŽธ์ธ๋ฐ ArrayList๋Š” ์™œ Array์™€ ๋‹ค๋ฅด๊ฒŒ ์ดˆ๊ธฐ ์‚ฌ์ด์ฆˆ๋ฅผ ์ •ํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๋˜๊ณ , ๋ฌดํ•œ์ •์œผ๋กœ ์›์†Œ๊ฐ€ ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์ด ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ๋„ ๋Š˜ ์˜๋ฌธ์ด์˜€์Šต๋‹ˆ๋‹ค. ์˜ค๋Š˜์€ ArrayList๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๋ถ€ํ•ด๋ณผ ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ์›์†Œ ์ถ”๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„์„ ์ค‘์ ์ ์œผ๋กœ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ArrayList ๋‚ด๋ถ€์ ์œผ๋กœ๋Š” ์–ด๋–ป๊ฒŒ ๋˜์–ด์žˆ์„๊นŒ? public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable { private static final long serialVersionUID = 8683452581122892189L; /** * Default..

๐ŸŒป 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/์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ž๋ฃŒ๊ตฌ์กฐ

์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์‚ฝ์ž… ์ •๋ ฌ (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/์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ž๋ฃŒ๊ตฌ์กฐ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก