Skip to content

Commit ba69a89

Browse files
committed
Fix big-O performance for insert and remove.
Was O(n log n) but should be O(n).
1 parent a656e25 commit ba69a89

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

Ordered Set/OrderedSet.playground/Sources/OrderedSet.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public struct OrderedSet<T: Comparable> {
1313
return internalSet.count
1414
}
1515

16-
// Inserts an item. Performance: O(n log n)
16+
// Inserts an item. Performance: O(n)
1717
public mutating func insert(item: T){
1818
if exists(item) {
1919
return // don't add an item if it already exists
@@ -31,7 +31,7 @@ public struct OrderedSet<T: Comparable> {
3131
internalSet.append(item)
3232
}
3333

34-
// Removes an item if it exists.
34+
// Removes an item if it exists. Performance: O(n)
3535
public mutating func remove(item: T) {
3636
if let index = indexOf(item) {
3737
internalSet.removeAtIndex(index)

Ordered Set/OrderedSet.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public struct OrderedSet<T: Comparable> {
1313
return internalSet.count
1414
}
1515

16-
// Inserts an item. Performance: O(n log n)
16+
// Inserts an item. Performance: O(n)
1717
public mutating func insert(item: T){
1818
if exists(item) {
1919
return // don't add an item if it already exists
@@ -31,7 +31,7 @@ public struct OrderedSet<T: Comparable> {
3131
internalSet.append(item)
3232
}
3333

34-
// Removes an item if it exists.
34+
// Removes an item if it exists. Performance: O(n)
3535
public mutating func remove(item: T) {
3636
if let index = indexOf(item) {
3737
internalSet.removeAtIndex(index)

Ordered Set/README.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ As we'll see later on, checking if the item is already in the set has an efficie
7777

7878
To insert the new item, the `for` loop starts from the beginning of the array, and checks to see if each item is larger than the item we want to insert. Once we find such an item, we insert the new one into its place. This shifts the rest of the array over to the right by 1 position. This loop is at worst **O(n)**.
7979

80-
The total performance of the `insert()` function is therefore **O(n log(n))**.
80+
The total performance of the `insert()` function is therefore **O(n)**.
8181

8282
Next up is the `remove()` function:
8383

@@ -89,7 +89,7 @@ Next up is the `remove()` function:
8989
}
9090
```
9191

92-
First this checks if the item exists and then removes it from the array. Because of the `removeAtIndex()` function, the efficiency for remove is **O(n log(n))**.
92+
First this checks if the item exists and then removes it from the array. Because of the `removeAtIndex()` function, the efficiency for remove is **O(n)**.
9393

9494
The next function is `indexOf()`, which takes in an object of type `T` and returns the index of the corresponding item if it is in the set, or `nil` if it is not. Since our set is sorted, we can use a binary search to quickly search for the item.
9595

0 commit comments

Comments
 (0)