diff --git a/Sources/Algorithms/isSorted.swift b/Sources/Algorithms/isSorted.swift new file mode 100644 index 00000000..55e70ce8 --- /dev/null +++ b/Sources/Algorithms/isSorted.swift @@ -0,0 +1,51 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Algorithms open source project +// +// Copyright (c) 2020 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +//===----------------------------------------------------------------------===// +// isSorted() +//===----------------------------------------------------------------------===// + + +extension Sequence where Element: Comparable { + public func isSorted() -> Bool { + /// Returns Bool, indicating whether a sequence is sorted + /// into non-descending order. + /// + /// - Complexity: O(*n*), where *n* is the length of the sequence. + + isSorted(by: <) + } + + public func isSorted(by areInIncreasingOrder: (Element, Element) -> Bool) -> Bool { + /// Returns Bool, indicating whether a sequence is sorted using + /// the given predicate as the comparison between elements. + /// + /// - Complexity: O(*n*), where *n* is the length of the sequence. + + var prev: Element? + for element in self { + if let p = prev, !areInIncreasingOrder(p, element) { + return false + } + prev = element + } + return true + } + + public func allEqual() -> Bool { + /// Returns Bool, indicating whether all the + /// elements in sequence are equal to each other. + /// + /// - Complexity: O(*n*), where *n* is the length of the sequence. + + isSorted(by: ==) + } +} diff --git a/Tests/SwiftAlgorithmsTests/IsSortedTests.swift b/Tests/SwiftAlgorithmsTests/IsSortedTests.swift new file mode 100644 index 00000000..ba808089 --- /dev/null +++ b/Tests/SwiftAlgorithmsTests/IsSortedTests.swift @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Algorithms open source project +// +// Copyright (c) 2020 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +import XCTest +import Algorithms + +final class IsSortedTests: XCTestCase { + + func testIsSorted() { + let a = 0...10 + let b = (0...10).reversed() + let c = Array(repeating: 42, count: 10) + XCTAssertTrue(a.isSorted()) + XCTAssertTrue(b.isSorted(by: >)) + XCTAssertTrue(c.allEqual()) + } +}