冒泡排序动画演示
Array.prototype.bubbleSort = function () { for(let i = 0; i < this.length - 1; i++) { for(let j = 0; j < this.length - 1 - i; j++) { if(this[j] > this[j + 1]) { const temp = this[j] this[j] = this[j + 1] this[j + 1] = temp } } } }
选择排序动画演示
Array.prototype.selectionSort = function () { for(let i = 0; i < this.length-1; i++) { let indexMin = i for(let j = i; j < this.length; j++) { if(this[j] < this[indexMin]) { indexMin = j } } if(indexMin !== i) { const temp = this[i] this[i] = this[indexMin] this[indexMin] = temp } } }
插入排序动画演示
Array.prototype.insertionSort = function () { for(let i = 1; i < this.length; i++) { const temp = this[i] let j = i while(j > 0) { if(this[j-1] > temp) { this[j] = this[j-1] } else { break } j-- } this[j] = temp } }
归并排序动画演示
Array.prototype.mergeSort = function () { const rec = arr => { if(arr.length == 1) return arr const mid = Math.floor(arr.length / 2) const left = arr.slice(0, mid) const right = arr.slice(mid, arr.length) const orderLeft = rec(left) const orderRight = rec(right) const res = [] while(orderLeft.length || orderRight.length) { if(orderLeft.length && orderRight.length) { res.push(orderLeft[0] < orderRight[0] ? orderLeft.shift() : orderRight.shift()) } else if(orderLeft.length) { res.push(orderLeft.shift()) } else if(orderRight.length) { res.push(orderRight.shift()) } } return res } const res = rec(this) res.forEach((n, i) => this[i] = n) }
快速排序动画演示
Array.prototype.quickSort = function () { const rec = (arr) => { if(arr.length == 1) return arr const left = [] const right = [] const mid = arr[0]; for(let i = 1; i < this.length; i++) { if(arr[i] < mid) { left.push(arr[i]) } else { right.push(arr[i]) } } return [...rec(left), mid, ...rec(right)] } const res = rec(this) res.forEach((n, i) => this[i] = n) }
Array.prototype.sequentialSearch = function(item) { for(let i = 0; i < this.length; i++) { if(this[i] === item) { return i } } return -1 }
前提:数组有序
Array.prototype.binarySearch = function(item) { let low = 0, high = this.length - 1 while(low <= high) { const mid = Math.floor((low + high) / 2) const element = this[mid] if(element < item) { low = mid + 1 } else if(element > item) { high = mid - 1 } else { return mid } } return -1 }
function mergeTwoLists(l1, l2) { const res = new ListNode(0); let p = res, p1 = l1, p2 = l2; while(p1 && p2) { if(p1.val < p2.val) { p.next = p1; p1 = p1.next; } else { p.next = p2; p2 = p2.next; } p = p.next; } if(p1) { p.next = p1; } if(p2) { p.next = p2 } return res.next }
输入:n = 10, pick = 6
输出:6
function guessNumber(n) { let low = 1, high = n; while(low <= high) { const mid = Math.floor((low + high) / 2); const res = guess(mid) if(res === 0) { return mid } else if(res === 1) { low = mid + 1 } else if(res === -1) { high = mid - 1 } } }