SlideShare a Scribd company logo
Algorithm
Shell Sort
Bill Kim(김정훈) | ibillkim@gmail.com
목차
•Shell Sort
•Concept
•Features
•Implementation
•References
Shell Sort
Shell Sort(쉘 정렬)는 ‘Donald L. Shell’이라는 사람이 제안한 정
렬 알고리즘으로서 삽입 정렬을 보완한 알고리즘입니다.
삽입 정렬이 어느 정도 정렬된 배열에 대해서는 대단히 빠른 것에
착안하여 고안된 알고리즘으로서 기본 삽입 정렬보다 빠른 속도로
동작합니다.
쉘 정렬의 큰 핵심은 데이터를 일정한 수의 부분 집합으로 나누고
해당 부분 집합을 삽입 정렬을 하면서 최종 부분 집합이 0이 될때까
지 반복하는 방식입니다.
Concept
기본적인 알고리즘의 컨셉을 살펴보면 아래와 같습니다.
1. 리스트를 일정한 기준에 따라서 부분 리스트를 생성(처음에는 N
/ 2 개의 부분 리스트를 생성함)
2. 각 부분 리스트를 삽입 정렬을 이용하여 정렬
3. 각 부분 리스트가 정렬이 되고 나면 다시 전체 리스트를 더 적은
개수로 부분 리스트로 만듬(이전의 생성한 부분 리스트의 수보다 2
배 적은 수로 생성)
4. 2번의 과정을 다시 반복함
5. 다시 부분 리스트를 3번의 규칙대로 생성
6. 부분 리스트 갯수가 0보다 클 때까지 계속 반복하며 정렬을 함
만약 아래와 같은 수가 있다고 가정합니다.
처음은 원소 갯수(N) / 2개 만큼 부분 리스트를 만듭니다.
아래와 같이 4개의 부분 리스트 쌍을 정합니다.
{69, 16}, {10, 8}, {30, 31}, {2, 22}
Concept
{69, 16}, {10, 8}, {30, 31}, {2, 22}
해당 부분 리스트의 원소끼리 삽입 정렬을 통하여 정렬하면 아래와
같이 변경됩니다.
{16, 69}, {8, 10}, {30, 31}, {2, 22}
이번에는 부분 집합을 처음보다 2배 작은 2개로 다시 만들어봅니다.
Concept
{16, 30, 69, 31}, {8, 2, 10, 22}
이렇게 생성된 부분 집합을 다시 삽입 정렬로 정렬을 하면 아래와
같이 변경됩니다.
{16, 30, 31, 69}, {2, 8, 10, 22}
이제 다시 마지막으로 부분 집합을 1로 해서 삽입 정렬을 시도하면
아래와 같이 최종 정렬된 배열을 얻을 수 있습니다.
Concept
Features
Shell Sort(쉘 정렬)는 아래와 같은 특징을 가진 알고리즘입니다.
1. 쉘 정렬은 삽입 정렬을 활용한 정렬 알고리즘으로서 어느정도 정
렬된 상태에서의 삽입 정렬 속도가 빠른 것을 이용한 알고리즘
2. 정렬 속도는 최악의 경우는 O(n^2)이지만 최고의 경우는
O(nlogn)을 가진다.
3. Gap의 규칙에 따라서 속도가 다를 수 있다.
4. 비교적 속도 대비 알고리즘이 간단하여 활용도가 좋다.
Implementation
Swift를 활용하여 쉘 정렬 알고리즘을 살펴보겠습니다.
public func shellSort<T : Comparable>(_ array: inout [T]) -> [T] {
var sublistCount = array.count / 2
while sublistCount > 0 {
for pos in 0..<sublistCount {
insertionSort(&array, start: pos, gap: sublistCount)
}
sublistCount = sublistCount / 2
}
return array
}
Implementation
private func insertionSort<T : Comparable>(_ array: inout [T], start: Int, gap:
Int) {
// start 부터 to 위치까지 gap 만큼의 차이로 루프를 돈다.
for i in stride(from: (start + gap), to: array.count, by: gap) {
let currentValue = array[i]
var pos = i
while pos >= gap && array[pos - gap] > currentValue {
array[pos] = array[pos - gap]
pos -= gap
}
array[pos] = currentValue
}
}
Implementation
var array = [ 5, 13, 2, 25, 7, 17, 20, 8, 4 ]
print(shellSort(&array))
// before : [5, 13, 2, 25, 7, 17, 20, 8, 4]
// after : [4, 13, 2, 25, 5, 17, 20, 8, 7]
// before : [4, 13, 2, 25, 5, 17, 20, 8, 7]
// after : [4, 13, 2, 25, 5, 17, 20, 8, 7]
// before : [4, 13, 2, 25, 5, 17, 20, 8, 7]
// after : [4, 13, 2, 25, 5, 17, 20, 8, 7]
// before : [4, 13, 2, 25, 5, 17, 20, 8, 7]
// after : [4, 13, 2, 8, 5, 17, 20, 25, 7]
// before : [4, 13, 2, 8, 5, 17, 20, 25, 7]
// after : [2, 13, 4, 8, 5, 17, 7, 25, 20]
// before : [2, 13, 4, 8, 5, 17, 7, 25, 20]
// after : [2, 8, 4, 13, 5, 17, 7, 25, 20]
// before : [2, 8, 4, 13, 5, 17, 7, 25, 20]
// after : [2, 4, 5, 7, 8, 13, 17, 20, 25]
// [2, 4, 5, 7, 8, 13, 17, 20, 25]
References
[1] [Sort] 셸 정렬(Shell Sort)
: https://palpit.tistory.com/127
[2] Shell Sort - 쉘 정렬 : https://dejavuqa.tistory.com/
369
[3] Shell Sort : https://victorqi.gitbooks.io/swift-
algorithm/shell_sort.html
[4] Shell, merge, heap sort : https://
www.slideshare.net/springofmylife/shell-merge-heap-
sort-33141131
[5] Shell Sort (쉘 정렬) : https://blastic.tistory.com/127
References
[6] Shell Sort : https://iq.opengenus.org/shell-sort/
[7] Shell Sort Algorithm : https://www.programiz.com/
dsa/shell-sort
[8] Implementing Common Sorting Algorithms in Swift :
https://agostini.tech/2017/01/10/implementing-common-
sorting-algorithms-in-swift/
[9] [알고리즘] 셸 정렬(shell sort)이란 : https://
gmlwjd9405.github.io/2018/05/08/algorithm-shell-
sort.html
[10] <셸 정렬(shell sort)> 기본 개념 및 알고리즘 : https://
mattlee.tistory.com/76
Thank you!

