일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Study Halle
- 제네릭 와일드 카드
- 합병 정렬
- System.err
- auto.create.topics.enable
- Switch Expressions
- 프리미티브 타입
- 바운디드 타입
- 스파르타코딩클럽
- 함수형 인터페이스
- public 필드
- 자바스터디
- 람다식
- 브릿지 메소드
- 접근지시자
- System.out
- 항해99
- 로컬 클래스
- 익명 클래스
- annotation processor
- System.in
- Effective JAVA
- junit 5
- github api
- 자바할래
- raw 타입
- 정렬
- 상속
- 제네릭 타입
- Java
Archives
- Today
- Total
코딩하는 털보
버블 정렬 본문
버블 정렬은 배열에서 차례대로 두 개의 붙어있는 인덱스를 비교하여 더 큰 값을 뒤로 보내는 정렬 방법이다.
가장 큰 값이 먼저 맨 뒤로 보내지고 그 다음 뒤에서부터 순차적으로 정렬이 완성된다.
n = array length
처음엔 인덱스 0부터 n-2까지 그다음 인덱스와 비교하면서 다음 인덱스가 크면 서로 교환
위 절차를 0~n-3, 0~n-4, 0~n-5, ... 0~0으로 총 n-1번 순회
i : 0~n-2
j : 0~n-2-i
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 BubbleSort { | |
public int[] sort(int[] arr) { | |
int temp; | |
for (int i = 0; i < arr.length-1; i++) { | |
for (int j = 0; j < arr.length-1-i; j++) { | |
if ( arr[j] > arr[j+1] ) { | |
temp = arr[j]; | |
arr[j] = arr[j+1]; | |
arr[j+1] = temp; | |
} | |
} | |
} | |
return arr; | |
} | |
public static void main(String[] args) { | |
BubbleSort bubbleSort = new BubbleSort(); | |
int[] sort = bubbleSort.sort(new int[]{7, 3, 78, 3, 4, 8, 10, 34}); | |
System.out.println(Arrays.toString(sort)); | |
} | |
} |
최선의 경우, 최악의 경우 모두 시간 복잡도가 O(n^2)으로 성능이 좋지 못하여 구현이 쉽다는 장점 말고는 다른 정렬 방법과 비교했을 때 좋은 정렬 방법이 아님.