Skip to content

Instantly share code, notes, and snippets.

@abdulrahmanAlotaibi
Created December 15, 2022 23:19
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/ad0ae5b1393723d569b1a4035c5ed3c9 to your computer and use it in GitHub Desktop.
Save abdulrahmanAlotaibi/ad0ae5b1393723d569b1a4035c5ed3c9 to your computer and use it in GitHub Desktop.
Arrays #3 : The Dutch National Flag Problem (V1)
func dutchFlagPartition(arr []int, pivotIndex int) []int{
pivot := arr[pivotIndex]
for i:= 0 ; i < len(arr) ; i++ {
for j:= i + 1 ; j < len(arr) ; j++ {
if arr[j] < pivot {
arr[i], arr[j] = arr[j], arr[i]
break;
}
}
}
// Second pass
for i:= len(arr) - 1 ; i >= 0 ; i-- {
for j:= i - 1 ; j >= 0 ; j-- {
if arr[j] > pivot {
arr[i], arr[j] = arr[j], arr[i]
break;
}
}
}
return arr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment