# JavaScript排序算法

## 冒泡排序

function bubbleSort(arr) {
var len = arr.length;
for (var i = 0; i < len-1; i++) {
for (var j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j+1]) {        //相邻元素两两对比
var temp = arr[j+1];        //元素交换
arr[j+1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}


## 选择排序

function selectionSort(arr) {
var len = arr.length;
var minIndex, temp;
for (var i = 0; i < len - 1; i++) {
minIndex = i;
for (var j = i + 1; j < len; j++) {
if (arr[j] < arr[minIndex]) {     //寻找最小的数
minIndex = j;                 //将最小数的索引保存
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
return arr;
}


## 插入排序

function insertionSort(arr){
var len=arr.length;
for(var i=1;i<len;i++){
var j=i-1;
var current=arr[i];
while(arr[j]>current){
arr[j+1]=arr[j];
j--;
}
arr[j+1]=current;
}
return arr;
}


## 希尔排序

function shellSort(arr) {
var len = arr.length,
temp,
gap = 1;
while (gap < len / 3) {          // 动态定义间隔序列
gap = gap * 3 + 1;
}
for (gap; gap > 0; gap = Math.floor(gap / 3)) {
for (var i = gap; i < len; i++) {
temp = arr[i];
for (var j = i-gap; j > 0 && arr[j]> temp; j-=gap) {
arr[j + gap] = arr[j];
}
arr[j + gap] = temp;
}
}
return arr;
}


## 归并排序

function mergeSort(arr) {  //采用自上而下的递归方法
var len = arr.length;
if(len < 2) {
return arr;
}
var middle = Math.floor(len / 2),
left = arr.slice(0, middle),
right = arr.slice(middle);
return merge(mergeSort(left), mergeSort(right));
}

function merge(left, right)
{
var result = [];

while (left.length>0 && right.length>0) {
if (left[0] <= right[0]) {
result.push(left.shift());
} else {
result.push(right.shift());
}
}

while (left.length)
result.push(left.shift());

while (right.length)
result.push(right.shift());

return result;
}


## 快速排序

    function swap(arr,a,b){
var temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
function partition(arr,left,right){
var i=left;
var pivot=arr[i];
var j=right;
while(i<j){
while(arr[j]>=pivot)
j--;
if(i<j){
swap(arr,i++,j);//找到比基准小的就交换
}//从右往左
while(arr[i]<pivot)
i++;
if(i<j){
swap(arr,i,j--);
}//从左往右
}
return i;
}
function quickSort2(arr,left,right){
if(left>right)
return;
var p_index=partition(arr,left,right);
quickSort2(arr,left,p_index-1);
quickSort2(arr,p_index+1,right);
}


function quickSort(arr){

if(arr.length==0){
return [];
}
var left=[];
var right=[];
var p=arr[0];
for(var i=1; i<arr.length; i++){
if(arr[i]<p){
left.push(arr[i]);
}else{
right.push(arr[i]);
}
}
return quickSort(left).concat(p,quickSort(right));
}


## 堆排序

var len;

function buildMaxHeap(arr) {   // 建立大顶堆
len = arr.length;
for (var i = Math.floor(len/2); i >= 0; i--) {
heapify(arr, i);
}
}

function heapify(arr, i) {     // 堆调整
var left = 2 * i + 1,
right = 2 * i + 2,
largest = i;

if (left < len && arr[left] > arr[largest]) {
largest = left;
}

if (right < len && arr[right] > arr[largest]) {
largest = right;
}

if (largest != i) {
swap(arr, i, largest);
heapify(arr, largest);
}
}

function swap(arr, i, j) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

function heapSort(arr) {
buildMaxHeap(arr);

for (var i = arr.length - 1; i > 0; i--) {
swap(arr, 0, i);
len--;
heapify(arr, 0);
}
return arr;
}

11-09 1039
06-11 518

06-22 1万+
03-13 2851
05-04 368
05-20 131
09-17 7373
03-31 315
12-27 5059
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客