More Related Content

What's hot

[OpenTRS-001] Hotel California
[OpenTRS-001] Hotel California[OpenTRS-001] Hotel California
[OpenTRS-001] Hotel California
Theori
 
[2013 CodeEngn Conference 09] wh1ant - various tricks for linux remote exploits
[2013 CodeEngn Conference 09] wh1ant - various tricks for linux remote exploits[2013 CodeEngn Conference 09] wh1ant - various tricks for linux remote exploits
[2013 CodeEngn Conference 09] wh1ant - various tricks for linux remote exploits
GangSeok Lee
 
7가지 동시성 모델 - 3장. 함수형 프로그래밍
7가지 동시성 모델 - 3장. 함수형 프로그래밍7가지 동시성 모델 - 3장. 함수형 프로그래밍
7가지 동시성 모델 - 3장. 함수형 프로그래밍
Hyunsoo Jung
 
[ES6] 9. Iterator
[ES6] 9. Iterator[ES6] 9. Iterator
[ES6] 9. Iterator
Han JaeYeab
 
Jupyter notebok tensorboard 실행하기_20160706
Jupyter notebok tensorboard 실행하기_20160706Jupyter notebok tensorboard 실행하기_20160706
Jupyter notebok tensorboard 실행하기_20160706
Yong Joon Moon
 
[2011 CodeEngn Conference 05] ashine - 안드로이드 리눅스에서의 시스템 해킹
[2011 CodeEngn Conference 05] ashine - 안드로이드 리눅스에서의 시스템 해킹[2011 CodeEngn Conference 05] ashine - 안드로이드 리눅스에서의 시스템 해킹
[2011 CodeEngn Conference 05] ashine - 안드로이드 리눅스에서의 시스템 해킹
GangSeok Lee
 
