排序
冒泡排序
Array.prototype.bubbleSort = function () {
for (let i = 0; i < this.length - 1; i++) {
// this.length - 1 - i已经冒泡的不再处理
for (let j = 0; j < this.length - 1 - i; j++) {
console.log(this[j], this[j + 1]);
if (this[j] > this[j + 1]) {
const temp = this[j];
this[j] = this[j + 1];
this[j + 1] = temp;
}
}
}
}
const arr = [1, 3, 6, 5, 4, 2];
console.log(arr)
选择排序
Array.prototype.selectSort = function () {
for (let i = 0; i < this.length - 1; i++) {
let minIndex = i;
for (let j = i; j < this.length; j++) {
if (this[j] < this[minIndex]) {
minIndex = j
}
}
if (minIndex !== i) {
const temp = this[i];
this[i] = this[minIndex];
this[minIndex] = temp;
}
}
}
const arr = [1, 3, 6, 5, 4, 2];
arr.selectSort()
console.log(arr)
插入排序
Array.prototype.insertSort = function () {
for (let i = 0; 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 = j - 1;
}
this[j] = temp
}
}
const arr = [1, 3, 6, 5, 4, 2];
arr.insertSort()
console.log(arr)
归并排序(火狐)
思路
1.分:将数组分成两半,再递归对子数组进行“分”操作;直到分成一个个单独的数
2.合:把两个数合并为有序数组,,再对有序数组进行合并,直到全部子数组合并为一个完整数组
合并两个有序数组
1.新建一个空数组res,用于存放最终排序后的数组2.比较两个有序数组的头部,较小者出队并推入res中
3.如果两个数组还有值,就重复步骤2
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);
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;
})
}
const arr = [1, 3, 6, 5, 4, 2];
arr.mergeSort()
console.log(arr)
快速排序
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((n, i) => {
this[i] = n
})
}
const arr = [1, 3, 6, 5, 4, 2];
arr.quickSort();
console.log(arr)
搜索
顺序搜索
Array.prototype.sequentialSearch = function (item) {
for (let i = 0; i < this.length; i++) {
if (this[i] === item) {
return i
}
}
return -1;
}
const arr = [1, 3, 6, 5, 4, 2];
const index = arr.sequentialSearch(3);
console.log(index);
二分搜索(前提数组已经排序)
Array.prototype.binarySearch = function (item) {
let low = 0;
let high = this.length - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
// const mid = (low + high) >> 1;
const element = this[mid];
if (element < item) {
low = mid + 1;
} else if (element > item) {
high = mid - 1;
} else {
return mid;
}
}
return -1;
}
const arr = [1, 2, 3, 4, 5, 6];
const index = arr.binarySearch(6);
console.log(index);