SlideShare a Scribd company logo
Algorithm
Quick Sort
Bill Kim(김정훈) | ibillkim@gmail.com
목차
•Quick Sort
•Concept
•Features
•Implementation
•References
Quick Sort
Quick Sort(퀵 정렬)는 대표적인 분할, 정복 정렬 알고리즘으로서
최악의 경우는 O(n^2)이지만 평균적으로는 O(nlogn)으로서 병합
정렬보다 보편적으로 빠른 속도를 가진 알고리즘입니다.
특정 값(Pivot)을 선택한 후 좌우로 작은 수와 큰 수 리스트를 나누
어 최종 배열을 합치는 방법으로 정렬을 하는 방식입니다.
Pivot 값 선정에 따라서 일부 속도가 달라질 수 있는 그러한 알고
리즘입니다.
Concept
기본적인 알고리즘의 컨셉을 살펴보면 아래와 같습니다.
1. 기준 값(Pivot)를 하나 선정한다.
2. 해당 값을 중심으로 하여 작은 수와 큰 수로 나눈다.
( L < 기준값 < R )
3. 재귀 호출을 통하여 마지막 배열의 크기가 1개일때까지 1~2번
을 반복합니다.
4. 최종적으로 작은 수 배열과 기준 값(Pivot) 큰 수 배열을 합치면
정렬된 배열을 얻을 수 있습니다.
Concept
Features
Quick Sort(퀵 정렬)는 아래와 같은 특징을 가진 알고리즘입니다.
1. 분할 정복을 활용한 빠른 정렬 알고리즘
2. 평균적으로 삽입이나 병합 정렬보다 빠른 속도를 가짐
3. 초기 Pivot 선택에 따라서 속도가 차이가 날 수 있다.
4. 평균은 O(nlogn), 최악의 경우 O(n^2)의 시간복잡도를 가짐
5. 최악의 시간 복잡도를 가지는 경우는 정렬되어 있거나 역순일 경
우이므로 일반적인 상황에서는 거의 발생하지 않음
Implementation
Swift를 활용하여 퀵 정렬 알고리즘을 살펴보겠습니다.
func quickSort<T: Comparable>(_ array: [T]) -> [T] {
if array.count < 2 {
print("last array : (array)")
return array
}
else { print("array : (array)") }
let pivot = array.first!
print("pivot : (pivot)")
let smaller = array.filter { $0 < pivot }
let larger = array.filter { $0 > pivot }
print("smaller : (smaller)")
print("larger : (larger)")
let result = quickSort(smaller) + [pivot] + quickSort(larger)
print("smaller + pivot + larger : (result)")
return result
}
Implementation
let array = [3, 2, 5, 1, 4]
print(quickSort(array)) // [1, 2, 3, 4, 5]
// array : [3, 2, 5, 1, 4]
// pivot : 3
// smaller : [2, 1]
// larger : [5, 4]
// array : [2, 1]
// pivot : 2
// smaller : [1]
// larger : []
// last array : [1]
// last array : []
// smaller + pivot + larger : [1, 2]
// array : [5, 4]
// pivot : 5
// smaller : [4]
// larger : []
// last array : [4]
// last array : []
// smaller + pivot + larger : [4, 5]
// smaller + pivot + larger : [1, 2, 3, 4, 5]
// [1, 2, 3, 4, 5]
References
[1] Algorithm) 퀵 정렬(Quick Sort) in Swift : https://atelier-chez-
moi.tistory.com/87
[2] 정렬 알고리즘 - Quick Sort (평균- nlogn, 최악- n^2) : https://
zeddios.tistory.com/35
[3] [Swift]Quick Sort : http://minsone.github.io/programming/
quick-sort-in-swift
[4] [알고리즘] 퀵 정렬 (Quick sort) : http://blog.naver.com/
PostView.nhn?
blogId=writer0713&logNo=221138306597&parentCategoryNo=
&categoryNo=68&viewDate=&isShowPopularPosts=true&from
=search
[5] 퀵 정렬 quick sort with swift : https://hyerios.tistory.com/70
References
[6] 알고리즘 ] 3. 퀵 정렬 : https://its-blog.tistory.com/m/105?
category=801513
[7] 피벗설정에 따른 퀵정렬의 속도 : https://
ghd5262.tistory.com/25
[8] [Swift 자료구조 ch11] 퀵소트 (Quick Sort) : https://
kor45cw.tistory.com/247
[9] How do you implement Quick Sort in Swift? : https://
medium.com/@notestomyself/how-do-you-implement-
quick-sort-in-swift-d0dd0308a473
[10] Swift Quick Sort Algorithm With Recursion and
Generics : https://medium.com/@augusteo/swift-quick-
sort-algorithm-with-recursion-and-generics-78a256b3b392
Thank you!

More Related Content

What's hot

Binary Search
Binary SearchBinary Search
Binary Search
skku_npc
 
Data Structures
Data StructuresData Structures
Data Structures
skku_npc
 
[Swift] Data Structure - Array
[Swift] Data Structure - Array[Swift] Data Structure - Array
[Swift] Data Structure - Array
Bill Kim
 
[Algorithm] Binary Search
[Algorithm] Binary Search[Algorithm] Binary Search
[Algorithm] Binary Search
Bill Kim
 
6. Sorting
6. Sorting6. Sorting
6. Sorting
Melon Lemon
 
Item10. toString 메소드는 항상 오버라이드 하자
Item10. toString 메소드는 항상 오버라이드  하자Item10. toString 메소드는 항상 오버라이드  하자
Item10. toString 메소드는 항상 오버라이드 하자
Sungho Moon
 
R 기초 : R Basics
R 기초 : R BasicsR 기초 : R Basics
R 기초 : R Basics
Yoonwhan Lee
 
06. sorting
06. sorting06. sorting
06. sorting
승혁 조
 
Data Mining with R CH1 요약
Data Mining with R CH1 요약Data Mining with R CH1 요약
Data Mining with R CH1 요약
Sung Yub Kim
 
제4장 sql 함수를 사용해보기
제4장 sql 함수를 사용해보기제4장 sql 함수를 사용해보기
제4장 sql 함수를 사용해보기
sang doc Lee
 

What's hot (12)

Binary Search
Binary SearchBinary Search
Binary Search
 
Data Structures
Data StructuresData Structures
Data Structures
 
[Swift] Data Structure - Array
[Swift] Data Structure - Array[Swift] Data Structure - Array
[Swift] Data Structure - Array
 
[Algorithm] Binary Search
[Algorithm] Binary Search[Algorithm] Binary Search
[Algorithm] Binary Search
 
Apply교육
Apply교육Apply교육
Apply교육
 
6. Sorting
6. Sorting6. Sorting
6. Sorting
 
R intro
R introR intro
R intro
 
Item10. toString 메소드는 항상 오버라이드 하자
Item10. toString 메소드는 항상 오버라이드  하자Item10. toString 메소드는 항상 오버라이드  하자
Item10. toString 메소드는 항상 오버라이드 하자
 
R 기초 : R Basics
R 기초 : R BasicsR 기초 : R Basics
R 기초 : R Basics
 
06. sorting
06. sorting06. sorting
06. sorting
 
Data Mining with R CH1 요약
Data Mining with R CH1 요약Data Mining with R CH1 요약
Data Mining with R CH1 요약
 
제4장 sql 함수를 사용해보기
제4장 sql 함수를 사용해보기제4장 sql 함수를 사용해보기
제4장 sql 함수를 사용해보기
 

Similar to [Algorithm] Quick Sort

[Algorithm] Shell Sort
[Algorithm] Shell Sort[Algorithm] Shell Sort
[Algorithm] Shell Sort
Bill Kim
 
퀵 정렬 알고리즘 기초
퀵 정렬 알고리즘 기초퀵 정렬 알고리즘 기초
퀵 정렬 알고리즘 기초
Dongyi Kim
 
Coursera Machine Learning (by Andrew Ng)_강의정리
Coursera Machine Learning (by Andrew Ng)_강의정리Coursera Machine Learning (by Andrew Ng)_강의정리
Coursera Machine Learning (by Andrew Ng)_강의정리
SANG WON PARK
 
Project#5 최단거리 찾기 D0 Hwp
Project#5 최단거리 찾기 D0 HwpProject#5 최단거리 찾기 D0 Hwp
Project#5 최단거리 찾기 D0 HwpKimjeongmoo
 
[Algorithm] Merge Sort
[Algorithm] Merge Sort[Algorithm] Merge Sort
[Algorithm] Merge Sort
Bill Kim
 
[Algorithm] Heap Sort
[Algorithm] Heap Sort[Algorithm] Heap Sort
[Algorithm] Heap Sort
Bill Kim
 
R 프로그래밍-향상된 데이타 조작
R 프로그래밍-향상된 데이타 조작R 프로그래밍-향상된 데이타 조작
R 프로그래밍-향상된 데이타 조작
Terry Cho
 
밑바닥부터 시작하는 딥러닝_신경망학습
밑바닥부터 시작하는 딥러닝_신경망학습밑바닥부터 시작하는 딥러닝_신경망학습
밑바닥부터 시작하는 딥러닝_신경망학습
Juhui Park
 
자료구조5보고서
자료구조5보고서자료구조5보고서
자료구조5보고서KimChangHoen
 
Shell, merge, heap sort
Shell, merge, heap sortShell, merge, heap sort
Shell, merge, heap sort
Hyun Jin Moon
 
Project#2말의여행 Hwp
Project#2말의여행 HwpProject#2말의여행 Hwp
Project#2말의여행 HwpKimjeongmoo
 
Pyconkr2019 features for using python like matlab
Pyconkr2019 features for using python like matlabPyconkr2019 features for using python like matlab
Pyconkr2019 features for using python like matlab
Intae Cho
 
[데브루키]노대영_알고리즘 스터디
[데브루키]노대영_알고리즘 스터디[데브루키]노대영_알고리즘 스터디
[데브루키]노대영_알고리즘 스터디
대영 노
 
From MSSQL to MySQL
From MSSQL to MySQLFrom MSSQL to MySQL
From MSSQL to MySQL
I Goo Lee
 
[Algorithm] Insertion Sort
[Algorithm] Insertion Sort[Algorithm] Insertion Sort
[Algorithm] Insertion Sort
Bill Kim
 
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexity
skku_npc
 
이산치 과제7
이산치 과제7이산치 과제7
이산치 과제7mil23
 
R 시작해보기
R 시작해보기R 시작해보기
R 시작해보기
SEUNGWOO LEE
 
2012 Dm A0 07 Pdf
2012 Dm A0 07 Pdf2012 Dm A0 07 Pdf
2012 Dm A0 07 Pdfkd19h
 
2012 Dm A0 07 Pdf
2012 Dm A0 07 Pdf2012 Dm A0 07 Pdf
2012 Dm A0 07 Pdfjinwookhong
 

Similar to [Algorithm] Quick Sort (20)

[Algorithm] Shell Sort
[Algorithm] Shell Sort[Algorithm] Shell Sort
[Algorithm] Shell Sort
 
퀵 정렬 알고리즘 기초
퀵 정렬 알고리즘 기초퀵 정렬 알고리즘 기초
퀵 정렬 알고리즘 기초
 
Coursera Machine Learning (by Andrew Ng)_강의정리
Coursera Machine Learning (by Andrew Ng)_강의정리Coursera Machine Learning (by Andrew Ng)_강의정리
Coursera Machine Learning (by Andrew Ng)_강의정리
 
Project#5 최단거리 찾기 D0 Hwp
Project#5 최단거리 찾기 D0 HwpProject#5 최단거리 찾기 D0 Hwp
Project#5 최단거리 찾기 D0 Hwp
 
[Algorithm] Merge Sort
[Algorithm] Merge Sort[Algorithm] Merge Sort
[Algorithm] Merge Sort
 
[Algorithm] Heap Sort
[Algorithm] Heap Sort[Algorithm] Heap Sort
[Algorithm] Heap Sort
 
R 프로그래밍-향상된 데이타 조작
R 프로그래밍-향상된 데이타 조작R 프로그래밍-향상된 데이타 조작
R 프로그래밍-향상된 데이타 조작
 
