티스토리 뷰
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
49
|
package me.kingsubin.studyrepo.book.algorithm.section5;
public class findMaxMin {
public static void main(String[] args) {
int[] A = {24, 75, 92, 83, 61, 48, 97, 50};
int[] answer;
answer = findMaxMin(A, 0, A.length - 1);
System.out.println("Min: " + answer[0]);
System.out.println("Max: " + answer[1]);
}
// (Divide And Conquer) find the min and max in array A[i....j]
public static int[] findMaxMin(int[] A, int i, int j) {
int mid;
int[] result = new int[2];
int[] leftResult;
int[] rightResult;
if (i == j) { // size 가 1인 경우
result[0] = A[i];
result[1] = A[j];
} else if (i == j - 1) { // size 가 2인 경우
if (A[i] < A[j]) {
result[0] = A[i];
result[1] = A[j];
} else {
result[0] = A[j];
result[1] = A[i];
}
} else {
// 재귀 형식으로 계속 left, right 정복해나감
mid = (i + j) / 2; // 배열의 중간 부분 변수로
leftResult = findMaxMin(A, i, mid);
rightResult = findMaxMin(A, mid + 1, j);
if (leftResult[0] < rightResult[0]) {
result[0] = leftResult[0];
}
if (leftResult[1] < rightResult[1]) {
result[1] = rightResult[1];
}
}
return result;
}
}
|
cs |
Section 5에서는 분할정복에 대해서 다룬다.
일반적으로 알고있는 quick sort 가 떠오른다.
출처: 자바로 쉽게 배우는 알고리즘
'책 > misc' 카테고리의 다른 글
HTTP 완벽 가이드 책 샀다. (2) | 2022.02.10 |
---|---|
프로그래밍 면접 이렇게 준비한다 책 샀다. (0) | 2021.08.29 |
Section5. 빠른 정렬 (0) | 2021.08.06 |
SQL 첫걸음 책 샀다. (0) | 2021.07.25 |
알고리즘 책 샀다. (1) | 2021.04.21 |
링크
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- dreamcoding
- js array
- 패스트캠퍼스 컴퓨터공학 완주반
- 이펙티브자바 스터디
- 프로그래머스
- BOJ
- Spring Security
- ㅇㄷㅇㅈ
- HTTP 완벽가이드
- REST API
- 프로그래머스 SQL
- java
- 백기선 스터디
- js api
- HTTP 완벽 가이드
- 이펙티브자바 아이템60
- 이펙티브자바 아이템59
- 이펙티브자바
- 백준
- JPA 연관관계 매핑
- 드림코딩
- 모던자바스크립트
- JS 딥다이브
- GCP
- 킹수빈닷컴
- http
- 김영한 JPA
- 김영한 http
- js promise
- 가상 면접 사례로 배우는 대규모 시스템 설계 기초
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함