排序、搜索
- 排序:将某个乱序的数组变成升序或降序的数组
- 排序算法包括:冒泡排序、归并排序、选择排序、 快速排序、 插入排序等
- 搜索:找出数组中某个元素的下标
- 搜索算法包括:顺序搜索、二分搜索等
JS 实现排序
一个非常有助于理解算法的动画网站
一、冒泡排序
- 比较相邻元素,如果第一个元素比第二个元素大,则交换他们
- 一轮下来,可以保证最后一个元素是最大的
- 执行 n-1 轮
- 时间复杂度:O(n^2)
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 tmp = this[j + 1];
this[j + 1] = this[j];
this[j] = tmp;
}
}
}
}
const arr = [5, 4, 3, 2, 1];
arr.bubbleSort();
二、选择排序
- 选中数组中最小值放在第一位
- 选中数组中第二小的值放在第二位
- 执行 n-1 轮
- 时间复杂度:O(n^2)
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) continue;
const tmp = this[i];
this[i] = this[indexMin];
this[indexMin] = tmp;
}
}
const arr = [5, 4, 3, 2, 1];
arr.selectionSort();
三、插入排序
- 从第二个数开始往前比
- 比它大就往后排
- 以此类推进行到最后一个数
- 时间复杂度:O(n^2)
Array.prototype.insertionSort = function () {
for(let i = 1; i < this.length; i++) {
const tmp = this[i];
let j = i;
while (j > 0) {
if (this[j - 1] > tmp) {
this[j] = this[j - 1];
} else {
break;
}
j--;
}
this[j] = tmp;
}
}
const arr = [5, 4, 3, 2, 1];
arr.insertionSort();
四、归并排序
- 分:把数组劈成两半,再对子数组进行递归“分”的操作,直到分成一个个单独的数
- 合:把两个数合并为有序数组,再对有序数组进行合并,直到全部子数组合并为一个完整的数组
- 新建空数组 res,用于存放排序结果
- 比较两个有序数组的头部,较小者出队并推入 res 中
- 如果两个数组中还有值,重复“2”步骤
- 时间复杂度:O(logn * n)
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 {
res.push(orderRight.shift())
}
}
return res;
}
const res = rec(this);
res.forEach((v, i) => {
this[i] = v;
})
}
const arr = [5, 4, 3, 2, 1];
arr.mergeSort();
五、快速排序
- 分区:从数组中任意选择一个“基准”,所有比“基准”小的元素,放在“基准”前面,比“基准”大的元素,放在“基准”后面
- 递归:递归对“基准”前后的子数组进行分区
- 时间复杂度:O(logn * 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 < arr.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((v, i) => {
this[i] = v;
})
}
const arr = [5, 4, 3, 2, 1];
arr.quickSort();
JS 实现搜索
一、顺序搜索
- 遍历数组
- 找到与目标值相等的值,就返回下标
- 没搜到就返回 -1
- 时间复杂度:O(n)
Array.prototype.sequentialSearch = function (target) {
if (target == null) return -1;
for (let i = 0; i < this.length; i++) {
if (this[i] === target) {
return i;
}
}
return -1;
};
arr = [2, 313, 44, 412, 3, 4, 6, 8];
arr.sequentialSearch(313);
二、二分搜索
- 只能作用于有序数组
- 从数组的中间值开始,如果中间值恰好等于目标值,则搜索结束
- 如果目标值大于过小于中间值,则从大于或小于中间值的那个区间再进行二分搜索
- 时间复杂度:O(logn)
Array.prototype.binarySearch = function (target) {
if (target == null) return -1;
let low = 0;
let high = this.length - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
const midItem = this[mid];
if (target < midItem) {
high = mid - 1;
} else if (target > midItem) {
low = mid + 1;
} else {
return mid;
}
}
return -1;
};
arr = [1, 2, 3, 4, 5];
arr.binarySearch(3);
leetcode 相关题目
var mergeTwoLists = function (list1, list2) {
const res = new ListNode(0);
let p = res;
let p1 = list1;
let p2 = list2;
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;
};
var guessNumber = function (n) {
let low = 0;
let 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 {
high = mid - 1;
}
}
return -1;
};