Blogger Information
Blog 77
fans 0
comment 2
visits 55716
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
递归---选择排序--快速排序--归并排序--)哈希表----计数排序)
南瓜又个梦
Original
586 people have browsed it

1,递归
什么时候停止递归
最基本的递归步骤是什么
特点:不停调用自己,每次调用的参数会略有不同
当满足某个条件时就简单调用

所有递归都可以改成循环

2.快速排序
指定某一个数,小的去前面,打的去后面
这是一个数组排序

  1. let quickSort=arr=>{
  2. if (arr.length<=1){return arr;}
  3. let pivotIndex=Math.floor(arr.length/2);
  4. let pivot=arr.splice(pivotIndex,1)[0];
  5. let left=[];
  6. let right=[];
  7. for( i=0;i<arr.length;i++){
  8. if(arr[i]<pivot){
  9. left.push(arr[i])
  10. }
  11. else{
  12. right.push(arr[i])
  13. }
  14. }
  15. return quickSort(left).concat([pivot],quickSort(right));
  16. }
  1. 归并排序
  1. // 归并排序
  2. let mergeSort = arr =>{
  3. let k = arr.length
  4. if(k===1){return arr}
  5. let left = arr.slice(0, Math.floor(k/2))
  6. let right = arr.slice(Math.floor(k/2))
  7. return merge(mergeSort(left), mergeSort(right))
  8. }
  9. let merge = (a, b) => {
  10. if(a.length === 0) return b
  11. if(b.length === 0) return a
  12. return a[0] > b[0] ?
  13. [b[0]].concat(merge(a, b.slice(1))) :
  14. [a[0]].concat(merge(a.slice(1), b))
  15. }

1

  • 析构赋值

    1. let minOf2 = ([a, b]) => a < b ? a : b
  • 一些大小API

    1. Math.min.call(null,6,97);
    2. Math.min.apply(null,[8,22])
  • 以下是四个排序的基本模型

  1. let sort = (numbers) => {
  2. if(numbers.length > 2){
  3. let index = minIndex(numbers)
  4. let min = numbers[index]
  5. numbers.splice(index, 1)
  6. return [min].concat(sort(numbers))
  7. }else{
  8. return numbers[0]<numbers[1] ? numbers :
  9. numbers.reverse()
  10. }
  11. }
  12. let minIndex = (numbers) =>
  13. let index = 0
  14. for(let i=1; i<numbers.length; i++){
  15. if(numbers[i] < numbers[index]){
  16. index = i
  17. }
  18. }
  19. return index
  20. }
  21. let sort = (numbers) => {
  22. for(let i=0; i<???; i++){
  23. let index = minIndex(numbers)
  24. // index 是当前最小数的下标
  25. // index 对应的数应该放到 i 处
  26. swap(numbers, index, i) // swap 还没实现
  27. // index、i 都是 index 的意思,建议 i 改名
  28. }
  29. }
  30. let swap = (array, i, j) => {
  31. let temp = array[i]
  32. array[i] = array[j]
  33. array[j] = temp
  34. }
  35. swap(numbers, 1, 2)
  36. // 选择排序最终代码
  37. let sort = (numbers) => {
  38. for(let i=0; i< numbers.length -1; i++){
  39. console.log(`----`) //这个log很精髓
  40. console.log(`i: ${i}`)
  41. let index = minIndex(numbers.slice(i))+ i
  42. console.log(`index: ${index}`)
  43. console.log(`min: ${numbers[index]}`)
  44. if(index!==i){
  45. swap(numbers, index, i)
  46. console.log(`swap ${index}: ${i}`)
  47. console.log(numbers)
  48. }
  49. }
  50. return numbers
  51. }
  52. let swap = (array, i, j) => {
  53. let temp = array[i]
  54. array[i] = array[j]
  55. array[j] = temp
  56. }
  57. let minIndex = (numbers) => {
  58. let index = 0
  59. for(let i=1; i<numbers.length; i++){
  60. if(numbers[i] < numbers[index]){
  61. index = i
  62. }
  63. }
  64. return index
  65. }
  66. // 快速排序
  67. let quickSort = arr => {
  68.   if (arr.length <= 1) { return arr; }
  69.   let pivotIndex = Math.floor(arr.length / 2);
  70.   let pivot = arr.splice(pivotIndex, 1)[0];
  71.   let left = [];
  72.   let right = [];
  73.   for (let i = 0; i < arr.length; i++){
  74.     if (arr[i] < pivot) { left.push(arr[i])
  75.     } else { right.push(arr[i]) }
  76.   }
  77.   return quickSort(left).concat(
  78. [pivot], quickSort(right) )
  79. }
  80. // 归并排序
  81. let mergeSort = arr =>{
  82. let k = arr.length
  83. if(k===1){return arr}
  84. let left = arr.slice(0, Math.floor(k/2))
  85. let right = arr.slice(Math.floor(k/2))
  86. return merge(mergeSort(left), mergeSort(right))
  87. }
  88. let merge = (a, b) => {
  89. if(a.length === 0) return b
  90. if(b.length === 0) return a
  91. return a[0] > b[0] ?
  92. [b[0]].concat(merge(a, b.slice(1))) :
  93. [a[0]].concat(merge(a.slice(1), b))
  94. }
  95. // 计数排序
  96. let countSort = arr =>{
  97. let hashTable = {}, max = 0, result = []
  98. for(let i=0; i<arr.length; i++){ // 遍历数组
  99. if(!(arr[i] in hashTable)){ // 视频中这一行写错
  100. hashTable[arr[i]] = 1
  101. }else{
  102. hashTable[arr[i]] += 1
  103. }
  104. if(arr[i] > max) {max = arr[i]}
  105. }
  106. for(let j=0; j<=max; j++){ // 遍历哈希表
  107. if(j in hashTable){
  108. for(let i = 0; i<hashTable[j]; i++){
  109. result.push(j)
  110. }
  111. }
  112. }
  113. return result
  114. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post