Guest User

Untitled

a guest
Jan 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. //bubble, selection, quick, merge sort
  2.  
  3. const input = [5, 3, 4, 2, 7];
  4.  
  5. //bubble sort: iterate list, compare adjacent items,
  6. //swap them if they are out of order, repeat until no swaps in one iteration
  7. // O(n^2) worst case time, because each element compared with each other element
  8. // actually it is like: (n-1) comp + (n-2) comp + ... + (1) comp
  9. // sum of i=1 to i=n of (n - i)
  10. //simplifies to O(n^2) big O
  11.  
  12. const bubbleSort = arr => {
  13. //compare current index to next index
  14. // swap if next is smaller than current
  15. let i = 0;
  16. let sorted = false;
  17. while (!sorted) {
  18. sorted = true;
  19. for (i=0; i < arr.length-1; i++) {
  20. if (arr[i] > arr[i+1]) {
  21. [arr[i], arr[i+1]] = [arr[i+1], arr[i]];
  22. sorted = false;
  23. }
  24. }
  25. i = 0;
  26. }
  27.  
  28. return arr;
  29. }
  30.  
  31. console.log(bubbleSort(input));
  32.  
  33. //selection sort: find smallest element in unsorted array, place at front of new array
  34. // repeat on remaining elements of unsorted array
  35. // O(n) run time bc just iterating through n elements in the array
  36.  
  37. const input2 = [5, 3, 4, 2, 7];
  38.  
  39. const selectionSort = arr => {
  40.  
  41. const sorted = [];
  42. const len = arr.length; //cant use arr.length actively in for-loop as arr is mutated
  43.  
  44. for (let i = 0; i < len; i++) {
  45. const min = Math.min(...arr);
  46. sorted.push(min);
  47. arr.splice(arr.indexOf(min), 1);
  48. }
  49. return(sorted);
  50. }
  51.  
  52. console.log(selectionSort(input2));
  53.  
  54. // quicksort:
  55.  
  56. let input3 = [7, 4, 2, 3, 6, 5, 11, 16, 9];
  57. const left = [];
  58. const right = [];
  59.  
  60. //1 choose any element in array [p..r]
  61. // call this element 'pivot'
  62. // rearrange [p..r] with smaller on left and greater on right
  63. // actually always call rightmost element pivot
  64. // recursively sort subarray
  65. // now it is sorted
  66.  
  67. const quickSort = (arr, left, right) => {
  68. //choose pivot
  69. //put smaller on left of pivot
  70. //put smaller on right of pivot
  71. //sort array(s) on left of pivot
  72. //sort array(s) on right of pivot
  73. const pivot = arr[arr.length-1];
  74. console.log(pivot);
  75.  
  76. arr.forEach(element => {
  77. console.log(arr);
  78. console.log(element);
  79. if (element < pivot) {
  80. console.log(element);
  81. left.push(element);
  82. } else if (element > pivot) {
  83. console.log(element);
  84. right.push(element);
  85. }
  86. })
  87.  
  88. if (left.length > 1) {
  89. left = quickSort(left, [], []);
  90. }
  91. if (right.length > 1) {
  92. right = quickSort(right, [], []);
  93. }
  94. arr = [...left, pivot, ...right];
  95.  
  96. console.log(arr);
  97.  
  98. return arr;
  99. }
  100.  
  101. console.log(quickSort(input3, left, right));
  102.  
  103. //mergesort - also divide and conquer
  104. // 1 - divide problems to smaller instances of same problem (subproblems)
  105. // 2- solve subproblems recursively. when possible, solve base case.
  106. // 3- combine solutions for subproblem into solution for original problem
  107.  
  108. // divide array into two arrays about the middle
  109. // repeat until arrays are size 0 or 1
  110. // 'merge' two arrays ie [2, 4] [7, 8] --> [2, 4, 7, 8]
  111.  
  112. let input4 = [5, 3, 4, 2, 7, 9, 2, 1];
  113.  
  114. const merge = (arr1, arr2) => {
  115. const arr3 = [];
  116. const newLen = arr1.length + arr2.length
  117.  
  118. while(arr3.length < newLen) {
  119. if (arr1.length > 0 && arr2.length > 0) {
  120. console.log(arr3);
  121. console.log(arr1.length);
  122. console.log(arr2.length);
  123. arr3.push((arr1[0] < arr2[0]) ? arr1.shift() : arr2.shift());
  124. }
  125. else if (arr1.length > 0) arr3.push(arr1.shift());
  126. else if (arr2.length > 0) arr3.push(arr2.shift());
  127. }
  128. console.log(arr3);
  129. return arr3;
  130. }
  131.  
  132. const mergeSort = (arr) => {
  133. console.log(arr);
  134. const mid = Math.floor(arr.length / 2);
  135. let left = arr.slice(0, mid); //slice --> starting at, ending at (NOT inclusive)
  136. let right = arr.slice(mid, arr.length);
  137. console.log(left);
  138. console.log(right);
  139. if (left.length > 1) {
  140. left = mergeSort(left);
  141. }
  142. if (right.length > 1) {
  143. right = mergeSort(right);
  144. }
  145. console.log(left);
  146. console.log(right);
  147. arr = merge(left, right);
  148. console.log(arr);
  149. return arr;
  150. }
  151.  
  152. // let b = [3, 9];
  153. // let c = [2, 11];
  154. // merge(b, c);
  155. input4 = mergeSort(input4);
  156. //merge([3,5],[2,4]);
  157. console.log(input4);
Add Comment
Please, Sign In to add comment