Created
January 10, 2023 19:30
-
-
Save MickhailP/b33b572aad15da5de7001e5a76f335eb to your computer and use it in GitHub Desktop.
AlgorithmsQuickSort
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 quickSort(nums: [Int]) -> [Int] { | |
if nums.count < 2 { | |
return nums | |
} else { | |
let pivot = nums[0] | |
var lessPart: [Int] = [] | |
var greaterPart: [Int] = [] | |
for i in 1..<nums.count { | |
if nums[i] <= pivot { | |
lessPart.append(nums[i]) | |
} else { | |
greaterPart.append(nums[i]) | |
} | |
} | |
return quickSort(nums: lessPart) + [pivot] + quickSort(nums: greaterPart) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment