forked from waynewbishop/bishop-algorithms-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sortable.swift
41 lines (26 loc) · 808 Bytes
/
Sortable.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
35
36
37
38
39
40
41
//
// Sortable.swift
// SwiftStructures
//
// Created by Wayne Bishop on 7/1/16.
// Copyright © 2016 Arbutus Software Inc. All rights reserved.
//
import Foundation
//note: an extension on a protocol
extension Sortable {
func isSorted<T: Comparable>(_ sequence: Array<T>) -> Bool {
//check trivial cases
guard sequence.count >= 1 else {
return true
}
var index = sequence.startIndex
//compare sequence values
while index < sequence.endIndex - 1 {
if sequence[index] > sequence[sequence.index(after: index)] {
return false
}
index = sequence.index(after: index)
}
return true
}
}