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

PDF
[Algorithm] Selection Sort
PDF
[Swift] Data Structure - Heap
PDF
[Algorithm] Counting Sort
PPT
[0326 석재호]상호배타적 집합의 처리
PPTX
컴퓨터개론09
PDF
BOJ10547
PDF
알고리즘 스터디(정렬) Seungdols
PDF
Binary Search
[Algorithm] Selection Sort
[Swift] Data Structure - Heap
[Algorithm] Counting Sort
[0326 석재호]상호배타적 집합의 처리
컴퓨터개론09
BOJ10547
알고리즘 스터디(정렬) Seungdols
Binary Search

What's hot (11)

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

Similar to [Algorithm] Quick Sort (20)

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

More from Bill Kim (20)

PDF
[Algorithm] Sorting Comparison
PDF
[Algorithm] Big O Notation
PDF
[Algorithm] Radix Sort
PDF
[Algorithm] Bubble Sort
PDF
[Algorithm] Recursive(재귀)
PDF
[Swift] Data Structure - AVL
PDF
[Swift] Data Structure - Binary Search Tree
PDF
[Swift] Data Structure - Graph(BFS)
PDF
[Swift] Data Structure - Graph(DFS)
PDF
[Swift] Data Structure - Binary Tree
PDF
[Swift] Data Structure - Tree
PDF
[Swift] Data Structure - Graph
PDF
[Swift] Data Structure - Dequeue
PDF
[Swift] Data Structure - Stack
PDF
[Swift] Data Structure - Queue
PDF
[Swift] Data Structure - Linked List
PDF
[Swift] Data Structure Introduction
PDF
Design Pattern Introduction
PDF
[Swift] Visitor
PDF
[Swift] Template Method
[Algorithm] Sorting Comparison
[Algorithm] Big O Notation
[Algorithm] Radix Sort
[Algorithm] Bubble Sort
[Algorithm] Recursive(재귀)
[Swift] Data Structure - AVL
[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Tree
[Swift] Data Structure - Graph
[Swift] Data Structure - Dequeue
[Swift] Data Structure - Stack
[Swift] Data Structure - Queue
[Swift] Data Structure - Linked List
[Swift] Data Structure Introduction
Design Pattern Introduction
[Swift] Visitor
[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