主要使用分治算法
FindMaxSubArray.swift
如下:
//Max SubArray
func findMaxCrossingSubarray(arr: [Int], low: Int, mid: Int, high: Int) -> (max_left: Int, max_right: Int, sum: Int) {
print("start findMaxCrossingSubarray: arr is \(arr), low is \(low), mid is \(mid),high is \(high)")
var max_left = low
var max_right = high
var left_sum = 0
var right_sum = 0
var sum = 0
for index in (low...mid).reversed() {
//尝试从 mid 到 low 去遍历
sum = sum + arr[index]
if sum > left_sum {
//如果加上的和更大,那么就增加
left_sum = sum
max_left = index
}
}
sum = 0
for index in mid+1...high {
//尝试从 mid 到 high 遍历
sum = sum + arr[index]
if sum > right_sum {
//如果加上的和更大,那么就增加
right_sum = sum
max_right = index
}
}
print("finish findMaxCrossingSubarray. max_left is \(max_left), max_right is \(max_right), sum is \(left_sum + right_sum)")
return (max_left, max_right, left_sum + right_sum)
}
func findMaximumSubarray(arr: [Int], low: Int, high: Int) -> (max_left: Int, max_right: Int, sum: Int) {
print("start findMaximumSubarray: arr is \(arr), low is \(low), high is \(high)")
var left_low, left_high, left_sum: Int
var right_low, right_high, right_sum: Int
var cross_low, cross_high, cross_sum: Int
var mid: Int
if high == low {
print("high is equal to low, are \(high)")
print("")
return (low, high, arr[low])
} else {
mid = (low + high) / 2
(left_low, left_high, left_sum) = findMaximumSubarray(arr: arr, low: low, high: mid)
(right_low, right_high, right_sum) = findMaximumSubarray(arr: arr, low: mid + 1, high: high)
(cross_low, cross_high, cross_sum) = findMaxCrossingSubarray(arr: arr, low: low, mid: mid, high: high)
}
print("finish findMaximumSubarray.")
print("")
if left_sum >= right_sum && left_sum >= cross_sum {
print("left_low is \(left_low), left_high is \(left_high), left_sum is \(left_sum))")
print("")
return (left_low, left_high, left_sum)
} else if right_sum >= left_sum && right_sum >= cross_sum {
print("right_low is \(right_low), right_high is \(right_high), right_sum is \(right_sum)")
print("")
return (right_low, right_high, right_sum)
} else {
print("cross_low is \(cross_low), cross_high is \(cross_high), cross_sum is \(cross_sum)")
print("")
return (cross_low, cross_high, cross_sum)
}
}
//Test Code
var arrayForTest: [Int] = [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7]
var (max_left, max_right, sum) = findMaximumSubarray(arr: arrayForTest, low: 0, high: arrayForTest.count - 1)
print("max_left is \(max_left), max_right is \(max_right), sum is \(sum)")
//List num
print("max sum SubArray is \(arrayForTest[max_left...max_right])")
Terminal
运行swift FindMaxSubArray.swift
可见:
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 0, high is 15
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 0, high is 7
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 0, high is 3
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 0, high is 1
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 0, high is 0
high is equal to low, are 0
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 1, high is 1
high is equal to low, are 1
start findMaxCrossingSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 0, mid is 0,high is 1
finish findMaxCrossingSubarray. max_left is 0, max_right is 1, sum is 13
finish findMaximumSubarray.
left_low is 0, left_high is 0, left_sum is 13)
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 2, high is 3
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 2, high is 2
high is equal to low, are 2
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 3, high is 3
high is equal to low, are 3
start findMaxCrossingSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 2, mid is 2,high is 3
finish findMaxCrossingSubarray. max_left is 2, max_right is 3, sum is 20
finish findMaximumSubarray.
right_low is 3, right_high is 3, right_sum is 20
start findMaxCrossingSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 0, mid is 1,high is 3
finish findMaxCrossingSubarray. max_left is 0, max_right is 3, sum is 10
finish findMaximumSubarray.
right_low is 3, right_high is 3, right_sum is 20
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 4, high is 7
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 4, high is 5
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 4, high is 4
high is equal to low, are 4
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 5, high is 5
high is equal to low, are 5
start findMaxCrossingSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 4, mid is 4,high is 5
finish findMaxCrossingSubarray. max_left is 4, max_right is 5, sum is 0
finish findMaximumSubarray.
cross_low is 4, cross_high is 5, cross_sum is 0
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 6, high is 7
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 6, high is 6
high is equal to low, are 6
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 7, high is 7
high is equal to low, are 7
start findMaxCrossingSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 6, mid is 6,high is 7
finish findMaxCrossingSubarray. max_left is 6, max_right is 7, sum is 18
finish findMaximumSubarray.
right_low is 7, right_high is 7, right_sum is 18
start findMaxCrossingSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 4, mid is 5,high is 7
finish findMaxCrossingSubarray. max_left is 4, max_right is 7, sum is 0
finish findMaximumSubarray.
right_low is 7, right_high is 7, right_sum is 18
start findMaxCrossingSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 0, mid is 3,high is 7
finish findMaxCrossingSubarray. max_left is 3, max_right is 7, sum is 20
finish findMaximumSubarray.
left_low is 3, left_high is 3, left_sum is 20)
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 8, high is 15
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 8, high is 11
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 8, high is 9
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 8, high is 8
high is equal to low, are 8
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 9, high is 9
high is equal to low, are 9
start findMaxCrossingSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 8, mid is 8,high is 9
finish findMaxCrossingSubarray. max_left is 8, max_right is 9, sum is 20
finish findMaximumSubarray.
left_low is 8, left_high is 8, left_sum is 20)
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 10, high is 11
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 10, high is 10
high is equal to low, are 10
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 11, high is 11
high is equal to low, are 11
start findMaxCrossingSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 10, mid is 10,high is 11
finish findMaxCrossingSubarray. max_left is 10, max_right is 11, sum is 12
finish findMaximumSubarray.
left_low is 10, left_high is 10, left_sum is 12)
start findMaxCrossingSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 8, mid is 9,high is 11
finish findMaxCrossingSubarray. max_left is 8, max_right is 10, sum is 25
finish findMaximumSubarray.
cross_low is 8, cross_high is 10, cross_sum is 25
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 12, high is 15
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 12, high is 13
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 12, high is 12
high is equal to low, are 12
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 13, high is 13
high is equal to low, are 13
start findMaxCrossingSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 12, mid is 12,high is 13
finish findMaxCrossingSubarray. max_left is 12, max_right is 13, sum is 15
finish findMaximumSubarray.
right_low is 13, right_high is 13, right_sum is 15
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 14, high is 15
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 14, high is 14
high is equal to low, are 14
start findMaximumSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 15, high is 15
high is equal to low, are 15
start findMaxCrossingSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 14, mid is 14,high is 15
finish findMaxCrossingSubarray. max_left is 14, max_right is 15, sum is 7
finish findMaximumSubarray.
right_low is 15, right_high is 15, right_sum is 7
start findMaxCrossingSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 12, mid is 13,high is 15
finish findMaxCrossingSubarray. max_left is 13, max_right is 15, sum is 18
finish findMaximumSubarray.
cross_low is 13, cross_high is 15, cross_sum is 18
start findMaxCrossingSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 8, mid is 11,high is 15
finish findMaxCrossingSubarray. max_left is 8, max_right is 15, sum is 20
finish findMaximumSubarray.
left_low is 8, left_high is 10, left_sum is 25)
start findMaxCrossingSubarray: arr is [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7], low is 0, mid is 7,high is 15
finish findMaxCrossingSubarray. max_left is 7, max_right is 10, sum is 43
finish findMaximumSubarray.
cross_low is 7, cross_high is 10, cross_sum is 43
max_left is 7, max_right is 10, sum is 43
max sum SubArray is [18, 20, -7, 12]
其中比较有意思的地方,Swift 3.0
不支持了常见的方式,如果我们要逆序遍历,需要使用for index in (low...mid).reversed() {...}
,其中reversed()
就是逆序排列。
引用 Swift 2.x 源码[https://segmentfault.com/a/1190000002418778]