排序
冒泡排序
冒泡排序是一种简单的排序算法, 它重复的走访过要排序的树立, 一次比较两个元素, 如果他们的顺序错误就把他们交换过来
运算
- 比较相邻的元素, 如果第一个比第二个大, 就交换他们两个
- 对每一对相邻元素做同样的工作, 从开始第一对到结尾的最后一对, 这步做完后, 最后的元素会是最大的数
- 针对所有的元素重复以上的步骤, 除了最后一个
持续每次对越来袁绍的元素重复上面的步骤, 直到没有任何一对数字需要比较 示意图 ☞

Array.prototype.bubble_sort = function() {let i, j, temp;for(i = 0; i < this.length - 1; i ++) {for(j = 0; j < this.length - 1 - i; j ++) {if (this[j] > this[j + 1]) {temp = this[j];this[j] = this[j + 1];this[j + 1] = temp;}}}return this;}
插入排序
通过构建有序薛烈, 对于末排序数据, 在已排序序列中从后向前扫描, 找到相应位置并插入, 插入排序在实现上, 通常采用 in-place 排序(即只需用到 O(1)的额外空间的排序), 因而在从后向前扫描过程中, 需要反复把已排序元素逐步向后挪位, 为最新元素提供插入空间
示意图 ☞** **
Array.prototype.insertion_sort = function() {let i, j;for (i = 1; i < this.length; i ++) {for (j = 0; j < i; j ++) {if (this[j] > this[i]) {this.splice(j, 0, this[i]);this.splice(i + 1, 1);break;}}}return this;}// 用法[3,5,2,11,1,2,"abc","zfd","sad","eng"].insertion_sort();
选择排序
首先在末排序蓄力中找到最小(大) 元素, 存放在排序序列的起始位置, 然后, 再从剩余末排序元素中继续寻找最小(大)元素, 然后放在已排序序列的末尾, 以此来推, 直到所有元素均排序完毕
示意图 ☞ 
Array.prototype.selection_sort = function() {let i, j, min;for(i = 0; i < this.length - 1; i ++) {min = i;for(j = i + 1; j < this.length; j ++) {if (this[min] > this[j]) {min = j;}}temp = this[min];this[min] = this[i];this[i] = temp;}return this;}
希尔排序
希尔排序, 也称递减增量排序算法, 是插入排序的一种更搞笑的改进版本, 希尔排序是非稳定排序算法, 由两点性质提出改进方法
- 插入排序在对几乎已经排好序的数据操作时, 效率高, 即可以达到线性排序的效率
- 但插入排序一般来说是低效的, 因为插入排序每次只能移动数据一位
示意图 ☞ 
Array.prototype.shell_sort = function() {let gap, i, j;let temp;for (gap = this.length >> 1; gap > 0; gap >>= 1) {for (i = gap; i < this.length; i ++) {temp = this[i];for (j = i -gap; j >= 0 && this[j] > temp; j -= gap) {this[j + gap] = this[j];this[j + gap] = temp;}}}return this;}
归并排序
创建在归并操作上的一种有效的排序算法
示意图 ☞ 
function merge(left, right) {let result = [];while(left.length > 0 && right.length > 0) {if (left[0] < right[0]) {result.push(left.shift());} else {result.push(right.shift());}}return result.concat(left, right);}function mergeSort(arr) {if (arr.length <= 1) return arr;const middle = Math.floor(arr.length / 2);const left arr.slice(0, middle);const right = arr.slice(middle);return merge(mergeSort(left), mergeSort(right));}
快速排序
Quicksort 又称划分交换排序(partition-exchange sort)简称快排, 分成两块分别进行排序, 最后合并至一个数组
示意图 ☞ 
Array.prototype.quickSort = function() {const l = this.length;if (l < 2) return this;const basic = this[0];const left = [];const right = [];for (let i = 1; i < l; i ++) {const iv = this[i];iv >= basic && right.push(iv);iv < basic && left.push(iv);}return left.quickSort().concat(basic, right.quickSort())}
堆排序
指利用堆这种数据结构所设计的一种排序算法, 堆是一个近似完全二叉树的结构
示意图 ☞ 
Array.prototype.heap_sort = function() {const arr = this.slice(0);function swap(i, j) {let tmp = arr[i];arr[i] = arr[j];arr[j] = tmp;}function max_heapify(start, end) {let dad = start;const son = dad * 2 + 1;if (son >= end) {return;}if (son + 1 < end && arr[son] < arr[son + 1]) {son ++}if (arr[dad] <= arrp[son]) {swap(dad, son);max_heapify(son, end);}}const len = arr.length;for (let i = Math.floor(len / 2) - 1; i >= 0; i --) {max_heapify(i, len);}for (let i = len - 1; i > 0; i --) {swap(0, i);max_heapify(0, i);}return arr;}
参考
Javascript-algorithms
可视化算法
算法可视化
动画学习算法
Python 算法
炫酷的排序演示
可视化数据结构
分享/查看/创建的学习平台
旧金山大学数据结构和算法的可视化学习工具
**
