Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. // First write the swap function, which is just a helper function to swap values of the array.
  2. function swap(array, i, j) {
  3. var temp = array[i];
  4. array[i] = array[j];
  5. array[j] = temp;
  6. }
  7.  
  8. function quicksortHoare(array, left, right) {
  9. // left-pointer would be the index of the first element which is 0 and right-pointer would be the index of the last element which would be (length -1).
  10. left = left || 0;
  11. right = right || array.length - 1;
  12.  
  13. var pivot = partitionHoare(array, left, right);
  14.  
  15. if (left < pivot - 1) {
  16. quicksortHoare(array, left, pivot - 1);
  17. }
  18.  
  19. if (right > pivot) {
  20. quicksortHoare(array, pivot, right)
  21. }
  22.  
  23. return array;
  24.  
  25. }
  26.  
  27. /* Two indices that start at the ends of the array being partitioned, then move toward each other, until they detect an inversion: a pair of elements, one greater than the pivot, one smaller, that are in the wrong order relative to each other. The inverted elements are then swapped.
  28. Here the numerical values of left and right is continually getting updated with each inner while loop. But only if the while loop condition gets satisfied. That is, when the while loop condition is unsatisfied, e.g. for the first inner while loop, when array[left] > array[pivot] which means we have found a misplaced pair.
  29.  
  30. That is, although the left <= right (which is being made sure by the outer while loop) the actual elements are not sorted. Meaning a left side element is larger in value than the right side element. So, the code execution then jumps out of the inner while loop and goes right in to execute the swap function.
  31. */
  32. function partitionHoare(array, left, right) {
  33. var pivot = Math.floor((left + right) / 2);
  34.  
  35. while (left < right) {
  36. while (array[left] < array[pivot]) {
  37. left++
  38. }
  39. while (array[right] > array[pivot]) {
  40. right--
  41. }
  42.  
  43. if (left <= right) {
  44. swap(array, left, right);
  45. left++
  46. right--
  47. }
  48. }
  49. return left;
  50. }
  51.  
  52. /******************* Testing Hoare algorithm *********************/
  53.  
  54. function getRandomInt(min, max) {
  55. return Math.floor(Math.random() * (max - min + 1)) + min;
  56. // By adding 1, I am making the maximum inclusive ( the minimum is inclusive anyway). Because, the Math.random() function returns a floating-point, pseudo-random number in the range from 0 inclusive up to but not including 1
  57. }
  58.  
  59. var arr = [];
  60.  
  61. for (var i = 0; i < 10; i++) { //initialize a random integer unsorted array
  62. arr.push(getRandomInt(1, 100));
  63. }
  64.  
  65. console.log("Unsorted array: ");
  66. console.log(arr); //printing unsorted array
  67.  
  68. arr = quicksortHoare(arr, 0, arr.length - 1);
  69. console.log("Sorted array: ");
  70. console.log(arr);
  71.  
  72. /* Output:
  73. Unsorted array:
  74. [ 21, 45, 43, 11, 98, 59, 86, 81, 26, 56 ]
  75. Sorted array:
  76. [ 11, 21, 26, 43, 45, 56, 59, 81, 86, 98 ]
  77. [Finished in 0.1s]
  78. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement