Created
December 15, 2022 23:21
-
-
Save abdulrahmanAlotaibi/b5181a8244d5b590931f8e3239ef6638 to your computer and use it in GitHub Desktop.
Arrays #4 : Improved Dutch Flag solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func improvedDutchFlag(arr []int, pivotIndex int)[]int{ | |
pivot := arr[pivotIndex] | |
smaller := 0; | |
for i:=0; i < len(arr) ; i++ { | |
if arr[i] < pivot { | |
arr[i], arr[smaller] = arr[smaller] , arr[i] | |
smaller++ | |
} | |
} | |
larger := len(arr) - 1 | |
for i:= len(arr) - 1 ; i >= 0 ; i-- { | |
if arr[i] > pivot { | |
arr[i], arr[larger] = arr[larger] , arr[i] | |
larger-- | |
} | |
} | |
return arr | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment