You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * Hoares quicksort, this quicksort is optimimzed for * speed and memory efficiency. * * @example https://itnext.io/a-sort-of-quick-guide-to-quicksort-and-hoares-partitioning-scheme-in-javascript-7792112c6d1 * * @param {Array} arr * @param {Number} left * @param {Array} right * * @return {Array} */functionquicksort(arr,left=0,right=arr.length-1){if(left>=right)return;// we choose the pivot to be middle element// because if the array is already sorted it// won't do extra work// make sure that the pivot will be halfway between the left// and right pointersconstpivot=arr[Math.floor((left+right)/2)];// returned index is the pivot elementconstindex=partition(arr,left,right,pivot);quicksort(arr,left,index-1);quicksort(arr,index,right);returnarr;}/** * Partition the array so that the values * bigger than the pivot are on the left, * and the values smaller than the pivot * are on the right. * * @param {Array} arr * @param {Integer} left * @param {Integer} right * @param {Integer} pivot * * @return {Integer} */functionpartition(arr,left,right,pivot){// loop unitl left pointer is not past right pointerwhile(left<=right){while(arr[left]<pivot){left++;}while(arr[right]>pivot){right--;}// check if the left pointer is still to the left// of the right pointer, then switch the elementsif(left<=right){[arr[left],arr[right]]=[arr[right],arr[left]];left++;right--;}}// because everything is swapped we don't// know where our pivot point is in the// array, and the while loop breaks when// the left pointer comes next to the pivot// meaning that this is where the left sub-array// ends and the right one beginsreturnleft;}