You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Ordered Set/README.md
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ An Ordered Set is a collection of unique items in sorted order. Items are usuall
3
3
For example, we could define "a" and "z" to have the same value (their lengths), but clearly "a" != "z".
4
4
5
5
## Why use an Ordered Set?
6
-
Ordered Sets should be considered for use when you need to require keeping your collection sorted at all times, and do lookups on the collection much more freuqently than inserting or deleting items. A good example would be keeping track of the rankings of players in a scoreboard (see example 2 below). Many of the lookup operations for an Ordered Set are **O(1)**.
6
+
Ordered Sets should be considered for use when you require keeping your collection sorted at all times, and do lookups on the collection much more freuqently than inserting or deleting items. A good example would be keeping track of the rankings of players in a scoreboard (see example 2 below). Many of the lookup operations for an Ordered Set are **O(1)**.
7
7
8
8
### These are Ordered Sets
9
9
```
@@ -26,7 +26,7 @@ This Set violates the sorted property
26
26
```
27
27
28
28
## The Code
29
-
We'll start by creating our internal representation for the Ordered Set. Since the idea of a set is similar to that of an array, we will use an array to represent our set. Furthermore, since we'll need to keep our set sorted, we need to compare the individual elemants. Thus, any type must conform to the [Comparable Protocol](https://developer.apple.com/library/watchos/documentation/Swift/Reference/Swift_Comparable_Protocol/index.html).
29
+
We'll start by creating our internal representation for the Ordered Set. Since the idea of a set is similar to that of an array, we will use an array to represent our set. Furthermore, since we'll need to keep our set sorted, we need to compare the individual elements. Thus, any type must conform to the [Comparable Protocol](https://developer.apple.com/library/watchos/documentation/Swift/Reference/Swift_Comparable_Protocol/index.html).
30
30
31
31
```swift
32
32
publicstructOrderedSet<T: Comparable> {
@@ -68,7 +68,7 @@ Lets take a look at the insert function first. The insert function first checks
68
68
internalSet.append(item)
69
69
}
70
70
```
71
-
The first part of the function checks if the item is already in the set.As we'll see later on, this has an efficiency of **O(log(n) + k)**where k is the number of items with the same value as the item we are inserting. The second part iterates through the interal array so that it can find a spot for our given item. This is at worse **O(n)**. The insert function for arrays has an efficiency of **O(log(n))**, thus making the insert function for our Ordered Set**O(log(n)+ k)**.
71
+
The first part of the function checks if the item is already in the set.As we'll see later on, this has an efficiency of **O(log(n) + k)**where k is the number of items with the same value as the item we are inserting. The second part iterates through the interal array so that it can find a spot for our given item. This is at worse **O(n)**. The insert function for arrays has an efficiency of **O(nlog(n))**, thus making the insert function for our Ordered Set**O(nlog(n))**.
72
72
73
73
74
74
Next we have the `remove` function. First check if the item exists. If not, then return and no nothing. If it does exist, remove it.
@@ -83,7 +83,7 @@ Next we have the `remove` function. First check if the item exists. If not, then
83
83
internalSet.removeAtIndex(findIndex(item))
84
84
}
85
85
```
86
-
Again, because of the `exists` function, the efficiency for remove is**O(log(n)+ k)**
86
+
Again, because of the `removeAtIndex` function, the efficiency for remove is**O(nlog(n))**
87
87
88
88
The next function is the `findIndex` function which takes in an item of type `T` and returns the index of the item if it isin the set, otherwise returns -1.
89
89
@@ -194,7 +194,7 @@ Since the set is sorted, the following operations are all **O(1)**:
194
194
Below are a few examples that can be found in the playground file.
195
195
196
196
### Example 1
197
-
Here we create a set with random Integers. Pringint the largest/smallest 5 numbers in the setis fairly easy.
197
+
Here we create a set with random Integers. Printing the largest/smallest 5 numbers in the setis fairly easy.
The set we create will hold players. One thing to note is that two `Player`'s can each have the same value, but are not guaranteed to be equal.
251
+
The set we create will hold players. One thing to note is that two `Player`s can each have the same value, but are not guaranteed to be equal.
252
252
253
253
Inserting 20 random players and one player we will track of.
254
254
``` swift
@@ -272,7 +272,7 @@ print(playerSet.max())
272
272
print(playerSet.min())
273
273
```
274
274
275
-
Next we use the findIndex function to find out what rank `anotherPlayer` in comparison to the other `Player`s.
275
+
Next we use the findIndex function to find out what rank `anotherPlayer` is.
276
276
``` swift
277
277
// we'll find our player now
278
278
print("'Another Player (\(anotherPlayer.name))' is ranked at level: \(playerSet.count- playerSet.findIndex(anotherPlayer)) with \(anotherPlayer.points) points")
0 commit comments