11장 윈도우 스레드 풀
11장 윈도우 스레드 풀11장 윈도우 스레드 풀
11장 윈도우 스레드 풀홍준 김
 
(쿼리 변환, Query Transformation,서브쿼리푸시,SubQuery Pushing)SQL튜닝을 위해 서브쿼리의 드라이빙을 제어...
(쿼리 변환, Query Transformation,서브쿼리푸시,SubQuery Pushing)SQL튜닝을 위해 서브쿼리의 드라이빙을 제어...(쿼리 변환, Query Transformation,서브쿼리푸시,SubQuery Pushing)SQL튜닝을 위해 서브쿼리의 드라이빙을 제어...
(쿼리 변환, Query Transformation,서브쿼리푸시,SubQuery Pushing)SQL튜닝을 위해 서브쿼리의 드라이빙을 제어...
탑크리에듀(구로디지털단지역3번출구 2분거리)
 
함수적 사고 2장
함수적 사고 2장함수적 사고 2장
함수적 사고 2장
HyeonSeok Choi
 
고급시스템프로그래밍
고급시스템프로그래밍고급시스템프로그래밍
고급시스템프로그래밍
kimkiweon
 
(SQL초보자를 위한, 쿼리최적화 for SQL튜닝)SQL쿼리작성Tip,최적화팁,최적화된SQL작성방법교육
(SQL초보자를 위한, 쿼리최적화 for SQL튜닝)SQL쿼리작성Tip,최적화팁,최적화된SQL작성방법교육(SQL초보자를 위한, 쿼리최적화 for SQL튜닝)SQL쿼리작성Tip,최적화팁,최적화된SQL작성방법교육
(SQL초보자를 위한, 쿼리최적화 for SQL튜닝)SQL쿼리작성Tip,최적화팁,최적화된SQL작성방법교육
탑크리에듀(구로디지털단지역3번출구 2분거리)
 
Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저
Circulus
 
Realm.io for iOS
Realm.io for iOSRealm.io for iOS
Realm.io for iOS
Eunjoo Im
 
(오라클힌트/SQL튜닝 강좌)쿼리튜닝을 위한 오라클의 10053 이벤트
(오라클힌트/SQL튜닝 강좌)쿼리튜닝을 위한 오라클의 10053 이벤트(오라클힌트/SQL튜닝 강좌)쿼리튜닝을 위한 오라클의 10053 이벤트
(오라클힌트/SQL튜닝 강좌)쿼리튜닝을 위한 오라클의 10053 이벤트
탑크리에듀(구로디지털단지역3번출구 2분거리)
 
사내스터디 발표 온라인게임서버이해 20100401
사내스터디 발표 온라인게임서버이해 20100401사내스터디 발표 온라인게임서버이해 20100401
사내스터디 발표 온라인게임서버이해 20100401guest91f89d83
 
[OpenTRS-001] Vitor
[OpenTRS-001] Vitor[OpenTRS-001] Vitor
[OpenTRS-001] Vitor
Theori
 
Startup JavaScript 3 - 조건문, 반복문, 예외처리
Startup JavaScript 3 - 조건문, 반복문, 예외처리Startup JavaScript 3 - 조건문, 반복문, 예외처리
Startup JavaScript 3 - 조건문, 반복문, 예외처리
Circulus
 
Startup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JSStartup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JS
Circulus
 

What's hot (19)

[OpenTRS-001] Hotel California
[OpenTRS-001] Hotel California[OpenTRS-001] Hotel California
[OpenTRS-001] Hotel California
 
[2013 CodeEngn Conference 09] wh1ant - various tricks for linux remote exploits
[2013 CodeEngn Conference 09] wh1ant - various tricks for linux remote exploits[2013 CodeEngn Conference 09] wh1ant - various tricks for linux remote exploits
[2013 CodeEngn Conference 09] wh1ant - various tricks for linux remote exploits
 
7가지 동시성 모델 - 3장. 함수형 프로그래밍
7가지 동시성 모델 - 3장. 함수형 프로그래밍7가지 동시성 모델 - 3장. 함수형 프로그래밍
7가지 동시성 모델 - 3장. 함수형 프로그래밍
 
[ES6] 9. Iterator
[ES6] 9. Iterator[ES6] 9. Iterator
[ES6] 9. Iterator
 
