Created
April 30, 2021 11:38
-
-
Save syedjafer/bd997f78f3ad7f8a2561c59cd100b6b6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| function partition(array, low, high){ | |
| var pivot = array[high]; | |
| var partition_index = low; | |
| var temp; | |
| for (var itr=low; itr<high; itr++){ | |
| if (array[itr]<pivot){ | |
| temp = array[itr]; | |
| array[itr] = array[partition_index]; | |
| array[partition_index] = temp; | |
| partition_index += 1 | |
| } | |
| } | |
| temp = array[high]; | |
| array[high] = array[partition_index]; | |
| array[partition_index] = temp; | |
| return partition_index | |
| } | |
| function qsort(array, low, high){ | |
| if (low < high){ | |
| var part_ind = partition(array, low, high); | |
| console.log(part_ind) | |
| qsort(array, low, part_ind-1); | |
| qsort(array, part_ind+1, high); | |
| } | |
| } | |
| var array = [3, 8, 6, 2, 4, 7, 8, 5]; | |
| qsort(array, 0, 7); | |
| console.log(array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment