Skip to content

Instantly share code, notes, and snippets.

@abdulrahmanAlotaibi
Created December 15, 2022 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdulrahmanAlotaibi/b5181a8244d5b590931f8e3239ef6638 to your computer and use it in GitHub Desktop.
Save abdulrahmanAlotaibi/b5181a8244d5b590931f8e3239ef6638 to your computer and use it in GitHub Desktop.
Arrays #4 : Improved Dutch Flag solution
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