Last active
March 2, 2021 14:37
-
-
Save bogoreh/de659dd9fb41ec203ebfe820ef088a9c 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
| // This function partitions given array and returns | |
| // the index of the pivot. | |
| var partition=function(array, p, r){ | |
| // This code has been purposefully obfuscated, | |
| // as you will implement it yourself in next challenge | |
| var e=array,t=p,n=r;var r=function(e,t,n){var r=e[t];e[t]=e[n];e[n]=r;};var i=t;for(var s=t;s<n;s++){if(e[s]<=e[n]){r(e,s,i);i++;}}r(e,n,i);return i; | |
| }; | |
| var quickSort = function(array, p, r) { | |
| if (p < r){ | |
| var q = partition(array, p, r); | |
| quickSort(array, p, q-1); | |
| quickSort(array, q+1, r); | |
| } | |
| }; | |
| var array = [9, 7, 5, 11, 12, 2, 14, 3, 10, 6]; | |
| quickSort(array, 0, array.length-1); | |
| println("Array after sorting: " + array); | |
| Program.assertEqual(array, [2,3,5,6,7,9,10,11,12,14]); | |
| var array2 = [-1, 2, 0, 6, 10, 80]; | |
| quickSort(array2, 0, array2.length-1); | |
| println("Array after sorting: " + array2); | |
| Program.assertEqual(array2, [-1, 0, 2, 6, 10, 80]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment