際際滷

際際滷Share a Scribd company logo
Algorithm
Counting Sort
Bill Kim(蟾) | ibillkim@gmail.com
覈谿
Counting Sort
Concept
Features
Implementation
References
Counting Sort
Counting Sort(螻 ) 蠍一(Radix)螻 襷谿螳讌襦 螳 
襯 觜蟲讌 螻   螻襴讀.
螳 讌 觜蟲讌 螻 螳 螳 覈 螳 煙ロ讌 螳襯 
  覦覯.
企  伎 覈       
給.
  螳 覲旧° O(n+k)襦 旧企 覲 覲企
朱朱 觜襴.
讌襷 k螳 n覲企 る O(n) 襯 螳讌讌襷 k螳 n覲企 襷れ
 蟆曙磯 O(覓危)     螳讌螻 給.
 螻  るジ  觜伎 襷 覃覈襴螳 .
Concept
蠍磯蓋 螻襴讀 貉 危エ覲企  螳給.
1.  覦一伎 螳  襯 豢豢.
2. 覦一 伎  螳れ 螳襯 ロ 豺伎危 覦一伎  
螳襷 襷.
3. 豺伎危 覦一伎 0朱 豐蠍壱.
4.  覦一伎 覃伎  覦一伎 襯 碁煙る  豺伎危 覦一
 螳 讀螳.
5. 豺伎危 覦一伎 れ 伎 讌 れ 螳  豺伎
 覦一伎 譟一.
6.  覦一願骸 狩 蠍一 豢() 覦一伎 襷.
7.  覦一伎 襯 襦 覃伎  覦一伎 螳 豺伎危 覦
伎 碁煙るゼ 螳襴る 豺伎危 覦一伎 豺伎危 襯  觜殊.
8. 豺伎危 覦一伎 螳 豕譬 豢 覦一伎 碁煙るゼ 企 企
碁煙れ  覦一伎 螳 l伎.
9. 覈  覦一企 覦覲給語 覃 覈 螻殊 襷豺.
襷  螳 覦一伎 り 螳.
 覦一 : [ 3, 2, 4, 1]
企  覦一伎 企麹 豺伎危 覦一伎 襷る  螳給.
豺伎危 覦一 : [0, 1, 1, 1, 1]
豺伎危 覦一伎   螳 讀螳 襦 覲.
 豺伎危 覦一 : [0, 1, 2, 3, 4]
Concept
伎 豺伎危 覦一伎 谿語^ ル 覦一伎 螳 豕譬 豢 覦一伎 伎給.
 覦一 襦 一危磯ゼ 伎朱  螳 螻殊 蟇一蟆 .
1) 豢 覦一伎 2 覯讌 碁煙れ  覦一 3 螳 l
[ - - 3 - ]
2) 豢 覦一伎 1 覯讌 碁煙れ  覦一 2 螳 l
[ - 2 3 - ]
3) 豢 覦一伎 3 覯讌 碁煙れ  覦一 4 螳 l
[ - 2 3 4 ]
4) 豢 覦一伎 0 覯讌 碁煙れ  覦一 1 螳 l
[ 1 2 3 4 ]
 螳  覦一伎 襦 豕譬 豢 覦一企 蠍郁 覃  螳
 豕譬  覦一伎 詞  給.
