--- id: java-algorithms title: "Java Algorithms" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["Collections class algorithms", "자바 알고리즘"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.88 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["java", "programming", "w3schools", "algorithms", "collections"] raw_sources: ["https://www.w3schools.com/java/java_algorithms.asp"] applied_in: [] github_commit: "" --- # [[Java Algorithms]] ## 🎯 한 줄 통찰 (One-line insight) `Collections.binarySearch()` REQUIRES the list to already be sorted — the example explicitly calls `Collections.sort(names)` immediately before `binarySearch()`, since binary search's speed advantage depends entirely on the data being in order; searching an unsorted list with it gives undefined/incorrect results. [S1] ## 🧠 핵심 개념 (Core concepts) - **Algorithm** — a procedure to solve a problem (search, sort, manipulate data structures). [S1] - **`Collections` class** — provides built-in algorithms in `java.util`, avoiding the need to hand-write them. [S1] - **`Collections.binarySearch(list, key)`** — fast search, but requires a pre-sorted list. [S1] - **`Collections.sort(list)`** / **`sort(list, reverseOrder())`** — ascending/descending sort. [S1] - **`Collections.max()` / `min()`** — largest/smallest element. [S1] - **`Collections.shuffle()`** — randomly reorders elements. [S1] - **`Collections.frequency(list, element)`** — counts occurrences of a value. [S1] - **`Collections.swap(list, i, j)`** — swaps two elements by index. [S1] ## 📖 세부 내용 (Details) - Binary search (must sort first): `Collections.sort(names); int index = Collections.binarySearch(names, "Angie");`. [S1] - Max/min: `Collections.max(numbers); Collections.min(numbers);`. [S1] - Shuffle: `Collections.shuffle(cards);`. [S1] - Frequency: `int count = Collections.frequency(fruits, "Banana");`. [S1] - Swap: `Collections.swap(fruits, 0, 2); // Swap first and third element`. [S1] ## ⚖️ 모순 및 업데이트 (Contradictions & updates) - **정렬 선행 조건**: binarySearch()는 리스트가 반드시 정렬되어 있어야 정확히 동작한다는 점이 예제에서 명시적으로 지켜짐. [S1] ## 🛠️ 적용 사례 (Applied in summary) 현재 발견된 실제 적용 사례가 없습니다 — 카드 셔플(Collections.shuffle)이나 과일 빈도 계산(frequency)이 실전 알고리즘 활용의 대표 사례다. [S1] ## 💻 코드 패턴 (Code patterns) Sort-then-binarySearch pattern (Java): ```java ArrayList names = new ArrayList<>(); names.add("Liam"); names.add("Jenny"); names.add("Kasper"); names.add("Angie"); Collections.sort(names); // required before binarySearch int index = Collections.binarySearch(names, "Angie"); ``` ## ✅ 검증 상태 및 신뢰도 - **상태:** draft - **검증 단계:** conceptual - **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body) - **신뢰 점수:** 0.88 - **중복 검사 결과:** 신규 생성 (New discovery) ## 🔗 지식 그래프 (Knowledge Graph) - **상위/루트:** [[Java Tutorial]] - **관련 개념:** [[Java Sort List]], [[Java Iterator]], [[Java Advanced Sorting]] - **참조 맥락:** Collections 클래스의 내장 알고리즘 — 고급 정렬(Advanced Sorting) 챕터로 이어짐. ## 📚 출처 (Sources) - [S1] W3Schools — Java Algorithms — https://www.w3schools.com/java/java_algorithms.asp ## 📝 변경 이력 (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "Java Algorithms" page (Astra wiki-curation, P-Reinforce v3.1 format).