Skip to content

Commit 119718f

Browse files
author
Chris Pilcher
committed
Selecting the minimum/maximum value in an array
1 parent de971c5 commit 119718f

File tree

7 files changed

+120
-1
lines changed

7 files changed

+120
-1
lines changed

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ If you're new to algorithms and data structures, here are a few good ones to sta
3333
- [Linear Search](Linear Search/). Find an element in an array.
3434
- [Binary Search](Binary Search/). Quickly find elements in a sorted array.
3535
- [Count Occurrences](Count Occurrences/). Count how often a value appears in an array.
36-
- Select Minimum / Maximum
36+
- [Select Minimum / Maximum](Select Minimum Maximum). Find the minimum/maximum value in an array.
3737
- Select k-th Largest Element
3838
- Selection Sampling
3939
- Union-Find

Select Minimum Maximum/Maximum.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
Finds the maximum value in an array in O(n) time.
3+
The array must not be empty.
4+
*/
5+
6+
func maximum<T: Comparable>(var array: [T]) -> T? {
7+
var maximum = array.removeFirst()
8+
for element in array {
9+
maximum = element > maximum ? element : maximum
10+
}
11+
return maximum
12+
}

Select Minimum Maximum/Minimum.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
Finds the minimum value in an array in O(n) time.
3+
The array must not be empty.
4+
*/
5+
6+
func minimum<T: Comparable>(var array: [T]) -> T? {
7+
var minimum = array.removeFirst()
8+
for element in array {
9+
minimum = element < minimum ? element : minimum
10+
}
11+
return minimum
12+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Select Minimum / Maximum
2+
3+
Goal: Find the minimum/maximum object in an unsorted array.
4+
5+
We have an array of generic objects and we iterate over all the objects keeping track of the minimum/maximum element so far.
6+
7+
### An example
8+
9+
Let's say the we want to find the maximum value in the unsorted list `[ 8, 3, 9, 4, 6 ]`.
10+
11+
Pick the first number, `8`, and store it as the maximum element so far.
12+
13+
Pick the next number from the list, `3`, and compare it to the current maximum `8`. `3` is less than `8` so the maximum `8` does not change.
14+
15+
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+
17+
Repeat this process until the all items in the list have been processed.
18+
19+
### The code
20+
21+
Here is a simple implementation in Swift:
22+
23+
```swift
24+
func minimum<T: Comparable>(var array: [T]) -> T? {
25+
var minimum = array.removeFirst()
26+
for element in array {
27+
minimum = element < minimum ? element : minimum
28+
}
29+
return minimum
30+
}
31+
32+
func maximum<T: Comparable>(var array: [T]) -> T? {
33+
var maximum = array.removeFirst()
34+
for element in array {
35+
maximum = element > maximum ? element : maximum
36+
}
37+
return maximum
38+
}
39+
```
40+
41+
Put this code in a playground and test it like so:
42+
43+
```swift
44+
let array = [ 8, 3, 9, 4, 6 ]
45+
minimum(array) // This will return 3
46+
maximum(array) // This will return 9
47+
```
48+
49+
### Performance
50+
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.
52+
53+
### Swift library
54+
55+
The Swift library already contains an extension to `SequenceType` that returns the minimum/maximum element in a sequence.
56+
57+
```swift
58+
let array = [ 8, 3, 9, 4, 6 ]
59+
array.minElement() // This will return 3
60+
array.maxElement() // This will return 9
61+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func minimum<T: Comparable>(var array: [T]) -> T? {
2+
var minimum = array.removeFirst()
3+
for element in array {
4+
minimum = element < minimum ? element : minimum
5+
}
6+
return minimum
7+
}
8+
9+
func maximum<T: Comparable>(var array: [T]) -> T? {
10+
var maximum = array.removeFirst()
11+
for element in array {
12+
maximum = element > maximum ? element : maximum
13+
}
14+
return maximum
15+
}
16+
17+
// Simple test minimum and maximum functions
18+
let array = [ 8, 3, 9, 4, 6 ]
19+
minimum(array)
20+
maximum(array)
21+
22+
// Built-in Swift functions
23+
array.maxElement()
24+
array.minElement()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

0 commit comments

Comments
 (0)