Created
June 24, 2020 04:32
-
-
Save educartoons/ccc2fa98f9fcb59d4a0df56e913df7b7 to your computer and use it in GitHub Desktop.
Implementation of Quick Sort in Typescript
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
// We will call quicksort with p: 0 and r: Array.length - 1 | |
// console.log(quickSort(Array, 0, B.length - 1)); | |
function quickSort(A: number[], p: number, r: number): number[] { | |
if (p < r) { | |
const q = partition(A, p, r); | |
quickSort(A, p, q - 1); | |
quickSort(A, q + 1, r); | |
} | |
return A; | |
} | |
function partition(A: number[], p: number, r: number): number { | |
const x = A[r]; | |
let i = p - 1; | |
for (let j = p; j <= r - 1; j++) { | |
if (A[j] <= x) { | |
i = i + 1; | |
[A[i], A[j]] = [A[j], A[i]]; | |
} | |
} | |
[A[i + 1], A[r]] = [A[r], A[i + 1]]; | |
return i + 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment