From 514dc4ef0c8782ac4912e84e97222961af4f194d Mon Sep 17 00:00:00 2001 From: mcekr <> Date: Thu, 8 Oct 2020 18:57:53 +0800 Subject: [PATCH 1/3] Add isSorted --- Sources/Algorithms/isSorted.swift | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Sources/Algorithms/isSorted.swift diff --git a/Sources/Algorithms/isSorted.swift b/Sources/Algorithms/isSorted.swift new file mode 100644 index 00000000..e7182cef --- /dev/null +++ b/Sources/Algorithms/isSorted.swift @@ -0,0 +1,42 @@ +//===----------------------------------------------------------------------===// +// +// 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 + } +} From 0807ed02b0cc49c1c361443001a9cf9fe25a80f9 Mon Sep 17 00:00:00 2001 From: mcekr <> Date: Thu, 8 Oct 2020 19:20:33 +0800 Subject: [PATCH 2/3] Add IsSortedTests --- .../SwiftAlgorithmsTests/IsSortedTests.swift | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Tests/SwiftAlgorithmsTests/IsSortedTests.swift 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()) + } +} From a226cb982f2901dd0f600b3ead4c99e4ac55f67d Mon Sep 17 00:00:00 2001 From: mcekr <> Date: Thu, 8 Oct 2020 19:23:06 +0800 Subject: [PATCH 3/3] Add allEqual --- Sources/Algorithms/isSorted.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sources/Algorithms/isSorted.swift b/Sources/Algorithms/isSorted.swift index e7182cef..55e70ce8 100644 --- a/Sources/Algorithms/isSorted.swift +++ b/Sources/Algorithms/isSorted.swift @@ -39,4 +39,13 @@ extension Sequence where Element: Comparable { } 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: ==) + } }