Below is my attempt to the dutch flag problem. My algorithms isn't showing the right output. How do I fix this ?
Expected output:
a = [5,2,4,4,6,4,4,3] and pivot = 4 --> result = [3,2,4,4,4,4,5,6].
My Output:
[3, 2, 4, 4, 4, 4, 4, 3]
func swapElements (_ array: inout [Int], _ a:inout Int, _ b: inout Int)->Int{
var temp = array[a]
array[a] = array[b]
temp = array[b]
return temp
}
func dutchNationalFlag(flags: inout [Int])->[Int]{
var lowerBound = 0
var highBound = flags.count-1
var i = 0
while i <= highBound{
if flags[i] < 4{
swapElements(&flags, &i, &lowerBound)
lowerBound+=1
i+=1
}
else if flags[i] > 4{
swapElements(&flags, &i, &highBound)
highBound -= 1
}
else {
i+=1
}
}
return flags
}
var nums = [5,2,4,4,6,4,4,3]
dutchNationalFlag(flags: &nums)