밑바닥부터 시작하는 딥러닝_신경망학습
밑바닥부터 시작하는 딥러닝_신경망학습밑바닥부터 시작하는 딥러닝_신경망학습
밑바닥부터 시작하는 딥러닝_신경망학습
 
자료구조5보고서
자료구조5보고서자료구조5보고서
자료구조5보고서
 
Shell, merge, heap sort
Shell, merge, heap sortShell, merge, heap sort
Shell, merge, heap sort
 
Project#2말의여행 Hwp
Project#2말의여행 HwpProject#2말의여행 Hwp
Project#2말의여행 Hwp
 
Pyconkr2019 features for using python like matlab
Pyconkr2019 features for using python like matlabPyconkr2019 features for using python like matlab
Pyconkr2019 features for using python like matlab
 
[데브루키]노대영_알고리즘 스터디
[데브루키]노대영_알고리즘 스터디[데브루키]노대영_알고리즘 스터디
[데브루키]노대영_알고리즘 스터디
 
From MSSQL to MySQL
From MSSQL to MySQLFrom MSSQL to MySQL
From MSSQL to MySQL
 
[Algorithm] Insertion Sort
[Algorithm] Insertion Sort[Algorithm] Insertion Sort
[Algorithm] Insertion Sort
 
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexity
 
이산치 과제7
이산치 과제7이산치 과제7
이산치 과제7
 
R 시작해보기
R 시작해보기R 시작해보기
R 시작해보기
 
2012 Dm A0 07 Pdf
2012 Dm A0 07 Pdf2012 Dm A0 07 Pdf
2012 Dm A0 07 Pdf
 
2012 Dm A0 07 Pdf
2012 Dm A0 07 Pdf2012 Dm A0 07 Pdf
2012 Dm A0 07 Pdf
 

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] Bubble Sort
[Algorithm] Bubble Sort[Algorithm] Bubble Sort
[Algorithm] Bubble Sort
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 - 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 - Queue
[Swift] Data Structure - Queue[Swift] Data Structure - Queue
[Swift] Data Structure - Queue
Bill Kim
 
[Swift] Data Structure - Linked List
[Swift] Data Structure - Linked List[Swift] Data Structure - Linked List
[Swift] Data Structure - Linked List
Bill Kim
 
[Swift] Data Structure Introduction
[Swift] Data Structure Introduction[Swift] Data Structure Introduction
[Swift] Data Structure Introduction
Bill Kim
 
Design Pattern Introduction
Design Pattern IntroductionDesign Pattern Introduction
Design Pattern Introduction
Bill Kim
 
[Swift] Visitor
[Swift] Visitor[Swift] Visitor
[Swift] Visitor
Bill Kim
 
[Swift] Template Method
[Swift] Template Method[Swift] Template Method
[Swift] Template Method
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] Bubble Sort
[Algorithm] Bubble Sort[Algorithm] Bubble Sort
[Algorithm] Bubble Sort
 
[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 - 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 - Queue
[Swift] Data Structure - Queue[Swift] Data Structure - Queue
[Swift] Data Structure - Queue
 
[Swift] Data Structure - Linked List
[Swift] Data Structure - Linked List[Swift] Data Structure - Linked List
[Swift] Data Structure - Linked List
 
[Swift] Data Structure Introduction
[Swift] Data Structure Introduction[Swift] Data Structure Introduction
[Swift] Data Structure Introduction
 
Design Pattern Introduction
Design Pattern IntroductionDesign Pattern Introduction
Design Pattern Introduction
 
[Swift] Visitor
[Swift] Visitor[Swift] Visitor
[Swift] Visitor
 
[Swift] Template Method
[Swift] Template Method[Swift] Template Method
[Swift] Template Method
 

[Algorithm] Quick Sort

  • 3. Quick Sort Quick Sort(퀵 정렬)는 대표적인 분할, 정복 정렬 알고리즘으로서 최악의 경우는 O(n^2)이지만 평균적으로는 O(nlogn)으로서 병합 정렬보다 보편적으로 빠른 속도를 가진 알고리즘입니다. 특정 값(Pivot)을 선택한 후 좌우로 작은 수와 큰 수 리스트를 나누 어 최종 배열을 합치는 방법으로 정렬을 하는 방식입니다. Pivot 값 선정에 따라서 일부 속도가 달라질 수 있는 그러한 알고 리즘입니다.
  • 4. Concept 기본적인 알고리즘의 컨셉을 살펴보면 아래와 같습니다. 1. 기준 값(Pivot)를 하나 선정한다. 2. 해당 값을 중심으로 하여 작은 수와 큰 수로 나눈다. ( L < 기준값 < R ) 3. 재귀 호출을 통하여 마지막 배열의 크기가 1개일때까지 1~2번 을 반복합니다. 4. 최종적으로 작은 수 배열과 기준 값(Pivot) 큰 수 배열을 합치면 정렬된 배열을 얻을 수 있습니다.
  • 6. Features Quick Sort(퀵 정렬)는 아래와 같은 특징을 가진 알고리즘입니다. 1. 분할 정복을 활용한 빠른 정렬 알고리즘 2. 평균적으로 삽입이나 병합 정렬보다 빠른 속도를 가짐 3. 초기 Pivot 선택에 따라서 속도가 차이가 날 수 있다. 4. 평균은 O(nlogn), 최악의 경우 O(n^2)의 시간복잡도를 가짐 5. 최악의 시간 복잡도를 가지는 경우는 정렬되어 있거나 역순일 경 우이므로 일반적인 상황에서는 거의 발생하지 않음
  • 7. Implementation Swift를 활용하여 퀵 정렬 알고리즘을 살펴보겠습니다. func quickSort<T: Comparable>(_ array: [T]) -> [T] { if array.count < 2 { print("last array : (array)") return array } else { print("array : (array)") } let pivot = array.first! print("pivot : (pivot)") let smaller = array.filter { $0 < pivot } let larger = array.filter { $0 > pivot } print("smaller : (smaller)") print("larger : (larger)") let result = quickSort(smaller) + [pivot] + quickSort(larger) print("smaller + pivot + larger : (result)") return result }
  • 8. Implementation let array = [3, 2, 5, 1, 4] print(quickSort(array)) // [1, 2, 3, 4, 5] // array : [3, 2, 5, 1, 4] // pivot : 3 // smaller : [2, 1] // larger : [5, 4] // array : [2, 1] // pivot : 2 // smaller : [1] // larger : [] // last array : [1] // last array : [] // smaller + pivot + larger : [1, 2] // array : [5, 4] // pivot : 5 // smaller : [4] // larger : [] // last array : [4] // last array : [] // smaller + pivot + larger : [4, 5] // smaller + pivot + larger : [1, 2, 3, 4, 5] // [1, 2, 3, 4, 5]
  • 9. References [1] Algorithm) 퀵 정렬(Quick Sort) in Swift : https://atelier-chez- moi.tistory.com/87 [2] 정렬 알고리즘 - Quick Sort (평균- nlogn, 최악- n^2) : https:// zeddios.tistory.com/35 [3] [Swift]Quick Sort : http://minsone.github.io/programming/ quick-sort-in-swift [4] [알고리즘] 퀵 정렬 (Quick sort) : http://blog.naver.com/ PostView.nhn? blogId=writer0713&logNo=221138306597&parentCategoryNo= &categoryNo=68&viewDate=&isShowPopularPosts=true&from =search [5] 퀵 정렬 quick sort with swift : https://hyerios.tistory.com/70
  • 10. References [6] 알고리즘 ] 3. 퀵 정렬 : https://its-blog.tistory.com/m/105? category=801513 [7] 피벗설정에 따른 퀵정렬의 속도 : https:// ghd5262.tistory.com/25 [8] [Swift 자료구조 ch11] 퀵소트 (Quick Sort) : https:// kor45cw.tistory.com/247 [9] How do you implement Quick Sort in Swift? : https:// medium.com/@notestomyself/how-do-you-implement- quick-sort-in-swift-d0dd0308a473 [10] Swift Quick Sort Algorithm With Recursion and Generics : https://medium.com/@augusteo/swift-quick- sort-algorithm-with-recursion-and-generics-78a256b3b392