# 查找算法

• 顺序查找
• 二分查找

## 顺序查找

``````function seqSearch(item, array) {
let flag = false,
i = 0;
while (i < array.length && !flag) {
if (array[i] === item) {
flag = true;
}
i++;
}
return flag;
}
console.log(seqSarch(7, [2, 6, 5, 7, 4, 9, 3])); // true

## 二分查找

``````function binarySearch(item, array) {
let first = 0,
last = array.length - 1,
flag = false;
while (first <= last && !flag) {
let mid = Math.floor((first + last) / 2);
if (array[mid] === item) {
flag = true;
} else if (array[mid] < item) {
first = mid + 1;
} else if (array[mid] > item) {
last = mid - 1;
}
}
return flag;
}
console.log(binarySearch(6, [1, 2, 3, 4, 5, 6, 7, 8, 9])); // true

# 排序算法

• 冒泡排序
• 选择排序
• 插入排序
• 希尔排序
• 归并排序
• 快速排序

## 冒泡排序

``````function bubbleSort(array) {
for (let i = 0; i < array.length - 1; i++) {
for (let j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
let temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
return array;
}
console.log(bubbleSort([8, 9, 4, 1, 3, 2, 7, 5])); // [1, 2, 3, 4, 5, 7, 8, 9]

## 选择排序

``````function selectSort(array) {
for (let i = 0; i < array.length - 1; i++) {
let min = i;
for (let j = i + 1; j < array.length; j++) {
if (array[j] < array[min]) {
min = j;
}
}
let temp = array[min];
array[min] = array[i];
array[i] = temp;
}
return array;
}
console.log(selectSort([9, 8, 7, 6, 5, 4, 3, 2, 1])); // [1, 2, 3, 4, 5, 6, 7, 8, 9]

## 插入排序

``````function insertSort(array) {
for (let i = 1; i < array.length; i++) {
let item = array[i],
index = i;
while (index > 0 && array[index - 1] > item) {
array[index] = array[index - 1];
index--;
}
array[index] = item;
}
return array;
}
console.log(insertSort([8, 9, 4, 1, 3, 2, 7, 5])); // [1, 2, 3, 4, 5, 7, 8, 9]

## 希尔排序

``````function shellSort(array) {
let interval = Math.floor(array.length / 2);
while (interval > 0) {
for (let start = 0; start < interval; start++) {
gapInsertSort(array, start, interval);
start = Math.floor(start / 2);
}
}
}
function gapInsertSort(array, start, gap) {
for (let i = start + gap; i < array.length; i += gap) {
let item = array[i],
index = i;
while (index > 0 && array[index - gap] > item) {
array[index] = array[index - gap];
index -= gap;
}
array[index] = item;
}
}
console.log(shellSort([9, 8, 7, 6, 5, 4, 3, 2, 1])); // [1, 2, 3, 4, 5, 6, 7, 8, 9]

## 归并排序

``````function mergeSort(array) {
if (array.length <= 1) {
return array
}
let mid = Math.floor(array.length / 2),
left = mergeSort(array.slice(0, mid)),
right = mergeSort(array.slice(mid, array.length)),
sortArry = [];
while (left.length !== 0 && right.length !== 0) {
if (left[0] < right[0]) {
sortArry.push(left.shift());
} else {
sortArry.push(right.shift());
}
}
if (left.length !== 0) {
sortArry = sortArry.concat(left);
} else if (right.length !== 0) {
sortArry = sortArry.concat(right);
}
return sortArry;
}
console.log(mergeSort([8, 9, 4, 1, 3, 2, 7, 5])); // [1, 2, 3, 4, 5, 7, 8, 9]

## 快速排序

• 1.碰到比中值大的值
• 2.移动到右标的右侧

• 1.碰到比中值小的值
• 2.移动到左标的左侧

``````let arr = [9, 8, 7, 6, 5, 4, 3, 2, 1]
function quickSort(array) {
quickSortRecursion(array, 0, array.length - 1)
}
function quickSortRecursion(array, first, last) {
if (first < last) {
const splitPoint = division(array, first, last)
quickSortRecursion(array, first, splitPoint - 1)
quickSortRecursion(array, splitPoint + 1, last)
}
}
function division(array, first, last) {
let midValue = array[first],
leftMark = first + 1,
rightMark = last,
flag = false
while (!flag) {
while (
leftMark <= rightMark &&
array[leftMark] <= midValue
) {
leftMark++
}
while (
rightMark >= leftMark &&
array[rightMark] >= midValue
) {
rightMark--
}
if (rightMark < leftMark) {
flag = true
} else {
let temp = array[leftMark]
array[leftMark] = array[rightMark]
array[rightMark] = temp
}
}

let temp = array[first]
array[first] = array[rightMark]
array[rightMark] = temp

return rightMark
}
quickSort(arr)
console.log(arr) // [1, 2, 3, 4, 5, 6, 7, 8, 9]