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: Select Minimum Maximum/README.markdown
+74-4Lines changed: 74 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,11 @@
2
2
3
3
Goal: Find the minimum/maximum object in an unsorted array.
4
4
5
+
### Maximum or minimum
6
+
5
7
We have an array of generic objects and we iterate over all the objects keeping track of the minimum/maximum element so far.
6
8
7
-
### An example
9
+
####An example
8
10
9
11
Let's say the we want to find the maximum value in the unsorted list `[ 8, 3, 9, 4, 6 ]`.
10
12
@@ -14,9 +16,9 @@ Pick the next number from the list, `3`, and compare it to the current maximum `
14
16
15
17
Pick the next number from the list, `9`, and compare it to the current maximum `8`. `9` is greater than `8` so we store `9` as the maximum.
16
18
17
-
Repeat this process until the all items in the list have been processed.
19
+
Repeat this process until the all elements in the list have been processed.
18
20
19
-
### The code
21
+
####The code
20
22
21
23
Here is a simple implementation in Swift:
22
24
@@ -46,9 +48,77 @@ minimum(array) // This will return 3
46
48
maximum(array) // This will return 9
47
49
```
48
50
51
+
### Maximum and minimum
52
+
53
+
To find both the maximum and minimum values contained in array while minimizing the number of comparisons we can compare the items in pairs.
54
+
55
+
#### An example
56
+
57
+
Let's say the we want to find the minimum and maximum value in the unsorted list `[ 8, 3, 9, 4, 6 ]`.
58
+
59
+
Pick the first number, `8`, and store it as the minimum and maximum element so far.
60
+
61
+
Because we have an odd number of items we remove `8` from the list which leaves the pairs `[ 3, 9 ]` and `[ 4, 6 ]`.
62
+
63
+
Pick the next pair of numbers from the list, `[ 3, 9 ]`, `3` is less than `9` so we compare `3` to the current minimum `8` and `9` to the current maximum `8`. `3` is less than `8` so the new minimum is `3`. `9` is greater than `8` so the new maximum is `9`.
64
+
65
+
Pick the next pair of numbers from the list, `[ 4, 6 ]`, `4` is less than `6` so we compare `4` to the current minimum `3` and `6` to the current maximum `9`. `4` is greater than `3` so the minimum does not change. `6` is less than `9` so the maximum does not change.
66
+
67
+
The result is a minimum of `3` and a maximum of `9`.
let pair = (array.removeFirst(), array.removeFirst())
86
+
87
+
if pair.0 > pair.1 {
88
+
if pair.0 > maximum {
89
+
maximum = pair.0
90
+
}
91
+
if pair.1 < minimum {
92
+
minimum = pair.1
93
+
}
94
+
} else {
95
+
if pair.1 > maximum {
96
+
maximum = pair.1
97
+
}
98
+
if pair.0 < minimum {
99
+
minimum = pair.0
100
+
}
101
+
}
102
+
}
103
+
104
+
return (minimum, maximum)
105
+
}
106
+
```
107
+
108
+
Put this code in a playground and test it like so:
109
+
110
+
```swift
111
+
112
+
let result =minimumMaximum(array)
113
+
result.minimum// This will return 3
114
+
result.maximum// This will return 9
115
+
```
116
+
117
+
By picking elements in pairs and comparing their maximum and minimum with the running minimum and maximum we reduce the number of comparisons to 3 for every 2 elements.
118
+
49
119
### Performance
50
120
51
-
The algorithm runs at **O(n)**. It compares each object in the array with the running minimum/maximum so the time it takes is proportional to the array length.
121
+
These algorithms run at **O(n)**. Each object in the array is compared with the running minimum/maximum so the time it takes is proportional to the array length.
0 commit comments