250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 자바
- 스프링부트와AWS로혼자구현하는웹서비스
- vue.js
- 이펙티브 자바
- react
- 네트워크
- DDD
- 알고리즘분석
- 자바예외
- 이팩티브 자바
- 알고리즘
- 인덱스
- jpa
- MariaDB
- 자료구조
- 기술면접
- 인프런김영한
- java
- 인프런백기선
- 클린코드
- 자바스터디
- mysql
- 혼공SQL
- 도메인 주도 개발 시작하기
- 이펙티브자바
- AWS RDS
- AWS
- SQL쿡북
- CleanCode
- aop
Archives
- Today
- Total
기록이 힘이다.
알고리즘 분석(SET, INDEXOF, ADD, REMOVE) 본문
728x90
프로파일링 또는 성능 분석은 프로그램의 시간 복잡도 및 공간, 특정 명령어 이용, 함수 호출의 주기와 빈도 등을 측정하는 동적 프로그램 분석의 한 형태이다. 참조: 위키피디아
문제점, 모두 구현해봐야 하고 컴퓨터 성능에 의존, 데이터에 의존.
해결책 : 알고리즘 분석
구현하지 않고도 알고리즘을 비교
간단한 알고리즘은 몇 가지 범주로 구분상수 시간, 선형, 이차
선택 정렬
/**
*
*/
package com.allendowney.thinkdast;
import java.util.Arrays;
/**
* @author downey
*
*/
public class SelectionSort {
/**
* Swaps the elements at indexes i and j.
*/
public static void swapElements(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
/**
* Finds the index of the lowest value
* between indices low and high (inclusive).
*/
public static int indexLowest(int[] array, int start) {
int lowIndex = start;
for (int i = start; i < array.length; i++) {
if (array[i] < array[lowIndex]) {
lowIndex = i;
}
}
return lowIndex;
}
/**
* Sorts the cards (in place) using selection sort.
*/
public static void selectionSort(int[] array) {
for (int i = 0; i < array.length; i++) {
int j = indexLowest(array, i);
swapElements(array, i, j);
}
}
/**
* @param args
*/
public static void main(String[] args) {
int[] array = {2, 5, 6, 1, 3};
selectionSort(array);
System.out.println(Arrays.toString(array));
}
}
indexLowest 메서드는 선형, swapElements 메서드는 이차
총 연산 횟수는 n²에 비례
빅오 표기법 : 알고리즘을 분류하는 방식 ( 모든 선형 알고리즘 o(n), 모든 이차 알고리즘 o(n²))
@Override
public T set(int index, T element) {
//인덱스를 검사하는 코드의 반복을 피해야 한다.
return null;
}
@Override
public int indexOf(Object target) {
// TODO: FILL THIS IN!
return -1;
}
@Override
public boolean add(T element) {
return false;
}
@Override
public T remove(int index) {
// TODO: FILL THIS IN!
return null;
}
REFERENCE
GitHub - yudong80/ThinkDataStructures: LaTeX source and supporting code for Think Data Structures: Algorithms and Information Re
LaTeX source and supporting code for Think Data Structures: Algorithms and Information Retrieval in Java - GitHub - yudong80/ThinkDataStructures: LaTeX source and supporting code for Think Data Str...
github.com
'JAVA > 자료구조와 알고리즘' 카테고리의 다른 글
이중 연결 리스트 (0) | 2022.06.23 |
---|---|
LinkedList 클래스 (0) | 2022.06.22 |
ArrayList 클래스 (0) | 2022.06.20 |
인터페이스 (0) | 2022.06.16 |
기술 면접에 필요한 실용주의 자료구조와 알고리즘 (0) | 2022.05.27 |