Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. function qSort(arr){
  2. if(arr.length < 2){
  3. console.log("here is the count " + count);
  4. return list;
  5. }
  6. let left = [];
  7. let right = [];
  8. let pivot = arr[0];
  9. for(let i = 0; i < arr.length; i++){
  10. if(arr[i] < pivot){
  11. left.push(arr[i]);
  12. } else{
  13. right.push(arr[i]);
  14. }
  15. }
  16. return [...quicksort(left), pivot, ...quicksort(right)];
  17. }
  18.  
  19. function mSort(arr){
  20. if(arr.length === 1){
  21. return arr;
  22. }
  23.  
  24. const middle = Math.floor(arr.length/2);
  25. const left = arr.slice(0, middle) // items on the left side
  26. const right = arr.slice(middle)
  27.  
  28. return merge(
  29. mergeSort(left),
  30. mergeSort(right)
  31. )
  32. }
  33.  
  34. function merge(left, right){
  35. let result = [];
  36. let indexLeft = 0;
  37. let indexRight = 0;
  38. while(indexLeft < left.length && indexRight < right.length){
  39. if(left[indexLeft] < right[indexRight]){
  40. result.push(left[indexLeft]);
  41. indexLeft++;
  42. } else {
  43. result.push(right[indexRight]);
  44. indexRight++;
  45. }
  46. }
  47. return result.concat(left.slice(indexLeft)).concat(right.slice(indexRight))
  48. }
  49.  
  50. //bucket sort
  51. const data = [1,8,5,2,9];
  52. function insertionSort(array) {
  53. var length = array.length;
  54. for(var i = 1; i < length; i++) {
  55. var temp = array[i];
  56. for(var j = i - 1; j >= 0 && array[j] > temp; j--) {
  57. array[j+1] = array[j];
  58. }
  59. array[j+1] = temp;
  60. }
  61.  
  62. return array;
  63. }
  64.  
  65. // Implement bucket sort
  66. function bucketSort(array, bucketSize = 5, minValue, maxValue) {
  67. if (array.length === 0) {
  68. return array;
  69. }
  70.  
  71. // Declaring vars
  72. let i;
  73.  
  74. // Setting min and max values
  75.  
  76. // Initializing buckets
  77. var bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1;
  78. var allBuckets = new Array(bucketCount);
  79.  
  80. for (i = 0; i < allBuckets.length; i++) {
  81. allBuckets[i] = [];
  82. }
  83.  
  84. // Pushing values to buckets
  85. array.forEach(function (currentVal) {
  86. allBuckets[Math.floor((currentVal - minValue) / bucketSize)].push(currentVal);
  87. });
  88.  
  89. // Sorting buckets
  90. array.length = 0;
  91.  
  92. allBuckets.forEach(function(bucket) {
  93. insertionSort(bucket);
  94. bucket.forEach(function (element) {
  95. array.push(element)
  96. });
  97. });
  98. return array;
  99. }
  100.  
  101.  
  102. function shuffle(arr, times, count = 0){
  103. if(count === times){
  104. return arr;
  105. }
  106. let random1 = Math.floor(Math.random() * times);
  107. let random2 = Math.floor(Math.random() * times);
  108. let temp = arr[random1];
  109. arr[random1] = arr[random2];
  110. arr[random2] = temp;
  111. count+=1;
  112. return shuffle(arr, times, count)
  113. }
  114. // console.log(shuffle([1,2,3,4,5,6], 6));
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121. //////////////////////////////////////
  122. let alphaMap = {'a': 1, "b":2, "c":3, "d":4, "e":5, "f": 6, "g":7, "h":8, "i":9,
  123. "j":10, "k": 11, "l": 12, "m": 13, "n": 14, "o": 15, "p": 16, "q": 17, "r": 18, "s": 19, "t": 20,
  124. "u":21, "v":22,"w":23,"x":24,"y":25, "z": 26}
  125. console.log('alpha map c', alphaMap['c']);
  126. function bookSort(arr, alphaMap){
  127. let results = [];
  128. for(let book of arr){
  129. let numStr = '';
  130. let itemMap = {};
  131. for(let i = 0; i < book.length-1; i++){
  132. numStr+= alphaMap[book[i]] + '.';
  133. }
  134. itemMap.name = book;
  135. itemMap.sum = numStr;
  136. itemMap.sum = itemMap.sum.substring(0, numStr.length-1)
  137. itemMap.sum = parseInt(itemMap.sum);
  138. results.push(itemMap);
  139. }
  140. return qSortSum(results);
  141. }
  142.  
  143. function findLongestLength(arr){
  144. let maxCount = 0;
  145. for(let item of arr){
  146. let count = 0;
  147. for(let i = 0; i < item.sum.length; i++){
  148. count++;
  149. }
  150. if(count > maxCount){
  151. maxCount = count;
  152. }
  153. }
  154. return maxCount;
  155. }
  156.  
  157.  
  158. function qSortSum(arr, count = 0){
  159. if(arr.length < 2){
  160. return arr;
  161. }
  162. let left = [];
  163. let right = [];
  164. let pivot = arr[0];
  165. for(let i = 0; i < arr.length; i++){
  166. if(arr[i].sum < pivot.sum){
  167. left.push(arr[i]);
  168. } else if(arr[i].sum > pivot.sum){
  169. right.push(arr[i]);
  170. }
  171. }
  172. return [...qSortSum(left), pivot, ...qSortSum(right)];
  173. }
  174.  
  175. console.log(bookSort(["catcher", "wizard", "nun", "soldier", "hobbit"], alphaMap));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement