정리하긴 귀찮고 너무 정리를 잘해주신 분의 링크를 걸어둔다.
완전 한번에 이해된다 ~
선택정렬
public class Selection_Sort {
public static void selection_sort(int[] a) {
selection_sort(a, a.length);
}
private static void selection_sort(int[] a, int size) {
for(int i = 0; i < size - 1; i++) {
int min_index = i;
// 최솟값을 갖고있는 인덱스 찾기
for(int j = i + 1; j < size; j++) {
if(a[j] < a[min_index]) {
min_index = j;
}
}
// i번째 값과 찾은 최솟값을 서로 교환
swap(a, min_index, i);
}
}
private static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
https://st-lab.tistory.com/168
자바 [JAVA] - 선택 정렬 (Selection Sort)
[정렬 알고리즘 모음] 더보기 1. 계수 정렬 (Counting Sort) 2. 선택 정렬 (Selection Sort) - [현재 페이지] 3. 삽입 정렬 (Insertion Sort) 4. 거품 정렬 (Bubble Sort) 5. 셸 정렬 (Shell Sort) 6. 힙 정렬 (He..
st-lab.tistory.com
버블정렬
public class Bubble_Sort {
public static void bubble_sort(int[] a) {
bubble_sort(a, a.length);
}
private static void bubble_sort(int[] a, int size) {
// round는 배열 크기 - 1 만큼 진행됨
for(int i = 1; i < size; i++) {
// 각 라운드별 비교횟수는 배열 크기의 현재 라운드를 뺀 만큼 비교함
for(int j = 0; j < size - i; j++) {
//현재 원소가 다음 원소보다 클 경우, 서로 원소의 위치를 교환한다.
if(a[j] > a [j + 1]) {
swap(a, j, j + 1);
}
}
}
}
private static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
https://st-lab.tistory.com/195
자바 [JAVA] - 거품 정렬 (Bubble Sort)
[정렬 알고리즘 모음] 더보기 1. 계수 정렬 (Counting Sort) 2. 선택 정렬 (Selection Sort) 3. 삽입 정렬 (Insertion Sort) 4. 거품 정렬 (Bubble Sort) - [현재 페이지] 5. 셸 정렬 (Shell Sort) 6. 힙 정렬 (He..
st-lab.tistory.com
삽입정렬
public class Insertion_Sort {
public static void insertion_sort(int[] a) {
insertion_sort(a, a.length);
}
private static void insertion_sort(int[] a, int size) {
for(int i = 1; i < size; i++) {
// 타겟 넘버
int target = a[i];
int j = i - 1;
// 타겟이 이전 원소보다 크기 전 까지 반복
while(j >= 0 && target < a[j]) {
a[j + 1] = a[j]; // 이전 원소를 한 칸씩 뒤로 미룬다.
j--;
}
/*
* 위 반복문에서 탈출 하는 경우 앞의 원소가 타겟보다 작다는 의미이므로
* 타겟 원소는 j번째 원소 뒤에 와야한다.
* 그러므로 타겟은 j + 1 에 위치하게 된다.
*/
a[j + 1] = target;
}
}
}
https://st-lab.tistory.com/179
자바 [JAVA] - 삽입 정렬 (Insertion Sort)
[정렬 알고리즘 모음] 더보기 1. 계수 정렬 (Counting Sort) 2. 선택 정렬 (Selection Sort) 3. 삽입 정렬 (Insertion Sort) - [현재 페이지] 4. 거품 정렬 (Bubble Sort) 5. 셸 정렬 (Shell Sort) 6. 힙 정렬 (He..
st-lab.tistory.com
저 분 티스토리 들어가면 영상들도 첨부되어있는데 웃기다.
'JAVA' 카테고리의 다른 글
[디자인패턴] Factory Method (0) | 2022.03.21 |
---|---|
[sort] 좌표 정렬 (0) | 2022.02.20 |
[디자인패턴] MVC, MVP, MVVM 에 대해 알아보자 (1) | 2022.01.11 |
[eclipse] [Git] 이클립스와 깃 연동 (0) | 2021.07.29 |
[eclipse] 'Eclipse 응용 프로그램이 예기치 않게 종료되었습니다.' 오류 해결 (0) | 2021.06.18 |