一、说明
该文章只实现了面试中常考的几种排序算法,目前本人知道的有10种经典排序,除了本文章实现的排序算法,其余经典排序算法,有兴趣的可以自己去实现一下。
二、算法效率
大O表示法:
1、推导大O表示法的方式:
(1)用常量1取代运行时间中的所有加法常量
(2)在修改后的运行次数函数中,只保留最高阶项
(3)如果最高存在且不为1,则去除与这个项相乘的常数
三、具体排序算法
(一)冒泡排序
1、代码实现
// 冒泡排序
function bubbleSort(array) {
// 1、获取数组的长度
let length = array.length;
for(let j=length-1; j>=0; j--) {
//第一次进来: i=0, 比较0和1的位置
//最后一次进来: i=length-2, 比较length-2和length-1的位置
for(let i=0; i<j; i++) {
if(array[i] > array[i+1]) {
let temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
}
}
}
return array;
}
console.log(bubbleSort([1,8,11,90,2,7,8]));//[1,2,7,8,8,11,90]
2、复杂度
冒泡排序的比较次数O(n^2),交换次数O(n^2),总体效率是O(n^2)
(二)选择排序
1、代码实现
function selectionSort(array) {
// 1、获取数组长度
let length = array.length;
// 2、外层循环:从0位置开始取数据
for(let j=0; j<length-1; j++) {
let min = j;
// 3、内层循环: 从min+1位置开始,和后面的数据进行比较
for(let i=min+1; i<length; i++) {
if(array[min] >= array[i]) {
min = i;
}
}
let temp = array[j];
array[j] = array[min];
array[min] = temp;
}
console.log(array);
}
selectionSort([91,8,11,90,2,7,8])//[ 2, 7, 8, 8,11, 90, 91]
2、复杂度
选择排序的比较次数O(n^2),交换次数O(n),总体效率是O(n^2)
(三)插入排序
1、代码实现
function insertionSort(array) {
// 1、获取数组的长度
let length = array.length;
// 2、外层循环: 从第一个位置开始,向前面局部有序插入数据
for(let i=1; i<length; i++) {
// 3、内层循环: 获取第i个位置的数据,和前面的数据依次进行比较
let temp = array[i];
let j = i;
while(array[j-1] > temp && j > 0) {
array[j] = array[j-1];
j--;
}
// 4、将j位置的数据,放置temp即可
array[j] = temp;
}
console.log(array);
}
insertionSort([91,8,1,90,2,7,8]);//[1, 2, 7, 8,8, 90, 91]
2、复杂度
选择排序的比较次数N*(N-1)/4,复制次数N*(N-1)/4,总体效率是O(n^2)
(四)希尔排序
1、代码实现
function shellSort(array) {
// 1、获取数组长度
let length = array.length;
// 2、初始化的增量(gap -> 间隔/间隙)
let gap = Math.floor(length / 2);
// 3、while循环,gap不断减少
while(gap >= 1) {
// 4、以gap作为间隙,进行分组,对分组进行插入排序
for(let i=gap; i<length; i++) {
let temp = array[i];
let j = i;
while((array[j-gap] > temp) && (j > gap - 1)) {
array[j] = array[j-gap];
j-=gap;
}
// 5、将j位置的元素赋值为temp
array[j] = temp;
}
// 6、gap不断缩小
gap = Math.floor(gap / 2);
}
console.log(array);
}
shellSort([91,8,1,90,2,7,8]);//[1, 2, 7, 8, 8, 90, 91]
2、复杂度
复杂度小于O(n^2)
(五)快速排序
1、代码实现
function swap(m, n, array) {
let temp = array[m];
array[m] = array[n];
array[n] = temp;
}
// 一、枢纽的选择
function median(left, right, array) {
// 1、取出中间位置
let center = Math.floor((left + right) / 2);
if(array[left] > array[right]) {
swap(left, right, array)
}
// 2、判断大小,并进行交换
if(array[left] > array[center]) {
swap(left, center, array);
}
if(array[center] > array[right]) {
swap(center, right, array);
}
// 3、将center换到right-1的位置上
swap(center, right-1, array);
return array[right-1];
}
// 二、快速排序的实现
function quickSort(array) {
quick(0, array.length-1, array);
return array;
}
// 因为要传很多参数,所以另起一个函数
function quick(left, right, array) {
if(left >= right) return;
let pivot = median(left, right, array);
let i = left;
let j = right-1;
while(true) {
while(array[++i] < pivot) {}
while(array[--j] > pivot) {}
if(i < j) {
swap(i, j, array);
}else {
break;
}
}
swap(i, right-1, array);
quick(left, i-1, array);
quick(i+1, right, array);
}
console.log(quickSort([91,8,1,90,2,7,8]));
2、复杂度
复杂度是O(n * logn)
(六)堆排序
1、代码实现
function swap(tree, m, n) {
let temp = tree[m];
tree[m] = tree[n];
tree[n] = temp;
}
// 1、将每一层进行堆化
// (函数headify只考虑到局部(例如,左子树堆化了。右子树可能没堆化),所以整棵树堆化需要重新调用 buildTree函数)
function headify(tree, n, i) {
if(i >= n) {
return;
}
let c1 = Math.floor(2*i+1);
let c2 = Math.floor(2*i+2);
let max = i;
// 如果有赋值,要用赋值的结果,否则会出错
if(c1 < n && tree[c1] > tree[max]) {
max = c1;
}
if(c2 < n && tree[c2] > tree[max]) {
max = c2;
}
if(max !== i) {
swap(tree, max, i);
headify(tree, n, max);
}
}
// 2、建立一棵完全堆化成功的树(即若数字的顺序很乱,怎么把整棵树都 heapify,需要调用buildTree)
function buildTree(tree, n) {
let lastNode = n-1;//最后一个节点的index值
let parent = Math.floor((lastNode - 1) / 2);
for(let i=parent; i>=0; i--) {
headify(tree, n, i);
}
}
// 3、堆排序
function heapSort(tree, n) {
buildTree(tree, n);
let i;
for(i=n-1; i>=0; i--) {
swap(tree, i, 0);
headify(tree, i, 0);
}
}
// 4、调用堆排序函数
let tree = [91,8,1,90,2,7,8];
let size = tree.length;
heapSort(tree, size);
console.log(tree);//[1, 2, 7, 8, 8, 90, 91]
2、复杂度
复杂度是O(n * logn)
(七)归并排序
1、思路地址
https://www.bilibili.com/video/BV1Pt4y197VZ?from=search&seid=8783899578846664847
2、实现代码
// 归并排序
// 2、合并
function merge(arr, tempArr, left, mid, right) {
// 标记左半区第一个未排序的元素
let left_pos = left;
// 标记右半区第一个未排序的元素
let right_pos = mid + 1;
// 临时数组的下标
let pos = left;
// 合并
while(left_pos <= mid && right_pos <= right) {
if(arr[left_pos] < arr[right_pos]) {
tempArr[pos++] = arr[left_pos++];
}else {
tempArr[pos++] = arr[right_pos++];
}
}
// 合并左半区剩余的元素
while(left_pos <= mid) {
tempArr[pos++] = arr[left_pos++];
}
// 合并右半区剩余的元素
while(right_pos <= right) {
tempArr[pos++] = arr[right_pos++];
}
// 把临时数组中合并后的元素复制到原来的数组
while(left <= right) {
arr[left] = tempArr[left];
left++;
}
}
// 1、划分
function mergeSort(arr, tempArr, left, right) {
// 如果只有一个元素,那么不需要继续划分
// 只有一个元素的区域,本身就是有序的,只需要被归并就可以
if(left < right) {
// 找中间点
let mid = Math.floor((left + right) / 2);
// 递归划分左半区
mergeSort(arr, tempArr, left, mid);
// 递归划分右半区
mergeSort(arr, tempArr, mid + 1, right);
// 调用合并函数
merge(arr, tempArr, left, mid, right);
}
}
let arr = [1,8,11,90,2,7,8];
let n = arr.length;
let tempArr = [];
mergeSort(arr, tempArr, 0, n - 1);
console.log(arr);
3、复杂度
复杂度是O(n * logn)