Jupyter notebok tensorboard 실행하기_20160706
Jupyter notebok tensorboard 실행하기_20160706Jupyter notebok tensorboard 실행하기_20160706
Jupyter notebok tensorboard 실행하기_20160706
 
[2011 CodeEngn Conference 05] ashine - 안드로이드 리눅스에서의 시스템 해킹
[2011 CodeEngn Conference 05] ashine - 안드로이드 리눅스에서의 시스템 해킹[2011 CodeEngn Conference 05] ashine - 안드로이드 리눅스에서의 시스템 해킹
[2011 CodeEngn Conference 05] ashine - 안드로이드 리눅스에서의 시스템 해킹
 
11장 윈도우 스레드 풀
11장 윈도우 스레드 풀11장 윈도우 스레드 풀
11장 윈도우 스레드 풀
 
(쿼리 변환, Query Transformation,서브쿼리푸시,SubQuery Pushing)SQL튜닝을 위해 서브쿼리의 드라이빙을 제어...
(쿼리 변환, Query Transformation,서브쿼리푸시,SubQuery Pushing)SQL튜닝을 위해 서브쿼리의 드라이빙을 제어...(쿼리 변환, Query Transformation,서브쿼리푸시,SubQuery Pushing)SQL튜닝을 위해 서브쿼리의 드라이빙을 제어...
(쿼리 변환, Query Transformation,서브쿼리푸시,SubQuery Pushing)SQL튜닝을 위해 서브쿼리의 드라이빙을 제어...
 
함수적 사고 2장
함수적 사고 2장함수적 사고 2장
함수적 사고 2장
 
고급시스템프로그래밍
고급시스템프로그래밍고급시스템프로그래밍
고급시스템프로그래밍
 
(SQL초보자를 위한, 쿼리최적화 for SQL튜닝)SQL쿼리작성Tip,최적화팁,최적화된SQL작성방법교육
(SQL초보자를 위한, 쿼리최적화 for SQL튜닝)SQL쿼리작성Tip,최적화팁,최적화된SQL작성방법교육(SQL초보자를 위한, 쿼리최적화 for SQL튜닝)SQL쿼리작성Tip,최적화팁,최적화된SQL작성방법교육
(SQL초보자를 위한, 쿼리최적화 for SQL튜닝)SQL쿼리작성Tip,최적화팁,최적화된SQL작성방법교육
 
Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저
 
Realm.io for iOS
Realm.io for iOSRealm.io for iOS
Realm.io for iOS
 
(오라클힌트/SQL튜닝 강좌)쿼리튜닝을 위한 오라클의 10053 이벤트
(오라클힌트/SQL튜닝 강좌)쿼리튜닝을 위한 오라클의 10053 이벤트(오라클힌트/SQL튜닝 강좌)쿼리튜닝을 위한 오라클의 10053 이벤트
(오라클힌트/SQL튜닝 강좌)쿼리튜닝을 위한 오라클의 10053 이벤트
 
사내스터디 발표 온라인게임서버이해 20100401
사내스터디 발표 온라인게임서버이해 20100401사내스터디 발표 온라인게임서버이해 20100401
사내스터디 발표 온라인게임서버이해 20100401
 
[OpenTRS-001] Vitor
[OpenTRS-001] Vitor[OpenTRS-001] Vitor
[OpenTRS-001] Vitor
 
D2 Job Pool
D2 Job PoolD2 Job Pool
D2 Job Pool
 
Startup JavaScript 3 - 조건문, 반복문, 예외처리
Startup JavaScript 3 - 조건문, 반복문, 예외처리Startup JavaScript 3 - 조건문, 반복문, 예외처리
Startup JavaScript 3 - 조건문, 반복문, 예외처리
 
Startup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JSStartup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JS
 

Similar to [Algorithm] Shell Sort

[Algorithm] Heap Sort
[Algorithm] Heap Sort[Algorithm] Heap Sort
[Algorithm] Heap Sort
Bill Kim
 
[Algorithm] Quick Sort
[Algorithm] Quick Sort[Algorithm] Quick Sort
[Algorithm] Quick Sort
Bill Kim
 
02. data structure and stl
02. data structure and stl02. data structure and stl
02. data structure and stl
승혁 조
 
제 10회 엑셈 수요 세미나 자료 연구컨텐츠팀
제 10회 엑셈 수요 세미나 자료 연구컨텐츠팀제 10회 엑셈 수요 세미나 자료 연구컨텐츠팀
제 10회 엑셈 수요 세미나 자료 연구컨텐츠팀
EXEM
 
Project#2말의여행 Hwp
Project#2말의여행 HwpProject#2말의여행 Hwp
Project#2말의여행 HwpKimjeongmoo
 
Clean code
Clean codeClean code
Clean codebbongcsu
 
From MSSQL to MySQL
From MSSQL to MySQLFrom MSSQL to MySQL
From MSSQL to MySQL
I Goo Lee
 
Programming java day2
Programming java day2Programming java day2
Programming java day2Jaehoonyam
 
[Algorithm] Selection Sort
[Algorithm] Selection Sort[Algorithm] Selection Sort
[Algorithm] Selection Sort
Bill Kim
 
자료구조2보고서
자료구조2보고서자료구조2보고서
자료구조2보고서KimChangHoen
 
PostgreSQL Deep Internal
PostgreSQL Deep InternalPostgreSQL Deep Internal
PostgreSQL Deep Internal
EXEM
 
[Swift] Data Structure - Queue
[Swift] Data Structure - Queue[Swift] Data Structure - Queue
[Swift] Data Structure - Queue
Bill Kim
 
Windows reversing study_basic_9
Windows reversing study_basic_9Windows reversing study_basic_9
Windows reversing study_basic_9
Jinkyoung Kim
 
Amazon aurora 2
Amazon aurora 2Amazon aurora 2
Amazon aurora 2
EXEM
 
이산치수학 Project7
이산치수학 Project7이산치수학 Project7
이산치수학 Project7KoChungWook
 
How to build a web server on Linux.
How to build a web server on Linux.How to build a web server on Linux.
How to build a web server on Linux.
은석 김은석
 
Assembly 스터디 1
Assembly 스터디 1Assembly 스터디 1
Assembly 스터디 1
Jinkyoung Kim
 
제 9회 엑셈 수요 세미나 자료 연구컨텐츠팀
제 9회 엑셈 수요 세미나 자료 연구컨텐츠팀제 9회 엑셈 수요 세미나 자료 연구컨텐츠팀
제 9회 엑셈 수요 세미나 자료 연구컨텐츠팀
EXEM
 
Red Hat OpenStack 17 저자직강+스터디그룹_5주차
Red Hat OpenStack 17 저자직강+스터디그룹_5주차Red Hat OpenStack 17 저자직강+스터디그룹_5주차
Red Hat OpenStack 17 저자직강+스터디그룹_5주차
Nalee Jang
 
Terasort
TerasortTerasort
Terasort
hhyin
 

Similar to [Algorithm] Shell Sort (20)

[Algorithm] Heap Sort
[Algorithm] Heap Sort[Algorithm] Heap Sort
[Algorithm] Heap Sort
 
[Algorithm] Quick Sort
[Algorithm] Quick Sort[Algorithm] Quick Sort
[Algorithm] Quick Sort
 
02. data structure and stl
02. data structure and stl02. data structure and stl
02. data structure and stl
 
제 10회 엑셈 수요 세미나 자료 연구컨텐츠팀
제 10회 엑셈 수요 세미나 자료 연구컨텐츠팀제 10회 엑셈 수요 세미나 자료 연구컨텐츠팀
제 10회 엑셈 수요 세미나 자료 연구컨텐츠팀
 
Project#2말의여행 Hwp
Project#2말의여행 HwpProject#2말의여행 Hwp
Project#2말의여행 Hwp
 
Clean code
Clean codeClean code
Clean code
 
From MSSQL to MySQL
From MSSQL to MySQLFrom MSSQL to MySQL
From MSSQL to MySQL
 
Programming java day2
Programming java day2Programming java day2
Programming java day2
 
[Algorithm] Selection Sort
[Algorithm] Selection Sort[Algorithm] Selection Sort
[Algorithm] Selection Sort
 
자료구조2보고서
자료구조2보고서자료구조2보고서
자료구조2보고서
 
PostgreSQL Deep Internal
PostgreSQL Deep InternalPostgreSQL Deep Internal
PostgreSQL Deep Internal
 
