Created
December 15, 2022 23:19
-
-
Save abdulrahmanAlotaibi/ad0ae5b1393723d569b1a4035c5ed3c9 to your computer and use it in GitHub Desktop.
Arrays #3 : The Dutch National Flag Problem (V1)
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 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