豕譬  覦一 : [1, 2, 3, 4]
Concept
Features
Counting Sort(螻 )  螳 轟 螳讌 螻襴讀
.
1. 一危 觜蟲 れ 螳襯 牛  螻襴讀
2. 螳 覲旧°螳 O(n) 螳 豌  螻襴讀
3. 狩 螳 螳讌 れ    螻 螳 
襯 螳讌     螻襴讀
4. ル れ 豕 襷 覲 螻糾 覃 蟆曙一 
殊 襷れ 觜 螻糾 觜螳 企伎  
5. 螳   螳襷 觜蟲  蠍 覓語
Implementation
Swift襯  螻  螻襴讀 危エ覲願給.
func countingSort(_ array: [Int]) -> [Int] {
guard array.count > 0 else { return [] }
// Step 1
// Create an array to store the count of each element
let maxElement = array.max() ?? 0
var countArray = [Int](repeating: 0, count: Int(maxElement + 1))
for element in array {
countArray[element] += 1
}
print("inputArray : (array)")
print("countArray : (countArray)")
// Step 2
// Set each value to be the sum of the previous two values
for index in 1 ..< countArray.count {
let sum = countArray[index] + countArray[index - 1]
countArray[index] = sum
}
print("countArray(accumulated) : (countArray))
....
Implementation
....
// Step 3
// Place the element in the final array as per the number of elements before
it
print("inputArray : (array)")
var sortedArray = [Int](repeating: 0, count: array.count)
for element in array {
countArray[element] -= 1
sortedArray[countArray[element]] = element
print("豢 覦一伎 (countArray[element]) 覯讌 碁煙れ  覦一 (element) 螳
l")
//print("countArray[(element)] : (countArray[element])")
}
print("sortedArray : (sortedArray)")
return sortedArray
}
Implementation
var array = [3, 2, 4, 1]
print(countingSort(array))
// inputArray : [3, 2, 4, 1]
// countArray : [0, 1, 1, 1, 1]
// countArray(accumulated) : [0, 1, 2, 3, 4]
// inputArray : [3, 2, 4, 1]
// 豢 覦一伎 2 覯讌 碁煙れ  覦一 3 螳 l
// 豢 覦一伎 1 覯讌 碁煙れ  覦一 2 螳 l
// 豢 覦一伎 3 覯讌 碁煙れ  覦一 4 螳 l
// 豢 覦一伎 0 覯讌 碁煙れ  覦一 1 螳 l
// sortedArray : [1, 2, 3, 4]
// [1, 2, 3, 4]
References
[1] Counting Sort : 螻  : https://
bowbowbow.tistory.com/8
[2] 螻 (counting sort) : https://www.zerocho.com/
category/Algorithm/post/58006da88475ed00152d6c4b
[3] 螻 && 蠍一 : https://velog.io/@pa324/螻
-12k1akfhrm
[4] Counting Sort - 螻 : https://yaboong.github.io/
algorithms/2018/03/20/counting-sort/
[5] 11. 螻 (Counting Sort) : https://blog.naver.com/
ndb796/221228361368
References
[6] 12螳 - 螻 (Counting Sort) [ れ 螻襴讀 螳譬
(Algorithm Programming Tutorial) #12 ] : http://
www.ts-parfum.ru/video/n4kbFRn2z9M
[7] 豺伎危 ,   : https://ratsgo.github.io/
data%20structure&algorithm/2017/10/16/countingsort/
[8] 豺伎危  (Counting Sort) : https://
soobarkbar.tistory.com/101
[9] Counting Sort (螻 ) : https://
starkying.tistory.com/entry/Counting-Sort-螻-
[10] 螻 Counting sort : https://hongku.tistory.com/
155
Thank you!

More Related Content

What's hot (20)

[Algorithm] Bubble Sort
[Algorithm] Bubble Sort[Algorithm] Bubble Sort
[Algorithm] Bubble Sort
Bill Kim
螻襴讀 ろ磯() Seungdols
螻襴讀 ろ磯() Seungdols螻襴讀 ろ磯() Seungdols
螻襴讀 ろ磯() Seungdols
seungdols
Python ろ磯
Python ろ磯Python ろ磯
Python ろ磯
sanghyuck Na
[Commit Again] 1譯殊姶 STL study
[Commit Again] 1譯殊姶 STL study[Commit Again] 1譯殊姶 STL study
[Commit Again] 1譯殊姶 STL study
[Algorithm] Radix Sort
[Algorithm] Radix Sort[Algorithm] Radix Sort
[Algorithm] Radix Sort
Bill Kim
Python 螳譬 覦 襭
Python 螳譬 覦 襭Python 螳譬 覦 襭
Python 螳譬 覦 襭
Soobin Jung
R 蠍一 : R Basics
R 蠍一 : R BasicsR 蠍一 : R Basics
R 蠍一 : R Basics
Yoonwhan Lee
[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue
Bill Kim
粥沿沿鉛霞蟲
粥沿沿鉛霞蟲粥沿沿鉛霞蟲
粥沿沿鉛霞蟲
Kangwook Lee
R 蠍一 Part. 01
R 蠍一 Part. 01R 蠍一 Part. 01
R 蠍一 Part. 01
Yoonwhan Lee
1.襭蟲譟一 螻襴讀(螳襭)
1.襭蟲譟一 螻襴讀(螳襭)1.襭蟲譟一 螻襴讀(螳襭)
1.襭蟲譟一 螻襴讀(螳襭)
fmbvbfhs
R 蠍一 II
R 蠍一 IIR 蠍一 II
R 蠍一 II
Yoonwhan Lee
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
Acmicpcseminar2
Acmicpcseminar2Acmicpcseminar2
Acmicpcseminar2
yonsei
郁屋 襴ろ(蠍一)
郁屋 襴ろ(蠍一)郁屋 襴ろ(蠍一)
郁屋 襴ろ(蠍一)
Lee Geonhee
[磯襭]碁_螻襴讀 ろ磯
[磯襭]碁_螻襴讀 ろ磯[磯襭]碁_螻襴讀 ろ磯
[磯襭]碁_螻襴讀 ろ磯
R螻 蠍一糾 : 01.襭る蠍
R螻 蠍一糾 : 01.襭る蠍R螻 蠍一糾 : 01.襭る蠍
R螻 蠍一糾 : 01.襭る蠍
Yoonwhan Lee
[Algorithm] Bubble Sort
[Algorithm] Bubble Sort[Algorithm] Bubble Sort
[Algorithm] Bubble Sort
Bill Kim
螻襴讀 ろ磯() Seungdols
螻襴讀 ろ磯() Seungdols螻襴讀 ろ磯() Seungdols
螻襴讀 ろ磯() Seungdols
seungdols
[Commit Again] 1譯殊姶 STL study
[Commit Again] 1譯殊姶 STL study[Commit Again] 1譯殊姶 STL study
[Commit Again] 1譯殊姶 STL study
[Algorithm] Radix Sort
[Algorithm] Radix Sort[Algorithm] Radix Sort
[Algorithm] Radix Sort
Bill Kim
Python 螳譬 覦 襭
Python 螳譬 覦 襭Python 螳譬 覦 襭
Python 螳譬 覦 襭
Soobin Jung
R 蠍一 : R Basics
R 蠍一 : R BasicsR 蠍一 : R Basics
R 蠍一 : R Basics
Yoonwhan Lee
[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue
Bill Kim
粥沿沿鉛霞蟲
粥沿沿鉛霞蟲粥沿沿鉛霞蟲
粥沿沿鉛霞蟲
Kangwook Lee
R 蠍一 Part. 01
R 蠍一 Part. 01R 蠍一 Part. 01
R 蠍一 Part. 01
Yoonwhan Lee
1.襭蟲譟一 螻襴讀(螳襭)
1.襭蟲譟一 螻襴讀(螳襭)1.襭蟲譟一 螻襴讀(螳襭)
1.襭蟲譟一 螻襴讀(螳襭)
fmbvbfhs
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
Acmicpcseminar2
Acmicpcseminar2Acmicpcseminar2
Acmicpcseminar2
yonsei
郁屋 襴ろ(蠍一)
郁屋 襴ろ(蠍一)郁屋 襴ろ(蠍一)
郁屋 襴ろ(蠍一)
Lee Geonhee
[磯襭]碁_螻襴讀 ろ磯
[磯襭]碁_螻襴讀 ろ磯[磯襭]碁_螻襴讀 ろ磯
[磯襭]碁_螻襴讀 ろ磯
R螻 蠍一糾 : 01.襭る蠍
R螻 蠍一糾 : 01.襭る蠍R螻 蠍一糾 : 01.襭る蠍
R螻 蠍一糾 : 01.襭る蠍
Yoonwhan Lee

Similar to [Algorithm] Counting Sort (20)

[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #03 : (蠍磯蓋, , 豐
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #03 :  (蠍磯蓋, , 豐[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #03 :  (蠍磯蓋, , 豐
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #03 : (蠍磯蓋, , 豐
S.O.P.T - Shout Our Passion Together
Start IoT with JavaScript - 4.螳豌1
Start IoT with JavaScript - 4.螳豌1Start IoT with JavaScript - 4.螳豌1
Start IoT with JavaScript - 4.螳豌1
Park Jonggun
02. data structure and stl
02. data structure and stl02. data structure and stl
02. data structure and stl
麹 譟
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #01 : 螳, 蠏殊 覲旧°, 覦一, 郁屋襴ろ
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #01 : 螳, 蠏殊 覲旧°, 覦一, 郁屋襴ろ[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #01 : 螳, 蠏殊 覲旧°, 覦一, 郁屋襴ろ
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #01 : 螳, 蠏殊 覲旧°, 覦一, 郁屋襴ろ
S.O.P.T - Shout Our Passion Together
豺一 襦蠏碁
豺一 襦蠏碁豺一 襦蠏碁
豺一 襦蠏碁
譴 螻
Python + Excel
Python + Excel Python + Excel
Python + Excel
POSTECH
貊ろ 蟆 蠍 C++ 04_05_貊 ろ碁ゼ 覦 狩 覓碁
貊ろ 蟆 蠍 C++ 04_05_貊 ろ碁ゼ  覦 狩 覓碁貊ろ 蟆 蠍 C++ 04_05_貊 ろ碁ゼ  覦 狩 覓碁
貊ろ 蟆 蠍 C++ 04_05_貊 ろ碁ゼ 覦 狩 覓碁
ultrasuperrok
螳蟆渚 1譯殊姶 Stl study
螳蟆渚 1譯殊姶 Stl study螳蟆渚 1譯殊姶 Stl study
螳蟆渚 1譯殊姶 Stl study
貊ろ 蟆 蠍 C++ 13 (sorting) 螳 襭 .
貊ろ 蟆 蠍 C++ 13 (sorting)  螳 襭 .貊ろ 蟆 蠍 C++ 13 (sorting)  螳 襭 .
貊ろ 蟆 蠍 C++ 13 (sorting) 螳 襭 .
ultrasuperrok
Amugona study 1 jjw
Amugona study 1 jjwAmugona study 1 jjw
Amugona study 1 jjw
Amugona study 1 jjw
Amugona study 1 jjwAmugona study 1 jjw
Amugona study 1 jjw
R intro
R introR intro
R intro
譯殊
Linq to object using c#
Linq to object using c#Linq to object using c#
Linq to object using c#
覲蟇
2.linear regression and logistic regression
2.linear regression and logistic regression2.linear regression and logistic regression
2.linear regression and logistic regression
Haesun Park
Project#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Project#5 豕蟇磯Μ 谿剰鍵 D0 HwpProject#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Project#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Kimjeongmoo
襭蟲譟
襭蟲譟 襭蟲譟
襭蟲譟
Choonghyun Yang
2012 Dm A0 03 Pdf
2012 Dm A0 03 Pdf2012 Dm A0 03 Pdf
2012 Dm A0 03 Pdf
jinwookhong
2012 Dm A0 03 Pdf
2012 Dm A0 03 Pdf2012 Dm A0 03 Pdf
2012 Dm A0 03 Pdf
kd19h
螻襴讀
螻襴讀螻襴讀
螻襴讀
轟 覦
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #03 : (蠍磯蓋, , 豐
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #03 :  (蠍磯蓋, , 豐[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #03 :  (蠍磯蓋, , 豐
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #03 : (蠍磯蓋, , 豐
S.O.P.T - Shout Our Passion Together
Start IoT with JavaScript - 4.螳豌1
Start IoT with JavaScript - 4.螳豌1Start IoT with JavaScript - 4.螳豌1
Start IoT with JavaScript - 4.螳豌1
Park Jonggun
02. data structure and stl
02. data structure and stl02. data structure and stl
02. data structure and stl
麹 譟
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #01 : 螳, 蠏殊 覲旧°, 覦一, 郁屋襴ろ
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #01 : 螳, 蠏殊 覲旧°, 覦一, 郁屋襴ろ[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #01 : 螳, 蠏殊 覲旧°, 覦一, 郁屋襴ろ
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #01 : 螳, 蠏殊 覲旧°, 覦一, 郁屋襴ろ
S.O.P.T - Shout Our Passion Together
豺一 襦蠏碁
豺一 襦蠏碁豺一 襦蠏碁
豺一 襦蠏碁
譴 螻
Python + Excel
Python + Excel Python + Excel
Python + Excel
POSTECH
貊ろ 蟆 蠍 C++ 04_05_貊 ろ碁ゼ 覦 狩 覓碁
貊ろ 蟆 蠍 C++ 04_05_貊 ろ碁ゼ  覦 狩 覓碁貊ろ 蟆 蠍 C++ 04_05_貊 ろ碁ゼ  覦 狩 覓碁
貊ろ 蟆 蠍 C++ 04_05_貊 ろ碁ゼ 覦 狩 覓碁
ultrasuperrok
螳蟆渚 1譯殊姶 Stl study
螳蟆渚 1譯殊姶 Stl study螳蟆渚 1譯殊姶 Stl study
螳蟆渚 1譯殊姶 Stl study
貊ろ 蟆 蠍 C++ 13 (sorting) 螳 襭 .
貊ろ 蟆 蠍 C++ 13 (sorting)  螳 襭 .貊ろ 蟆 蠍 C++ 13 (sorting)  螳 襭 .
貊ろ 蟆 蠍 C++ 13 (sorting) 螳 襭 .
ultrasuperrok
Amugona study 1 jjw
Amugona study 1 jjwAmugona study 1 jjw
Amugona study 1 jjw
Amugona study 1 jjw
Amugona study 1 jjwAmugona study 1 jjw
Amugona study 1 jjw
R intro
R introR intro
R intro
譯殊
Linq to object using c#
Linq to object using c#Linq to object using c#
Linq to object using c#
覲蟇
2.linear regression and logistic regression
2.linear regression and logistic regression2.linear regression and logistic regression
2.linear regression and logistic regression
Haesun Park
Project#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Project#5 豕蟇磯Μ 谿剰鍵 D0 HwpProject#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Project#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Kimjeongmoo
2012 Dm A0 03 Pdf
2012 Dm A0 03 Pdf2012 Dm A0 03 Pdf
2012 Dm A0 03 Pdf
jinwookhong
2012 Dm A0 03 Pdf
2012 Dm A0 03 Pdf2012 Dm A0 03 Pdf
2012 Dm A0 03 Pdf
kd19h
螻襴讀
螻襴讀螻襴讀
螻襴讀
轟 覦

More from Bill Kim (19)

[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] Shell Sort
[Algorithm] Shell Sort[Algorithm] Shell Sort
[Algorithm] Shell Sort
Bill Kim
[Algorithm] Heap Sort
[Algorithm] Heap Sort[Algorithm] Heap Sort
[Algorithm] Heap Sort
Bill Kim
[Algorithm] Merge Sort
[Algorithm] Merge Sort[Algorithm] Merge Sort
[Algorithm] Merge 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 - 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
[Swift] State
[Swift] State[Swift] State
[Swift] State
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] Shell Sort
[Algorithm] Shell Sort[Algorithm] Shell Sort
[Algorithm] Shell Sort
Bill Kim
[Algorithm] Heap Sort
[Algorithm] Heap Sort[Algorithm] Heap Sort
[Algorithm] Heap Sort
Bill Kim
[Algorithm] Merge Sort
[Algorithm] Merge Sort[Algorithm] Merge Sort
[Algorithm] Merge 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 - 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
[Swift] State
[Swift] State[Swift] State
[Swift] State
Bill Kim

[Algorithm] Counting Sort

  • 3. Counting Sort Counting Sort(螻 ) 蠍一(Radix)螻 襷谿螳讌襦 螳 襯 觜蟲讌 螻 螻襴讀. 螳 讌 觜蟲讌 螻 螳 螳 覈 螳 煙ロ讌 螳襯 覦覯. 企 伎 覈 給. 螳 覲旧° O(n+k)襦 旧企 覲 覲企 朱朱 觜襴. 讌襷 k螳 n覲企 る O(n) 襯 螳讌讌襷 k螳 n覲企 襷れ 蟆曙磯 O(覓危) 螳讌螻 給. 螻 るジ 觜伎 襷 覃覈襴螳 .
  • 4. Concept 蠍磯蓋 螻襴讀 貉 危エ覲企 螳給. 1. 覦一伎 螳 襯 豢豢. 2. 覦一 伎 螳れ 螳襯 ロ 豺伎危 覦一伎 螳襷 襷. 3. 豺伎危 覦一伎 0朱 豐蠍壱. 4. 覦一伎 覃伎 覦一伎 襯 碁煙る 豺伎危 覦一 螳 讀螳. 5. 豺伎危 覦一伎 れ 伎 讌 れ 螳 豺伎 覦一伎 譟一. 6. 覦一願骸 狩 蠍一 豢() 覦一伎 襷. 7. 覦一伎 襯 襦 覃伎 覦一伎 螳 豺伎危 覦 伎 碁煙るゼ 螳襴る 豺伎危 覦一伎 豺伎危 襯 觜殊. 8. 豺伎危 覦一伎 螳 豕譬 豢 覦一伎 碁煙るゼ 企 企 碁煙れ 覦一伎 螳 l伎. 9. 覈 覦一企 覦覲給語 覃 覈 螻殊 襷豺.
  • 5. 襷 螳 覦一伎 り 螳. 覦一 : [ 3, 2, 4, 1] 企 覦一伎 企麹 豺伎危 覦一伎 襷る 螳給. 豺伎危 覦一 : [0, 1, 1, 1, 1] 豺伎危 覦一伎 螳 讀螳 襦 覲. 豺伎危 覦一 : [0, 1, 2, 3, 4] Concept
  • 6. 伎 豺伎危 覦一伎 谿語^ ル 覦一伎 螳 豕譬 豢 覦一伎 伎給. 覦一 襦 一危磯ゼ 伎朱 螳 螻殊 蟇一蟆 . 1) 豢 覦一伎 2 覯讌 碁煙れ 覦一 3 螳 l [ - - 3 - ] 2) 豢 覦一伎 1 覯讌 碁煙れ 覦一 2 螳 l [ - 2 3 - ] 3) 豢 覦一伎 3 覯讌 碁煙れ 覦一 4 螳 l [ - 2 3 4 ] 4) 豢 覦一伎 0 覯讌 碁煙れ 覦一 1 螳 l [ 1 2 3 4 ] 螳 覦一伎 襦 豕譬 豢 覦一企 蠍郁 覃 螳 豕譬 覦一伎 詞 給. 豕譬 覦一 : [1, 2, 3, 4] Concept
  • 7. Features Counting Sort(螻 ) 螳 轟 螳讌 螻襴讀 . 1. 一危 觜蟲 れ 螳襯 牛 螻襴讀 2. 螳 覲旧°螳 O(n) 螳 豌 螻襴讀 3. 狩 螳 螳讌 れ 螻 螳 襯 螳讌 螻襴讀 4. ル れ 豕 襷 覲 螻糾 覃 蟆曙一 殊 襷れ 觜 螻糾 觜螳 企伎 5. 螳 螳襷 觜蟲 蠍 覓語
  • 8. Implementation Swift襯 螻 螻襴讀 危エ覲願給. func countingSort(_ array: [Int]) -> [Int] { guard array.count > 0 else { return [] } // Step 1 // Create an array to store the count of each element let maxElement = array.max() ?? 0 var countArray = [Int](repeating: 0, count: Int(maxElement + 1)) for element in array { countArray[element] += 1 } print("inputArray : (array)") print("countArray : (countArray)") // Step 2 // Set each value to be the sum of the previous two values for index in 1 ..< countArray.count { let sum = countArray[index] + countArray[index - 1] countArray[index] = sum } print("countArray(accumulated) : (countArray)) ....
  • 9. Implementation .... // Step 3 // Place the element in the final array as per the number of elements before it print("inputArray : (array)") var sortedArray = [Int](repeating: 0, count: array.count) for element in array { countArray[element] -= 1 sortedArray[countArray[element]] = element print("豢 覦一伎 (countArray[element]) 覯讌 碁煙れ 覦一 (element) 螳 l") //print("countArray[(element)] : (countArray[element])") } print("sortedArray : (sortedArray)") return sortedArray }
  • 10. Implementation var array = [3, 2, 4, 1] print(countingSort(array)) // inputArray : [3, 2, 4, 1] // countArray : [0, 1, 1, 1, 1] // countArray(accumulated) : [0, 1, 2, 3, 4] // inputArray : [3, 2, 4, 1] // 豢 覦一伎 2 覯讌 碁煙れ 覦一 3 螳 l // 豢 覦一伎 1 覯讌 碁煙れ 覦一 2 螳 l // 豢 覦一伎 3 覯讌 碁煙れ 覦一 4 螳 l // 豢 覦一伎 0 覯讌 碁煙れ 覦一 1 螳 l // sortedArray : [1, 2, 3, 4] // [1, 2, 3, 4]
  • 11. References [1] Counting Sort : 螻 : https:// bowbowbow.tistory.com/8 [2] 螻 (counting sort) : https://www.zerocho.com/ category/Algorithm/post/58006da88475ed00152d6c4b [3] 螻 && 蠍一 : https://velog.io/@pa324/螻 -12k1akfhrm [4] Counting Sort - 螻 : https://yaboong.github.io/ algorithms/2018/03/20/counting-sort/ [5] 11. 螻 (Counting Sort) : https://blog.naver.com/ ndb796/221228361368
  • 12. References [6] 12螳 - 螻 (Counting Sort) [ れ 螻襴讀 螳譬 (Algorithm Programming Tutorial) #12 ] : http:// www.ts-parfum.ru/video/n4kbFRn2z9M [7] 豺伎危 , : https://ratsgo.github.io/ data%20structure&algorithm/2017/10/16/countingsort/ [8] 豺伎危 (Counting Sort) : https:// soobarkbar.tistory.com/101 [9] Counting Sort (螻 ) : https:// starkying.tistory.com/entry/Counting-Sort-螻- [10] 螻 Counting sort : https://hongku.tistory.com/ 155