[Swift] Data Structure - Queue
[Swift] Data Structure - Queue[Swift] Data Structure - Queue
[Swift] Data Structure - Queue
 
Windows reversing study_basic_9
Windows reversing study_basic_9Windows reversing study_basic_9
Windows reversing study_basic_9
 
Amazon aurora 2
Amazon aurora 2Amazon aurora 2
Amazon aurora 2
 
이산치수학 Project7
이산치수학 Project7이산치수학 Project7
이산치수학 Project7
 
How to build a web server on Linux.
How to build a web server on Linux.How to build a web server on Linux.
How to build a web server on Linux.
 
Assembly 스터디 1
Assembly 스터디 1Assembly 스터디 1
Assembly 스터디 1
 
제 9회 엑셈 수요 세미나 자료 연구컨텐츠팀
제 9회 엑셈 수요 세미나 자료 연구컨텐츠팀제 9회 엑셈 수요 세미나 자료 연구컨텐츠팀
제 9회 엑셈 수요 세미나 자료 연구컨텐츠팀
 
Red Hat OpenStack 17 저자직강+스터디그룹_5주차
Red Hat OpenStack 17 저자직강+스터디그룹_5주차Red Hat OpenStack 17 저자직강+스터디그룹_5주차
Red Hat OpenStack 17 저자직강+스터디그룹_5주차
 
Terasort
TerasortTerasort
Terasort
 

More from Bill Kim

