Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 애플인텔리전스
- ReactiveX
- mvvm
- OperationQueue
- gitlabci/cd
- rxswift
- 오토레이아웃
- apple intelligence
- CI/CD
- gitlab
- swift알고리즘
- 클린아키텍처
- swift
- Content Hugging priority
- CICD
- 백준
- AI
- RxCocoa
- RxSwift요약
- 알고리즘
- 자료구조
- ai expo
- Union-Find
- 동작과정
- LLM
- 동시성프로그래밍
- Autolayout
- Content Compression Resistance priority
- cleanarchitecture
- IOS
Archives
- Today
- Total
JosephCha의 개발일지
삽입정렬 본문
삽입 정렬 (insertion sort) 란
- 삽입 정렬은 두 번째 인덱스부터 시작
- 해당 인덱스(key 값) 앞에 있는 데이터(B)부터 비교해서 key 값이 더 작으면, B값을 뒤 인덱스로 복사
- 이를 key 값이 더 큰 데이터를 만날때까지 반복, 그리고 큰 데이터를 만난 위치 바로 뒤에 key 값을 이동
func insertionSort(unSortedArray: [Int]) -> [Int] {
var unSortedArray = unSortedArray
for index in 0..<unSortedArray.count - 1 {
for index2 in stride(from: index + 1, to: 0, by: -1) {
if unSortedArray[index2-1] > unSortedArray[index2] {
let temp = unSortedArray[index2]
unSortedArray[index2] = unSortedArray[index2-1]
unSortedArray[index2-1] = temp
} else {
break
}
}
}
return unSortedArray
}
삽입 정렬은 두번째 인덱스 부터 시작하니, 총 사이클 수는 정렬해야할 배열 수에서 1를 뺀 값이다. (첫번째 for문)
본격적으로 인덱스2(두번째 인덱스부터 비교시작) 앞에 있는 데이터들을 비교하며 정렬을 해나가는 과정이 두번째 for문 로직이다.
'알고리즘 및 자료구조' 카테고리의 다른 글
병합정렬 (0) | 2022.04.20 |
---|---|
동적 계획법(DP) (0) | 2022.02.24 |
선택정렬 (0) | 2021.12.08 |
버블정렬 (0) | 2021.12.06 |
백준 알고리즘 (팩토리얼) No. 10872 (0) | 2021.07.28 |
Comments