티스토리 뷰
동작 원리
배열의 모든 인덱스를 돌며 정해진 규칙에 따라 자리를 바꿔 정렬
시간 복잡도
O(N^2)
// n - 1 + n - 2 + ... + 2 + 1 = n * (n - 1) / 2
과정
반복문을 통한 배열 방문 -> 인접한 두 값 비교 - > 기준에 적합할 시 자리바꿈
위 과정 반복
코드구현
#include <iostream>
using namespace std;
void swap(int& n1, int& n2) {
int temp = n1;
n1 = n2;
n2 = temp;
}
void bubbleSort(int arr[], int size) {
for(int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - 1 - i; j++) {
if(arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}
'Develop > Algorithm' 카테고리의 다른 글
[Definition] 완전탐색 (0) | 2021.04.01 |
---|---|
[Tip] Problem Solving에서 C++ 사용 팁 (0) | 2021.03.31 |
[Definition] 복잡도 (0) | 2021.03.30 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- subject
- MVVM
- Swift
- ios
- APNS
- 아키텍처
- TextField
- RxSwift
- Crossing Boundaries
- 프로비저닝
- 프로비저닝 프로파일
- notification
- 클린아키텍처
- 코테
- Rx
- 프로파일
- Push
- 동적계획법
- MVC
- TabBar
- CSR
- certificate
- Apple
- provisioning profile
- rxcocoa
- remote
- dip
- 코드사이닝
- relay
- Clean Architecture
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함