[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison
Bill Kim
 
[Algorithm] Big O Notation
[Algorithm] Big O Notation[Algorithm] Big O Notation
[Algorithm] Big O Notation
Bill Kim
 
[Algorithm] Radix Sort
[Algorithm] Radix Sort[Algorithm] Radix Sort
[Algorithm] Radix Sort
Bill Kim
 
[Algorithm] Counting Sort
[Algorithm] Counting Sort[Algorithm] Counting Sort
[Algorithm] Counting Sort
Bill Kim
 
[Algorithm] Merge Sort
[Algorithm] Merge Sort[Algorithm] Merge Sort
[Algorithm] Merge Sort
Bill Kim
 
[Algorithm] Insertion Sort
[Algorithm] Insertion Sort[Algorithm] Insertion Sort
[Algorithm] Insertion Sort
Bill Kim
 
[Algorithm] Bubble Sort
[Algorithm] Bubble Sort[Algorithm] Bubble Sort
[Algorithm] Bubble Sort
Bill Kim
 
[Algorithm] Binary Search
[Algorithm] Binary Search[Algorithm] Binary Search
[Algorithm] Binary Search
Bill Kim
 
[Algorithm] Recursive(재귀)
[Algorithm] Recursive(재귀)[Algorithm] Recursive(재귀)
[Algorithm] Recursive(재귀)
Bill Kim
 
[Swift] Data Structure - AVL
[Swift] Data Structure - AVL[Swift] Data Structure - AVL
[Swift] Data Structure - AVL
Bill Kim
 
[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree
Bill Kim
 
[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)
Bill Kim
 
[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)
Bill Kim
 
[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree
Bill Kim
 
[Swift] Data Structure - Tree
[Swift] Data Structure - Tree[Swift] Data Structure - Tree
[Swift] Data Structure - Tree
Bill Kim
 
[Swift] Data Structure - Graph
[Swift] Data Structure - Graph[Swift] Data Structure - Graph
[Swift] Data Structure - Graph
Bill Kim
 
[Swift] Data Structure - Heap
[Swift] Data Structure - Heap[Swift] Data Structure - Heap
[Swift] Data Structure - Heap
Bill Kim
 
[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue
Bill Kim
 
[Swift] Data Structure - Stack
[Swift] Data Structure - Stack[Swift] Data Structure - Stack
[Swift] Data Structure - Stack
Bill Kim
 
[Swift] Data Structure - Linked List
[Swift] Data Structure - Linked List[Swift] Data Structure - Linked List
[Swift] Data Structure - Linked List
Bill Kim
 

More from Bill Kim (20)

[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison
 
[Algorithm] Big O Notation
[Algorithm] Big O Notation[Algorithm] Big O Notation
[Algorithm] Big O Notation
 
[Algorithm] Radix Sort
[Algorithm] Radix Sort[Algorithm] Radix Sort
[Algorithm] Radix Sort
 
[Algorithm] Counting Sort
[Algorithm] Counting Sort[Algorithm] Counting Sort
[Algorithm] Counting Sort
 
[Algorithm] Merge Sort
[Algorithm] Merge Sort[Algorithm] Merge Sort
[Algorithm] Merge Sort
 
[Algorithm] Insertion Sort
[Algorithm] Insertion Sort[Algorithm] Insertion Sort
[Algorithm] Insertion Sort
 
[Algorithm] Bubble Sort
[Algorithm] Bubble Sort[Algorithm] Bubble Sort
[Algorithm] Bubble Sort
 
[Algorithm] Binary Search
[Algorithm] Binary Search[Algorithm] Binary Search
[Algorithm] Binary Search
 
[Algorithm] Recursive(재귀)
[Algorithm] Recursive(재귀)[Algorithm] Recursive(재귀)
[Algorithm] Recursive(재귀)
 
[Swift] Data Structure - AVL
[Swift] Data Structure - AVL[Swift] Data Structure - AVL
[Swift] Data Structure - AVL
 
[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree
 
[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)
 
[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)
 
[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree
 
[Swift] Data Structure - Tree
[Swift] Data Structure - Tree[Swift] Data Structure - Tree
[Swift] Data Structure - Tree
 
[Swift] Data Structure - Graph
[Swift] Data Structure - Graph[Swift] Data Structure - Graph
[Swift] Data Structure - Graph
 
[Swift] Data Structure - Heap
[Swift] Data Structure - Heap[Swift] Data Structure - Heap
[Swift] Data Structure - Heap
 
[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue
 
[Swift] Data Structure - Stack
[Swift] Data Structure - Stack[Swift] Data Structure - Stack
[Swift] Data Structure - Stack
 
[Swift] Data Structure - Linked List
[Swift] Data Structure - Linked List[Swift] Data Structure - Linked List
[Swift] Data Structure - Linked List
 

[Algorithm] Shell Sort

  • 3. Shell Sort Shell Sort(쉘 정렬)는 ‘Donald L. Shell’이라는 사람이 제안한 정 렬 알고리즘으로서 삽입 정렬을 보완한 알고리즘입니다. 삽입 정렬이 어느 정도 정렬된 배열에 대해서는 대단히 빠른 것에 착안하여 고안된 알고리즘으로서 기본 삽입 정렬보다 빠른 속도로 동작합니다. 쉘 정렬의 큰 핵심은 데이터를 일정한 수의 부분 집합으로 나누고 해당 부분 집합을 삽입 정렬을 하면서 최종 부분 집합이 0이 될때까 지 반복하는 방식입니다.
  • 4. Concept 기본적인 알고리즘의 컨셉을 살펴보면 아래와 같습니다. 1. 리스트를 일정한 기준에 따라서 부분 리스트를 생성(처음에는 N / 2 개의 부분 리스트를 생성함) 2. 각 부분 리스트를 삽입 정렬을 이용하여 정렬 3. 각 부분 리스트가 정렬이 되고 나면 다시 전체 리스트를 더 적은 개수로 부분 리스트로 만듬(이전의 생성한 부분 리스트의 수보다 2 배 적은 수로 생성) 4. 2번의 과정을 다시 반복함 5. 다시 부분 리스트를 3번의 규칙대로 생성 6. 부분 리스트 갯수가 0보다 클 때까지 계속 반복하며 정렬을 함
  • 5. 만약 아래와 같은 수가 있다고 가정합니다. 처음은 원소 갯수(N) / 2개 만큼 부분 리스트를 만듭니다. 아래와 같이 4개의 부분 리스트 쌍을 정합니다. {69, 16}, {10, 8}, {30, 31}, {2, 22} Concept
  • 6. {69, 16}, {10, 8}, {30, 31}, {2, 22} 해당 부분 리스트의 원소끼리 삽입 정렬을 통하여 정렬하면 아래와 같이 변경됩니다. {16, 69}, {8, 10}, {30, 31}, {2, 22} 이번에는 부분 집합을 처음보다 2배 작은 2개로 다시 만들어봅니다. Concept
  • 7. {16, 30, 69, 31}, {8, 2, 10, 22} 이렇게 생성된 부분 집합을 다시 삽입 정렬로 정렬을 하면 아래와 같이 변경됩니다. {16, 30, 31, 69}, {2, 8, 10, 22} 이제 다시 마지막으로 부분 집합을 1로 해서 삽입 정렬을 시도하면 아래와 같이 최종 정렬된 배열을 얻을 수 있습니다. Concept
  • 8. Features Shell Sort(쉘 정렬)는 아래와 같은 특징을 가진 알고리즘입니다. 1. 쉘 정렬은 삽입 정렬을 활용한 정렬 알고리즘으로서 어느정도 정 렬된 상태에서의 삽입 정렬 속도가 빠른 것을 이용한 알고리즘 2. 정렬 속도는 최악의 경우는 O(n^2)이지만 최고의 경우는 O(nlogn)을 가진다. 3. Gap의 규칙에 따라서 속도가 다를 수 있다. 4. 비교적 속도 대비 알고리즘이 간단하여 활용도가 좋다.
  • 9. Implementation Swift를 활용하여 쉘 정렬 알고리즘을 살펴보겠습니다. public func shellSort<T : Comparable>(_ array: inout [T]) -> [T] { var sublistCount = array.count / 2 while sublistCount > 0 { for pos in 0..<sublistCount { insertionSort(&array, start: pos, gap: sublistCount) } sublistCount = sublistCount / 2 } return array }
  • 10. Implementation private func insertionSort<T : Comparable>(_ array: inout [T], start: Int, gap: Int) { // start 부터 to 위치까지 gap 만큼의 차이로 루프를 돈다. for i in stride(from: (start + gap), to: array.count, by: gap) { let currentValue = array[i] var pos = i while pos >= gap && array[pos - gap] > currentValue { array[pos] = array[pos - gap] pos -= gap } array[pos] = currentValue } }
  • 11. Implementation var array = [ 5, 13, 2, 25, 7, 17, 20, 8, 4 ] print(shellSort(&array)) // before : [5, 13, 2, 25, 7, 17, 20, 8, 4] // after : [4, 13, 2, 25, 5, 17, 20, 8, 7] // before : [4, 13, 2, 25, 5, 17, 20, 8, 7] // after : [4, 13, 2, 25, 5, 17, 20, 8, 7] // before : [4, 13, 2, 25, 5, 17, 20, 8, 7] // after : [4, 13, 2, 25, 5, 17, 20, 8, 7] // before : [4, 13, 2, 25, 5, 17, 20, 8, 7] // after : [4, 13, 2, 8, 5, 17, 20, 25, 7] // before : [4, 13, 2, 8, 5, 17, 20, 25, 7] // after : [2, 13, 4, 8, 5, 17, 7, 25, 20] // before : [2, 13, 4, 8, 5, 17, 7, 25, 20] // after : [2, 8, 4, 13, 5, 17, 7, 25, 20] // before : [2, 8, 4, 13, 5, 17, 7, 25, 20] // after : [2, 4, 5, 7, 8, 13, 17, 20, 25] // [2, 4, 5, 7, 8, 13, 17, 20, 25]
  • 12. References [1] [Sort] 셸 정렬(Shell Sort) : https://palpit.tistory.com/127 [2] Shell Sort - 쉘 정렬 : https://dejavuqa.tistory.com/ 369 [3] Shell Sort : https://victorqi.gitbooks.io/swift- algorithm/shell_sort.html [4] Shell, merge, heap sort : https:// www.slideshare.net/springofmylife/shell-merge-heap- sort-33141131 [5] Shell Sort (쉘 정렬) : https://blastic.tistory.com/127
  • 13. References [6] Shell Sort : https://iq.opengenus.org/shell-sort/ [7] Shell Sort Algorithm : https://www.programiz.com/ dsa/shell-sort [8] Implementing Common Sorting Algorithms in Swift : https://agostini.tech/2017/01/10/implementing-common- sorting-algorithms-in-swift/ [9] [알고리즘] 셸 정렬(shell sort)이란 : https:// gmlwjd9405.github.io/2018/05/08/algorithm-shell- sort.html [10] <셸 정렬(shell sort)> 기본 개념 및 알고리즘 : https:// mattlee.tistory.com/76