-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathContents.swift
34 lines (24 loc) · 904 Bytes
/
Contents.swift
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
32
33
34
//: [Previous](@previous)
import Foundation
// Shell sort is an improved version of insertion sort. The original list is broken into smaller sublists and then individually sorted using insertion sort.
extension Array where Element: Comparable {
public mutating func shellSorted(order sign: (Element, Element) -> Bool) {
var subcount = self.count / 2
while subcount > 0 {
for i in 0..<self.count {
let temp = self[i]
var j = i
while j >= subcount && sign(self[j - subcount], temp) {
self[j] = self[j - subcount]
j -= subcount
}
self[j] = temp
}
subcount /= 2
}
}
}
//: Usage
var data = [4, 1, 5, 6, 1, 2, 9, 8, 5, 4, 2, 7, 6]
data.shellSorted(order: >)
//: [Next](@next)