일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 항해99
- 제네릭 와일드 카드
- Study Halle
- Effective JAVA
- 상속
- Switch Expressions
- public 필드
- 정렬
- System.out
- Java
- 바운디드 타입
- raw 타입
- System.err
- 로컬 클래스
- 프리미티브 타입
- 함수형 인터페이스
- 스파르타코딩클럽
- System.in
- junit 5
- 자바스터디
- 자바할래
- 익명 클래스
- annotation processor
- github api
- 브릿지 메소드
- 접근지시자
- 람다식
- 제네릭 타입
- auto.create.topics.enable
- 합병 정렬
Archives
- Today
- Total
코딩하는 털보
선택 정렬 본문
선택 정렬은 배열의 부분 배열에서 최소값(또는 최대값)을 찾아 부분 배열의 시작 인덱스와 값을 교환하는 정렬 방법이다.
선택 정렬 알고리즘의 구체적인 과정은 다음과 같다.
- 배열의 첫 번째 원소를 선택
- 이후 원소들 중에서 최소값 검색
- 최소값을 찾았다면, 해당 최소값을 현재 선택한 원소와 교환
- 배열의 다음 원소를 선택하여 위 과정을 반복
- 모든 원소에 대해 위 과정을 반복하여 정렬을 완료
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SelectionSort { | |
public int[] sort(int[] arr) { | |
for (int i = 0; i < arr.length-1; i++) { | |
int min = arr[i]; | |
int index = i; | |
for (int j = i+1; j < arr.length; j++) { | |
if ( min > arr[j] ) { | |
min = arr[j]; | |
index = j; | |
} | |
} | |
int temp = arr[i]; | |
arr[i] = arr[index]; | |
arr[index] = temp; | |
} | |
return arr; | |
} | |
public static void main(String[] args) { | |
SelectionSort selectionSort = new SelectionSort(); | |
System.out.println(Arrays.toString(selectionSort.sort(new int[]{7, 3, 8, 3, 6, 9, 4, 65, 89}))); | |
} | |
} |
코드만 봐도 알수 있듯이 이 정렬법의 시간 복잡도는 O(n^2)이다.
배열의 크기가 클수록 성능이 좋지 못하므로 일반적으로는 이보다 더 효율적인 정렬 알고리즘을 사용하게 된다.
Comments