前言:
今天兄弟们对“js排序原理”大约比较关心,我们都想要知道一些“js排序原理”的相关知识。那么小编同时在网摘上收集了一些有关“js排序原理””的相关知识,希望看官们能喜欢,我们快快来学习一下吧!排序算法选择排序
调用方法:sort(数组)
例:sort([30,60,40,80,10]) // 得到[10, 30, 40, 60, 80]
let min = (numbers) => { if (numbers.length > 2) { return min([numbers[0], min(numbers.slice(1))]) } else { return Math.min.apply(null, numbers) }}let minIndex = (numbers) => numbers.indexOf(min(numbers))let sort = (numbers) => { if (numbers.length > 2) { let index = minIndex(numbers) let min = numbers[index] numbers.splice(index, 1) return [min].concat(sort(numbers)) } else { return numbers[0] < numbers[1] ? numbers : numbers.reverse() }}快速排序
调用方法:quickSort(数组)
例:quickSort([30,60,40,80,10]) // 得到[10, 30, 40, 60, 80]
let quickSort = arr => { if (arr.length <= 1) { return arr } let pivotIndex = Math.floor(arr.length / 2) let pivot = arr.splice(pivotIndex, 1)[0] let left = [] let right = [] for (let i = 0; i < arr.length; i++) { if (arr[i] < pivot) { left.push(arr[i]) } else { right.push(arr[i]) } } return quickSort(left).concat( [pivot], quickSort(right) ) }归并排序
调用方法:mergeSort(数组)
例:mergeSort([30,60,40,80,10]) // 得到[10, 30, 40, 60, 80]
let mergeSort = arr => { let x = arr.length if (x === 1) { return arr } let left = arr.slice(0, Math.floor(x / 2)) let right = arr.slice(Math.floor(x / 2)) return merge(mergeSort(left), mergeSort(right))}let merge = (a, b) => { if (a.length === 0) return b if (b.length === 0) return a return a[0] < b[0] ? [a[0]].concat(merge(a.slice(1), b)) : [b[0]].concat(merge(a, b.slice(1)))}计数排序
调用方法:countSort(数组)
例:countSort([30,60,40,80,10]) // 得到[10, 30, 40, 60, 80]
let countSort = arr => { let hashTable = {}, max = 0, result = [] for (let i = 0; i < arr.length; i++) {//遍历数组} if (!(arr[i] in hashTable)) { hashTable[arr[i]] = 1 } else { hashTable[arr[i]] += 1 } if (arr[i] > max) { max = arr[i] } } for (let x = 0; x <= max; x++) {//遍历哈希表 if (x in hashTable) { for (let i = 0; i < hashTable[x]; i++) { result.push(x) } } } return result}
其他知识点
let arr = [3, 9, 8]
// splice() 得到的是一个数组
arr.splice(0, 1) //得[3] 包含中括号
// splice()[] 得到的是一个数字
arr.splice(0, 1)[0] //得3 不含中括号
console.log 版本
快速排序 console.log版本
let quickSort = arr => { if (arr.length <= 1) { return arr } let pivotIndex = Math.floor(arr.length / 2) console.log(`----`) console.log(`pivotIndex:${pivotIndex}`) let pivot = arr.splice(pivotIndex, 1)[0] console.log(`pivot:${pivot}`) let left = [] let right = [] for (let i = 0; i < arr.length; i++) { if (arr[i] < pivot) { left.push(arr[i]) } else { right.push(arr[i]) } } console.log(`left:${left}`) console.log(`right:${right}`) return quickSort(left).concat( [pivot], quickSort(right) )}
归并排序 console.log版本
let mergeSort = arr => { let x = arr.length if (x === 1) { return arr } let left = arr.slice(0, Math.floor(x / 2)) console.log(`----`) console.log(`arr:${arr}`) console.log(`left:${left}`) let right = arr.slice(Math.floor(x / 2)) console.log(`right:${right}`) return merge(mergeSort(left), mergeSort(right))}let merge = (a, b) => { if (a.length === 0) return b if (b.length === 0) return a console.log(`------`) console.log(`a:${a}`) console.log(`b:${b}`) return a[0] < b[0] ? [a[0]].concat(merge(a.slice(1), b)) : [b[0]].concat(merge(a, b.slice(1)))}
有理解错误的帮忙指出,互相学习~ 晚安~
标签: #js排序原理