일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 깊이우선탐색
- android fragment
- 자료구조
- 안드로이드 ANR
- support fragment
- 안드로이드
- anr
- 소수 알고리즘
- 자바 컬렉션
- 안드로이드 AdapterView
- Github
- 알고리즘
- BFS
- 백준
- db
- 소수
- 액티비티 ANR
- java
- 백준 알고리즘
- application not responding
- android adapterview
- oracle
- android support
- 안드로이드 파일
- 컬렉션
- DFS
- 안드로이드 DBMS
- support 라이브러리
- SQLite와 Realm 차이점
- 너비우선탐색
Archives
- Today
- Total
밍의 기록들😉
[정렬] 선택 정렬, 삽입 정렬, 버블 정렬의 구현 본문
선택 정렬 구현 코드
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 | import java.util.Scanner; public class SortSelection { // 선택 정렬 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int [] data = new int [ 100 ]; for ( int i= 0 ; i<n; i++){ data[i] = sc.nextInt(); } // i : 앞의 정렬이 되어 있는 값들의 다음 위치 // index : (비교대상 1)으로 최솟값이 담김(i부터 n까지의) // j : (비교대상 2) 순차적으로 뒤로 가면서 index와 비교함 for ( int i= 0 ; i<n; i++){ int index = i; for ( int j=i+ 1 ; j<n; j++){ if (data[index] > data[j]){ // index값이 j값보다 크다면 index = j; // index 값이 최솟값이 됨 } } int temp; temp = data[i]; data[i] = data[index]; data[index] = temp; } for ( int i= 0 ; i<n; i++){ System.out.print(data[i]+ " " ); } } } |
삽입 정렬 구현 코드
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 | import java.util.Scanner; public class SortInsertion { //삽입 정렬 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int [] data = new int [ 100 ]; for ( int i= 0 ; i<n; i++){ data[i] = sc.nextInt(); } // i가 가리키고 있는 값이 왼쪽 정렬되어 있는 값과 비교 후 자리에 들어감 // i : 앞의 정렬되어 있는 값들의 다음 위치 (비교대상 1) // j : (비교 대상 2) 순차적으로 앞으로 가면서 i와 비교함 for ( int i= 1 ; i<n; i++){ for ( int j=i; j>= 1 ; j--){ if (data[j- 1 ] > data[j]){ int temp; temp = data[j- 1 ]; data[j- 1 ] = data[j]; data[j] = temp; } else break ; } } for ( int i= 0 ; i<n; i++){ System.out.print(data[i]+ " " ); } } } |
버블 정렬 구현 코드
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 | import java.util.Scanner; public class SortBubble { // 버블 정렬 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int [] data = new int [ 100 ]; for ( int i= 0 ; i<n; i++){ data[i] = sc.nextInt(); } // j가 가리키고 있는 값과 오른쪽 값 비교를 n번함 for ( int i= 0 ; i<n; i++){ for ( int j= 0 ; j<n-i- 1 ; j++){ if (data[j] > data[j+ 1 ]){ int temp; temp = data[j]; data[j] = data[j+ 1 ]; data[j+ 1 ] = temp; } } } for ( int i= 0 ; i<n; i++){ System.out.print(data[i]+ " " ); } } } |
'자료구조, 알고리즘 > 기본다지기' 카테고리의 다른 글
[정렬] 퀵 정렬 (Quick Sort) - 개념, 시간복잡도, 구현 (0) | 2018.09.10 |
---|---|
[정렬] 합병 정렬 (Merge Sort) - 개념, 시간복잡도, 구현 (0) | 2018.09.10 |
[트리] 트리의 순회 (0) | 2018.09.09 |
[트리] 트리의 표현 (0) | 2018.09.09 |
[트리] 트리의 기본 (0) | 2018.09.09 |