## Javascript常见排序算法的笔记

::::::: 2020-11-06 01:22:50
javascript 常见 排序 算法 笔记

```function ArrayData () {
let ret = []
this.times = 0 // 统计执行次数
this.push = (item) => {
ret.push(item)
}
this.toString = () => {
return ret.join()
}
}
const arr = [34, 11, 45, 22, 31, 99, 68, 54]```

## 冒泡排序

```function ArrayData () {
// ......
this.bubbleSort = function () {
let length = ret.length;
for (let i = 0; i < length; i++) {
for (let j = 0; j < length - 1; j++) {
this.times++
if (ret[j] > ret[j + 1]) {
[ret[j], ret[j + 1]] = [ret[j + 1], ret[j]]
}
}
}
}
}
let tmp = new ArrayData()
arr.forEach((item) => {
tmp.push(item)
})
tmp.bubbleSort()
console.log(tmp.times) // 56```

```function ArrayData () {
// ......
this.bubbleSort = function () {
let length = ret.length;
for (let i = 0; i < length; i++) {
let change = false
for (let j = 0; j < length - 1; j++) {
this.times++Ï
if (ret[j] > ret[j + 1]) {
[ret[j], ret[j + 1]] = [ret[j + 1], ret[j]]
change = true
}
}
if (!change) {
break
}
}
}
}
let tmp = new ArrayData()
arr.forEach((item) => {
tmp.push(item)
})
tmp.bubbleSort()
console.log(tmp.times) // 21```

```function ArrayData () {
// ......
this.bubbleSort = function () {
let length = ret.length;
for (let i = 0; i < length; i++) {
let change = false
for (let j = 0; j < length - 1 - i; j++) {
this.times++
if (ret[j] > ret[j + 1]) {
[ret[j], ret[j + 1]] = [ret[j + 1], ret[j]]
change = true
}
}
if (!change) {
break
}
}
}
}
let tmp = new ArrayData()
arr.forEach((item) => {
tmp.push(item)
})
tmp.bubbleSort()
console.log(tmp.times) // 18```

## 选择排序

```function ArrayData () {
// ......
this.selectionSort = function () {
let length = ret.length
for (let i = 0; i < length - 1; i++) {
let minIndex = i
for (let j = i; j < length; j++) {
if (ret[j] < ret[minIndex]) {
minIndex = j
}
}
if (i !== minIndex) {
[ret[i], ret[minIndex]] = [ret[minIndex], ret[i]]
}
}
}
}
let tmp = new ArrayData()
arr.forEach((item) => {
tmp.push(item)
})
tmp.selectionSort()```

## 插入排序

```function ArrayData () {
// ......
this.insertSort = function () {
let length = ret.length
let j
for (let i = 1; i < length; i++) {
let currentNumber = ret[i]
for (j = i - 1; j >= 0 && ret[j] > currentNumber; j--) {
ret[j + 1] = ret[j]
}
ret[j + 1] = currentNumber
}
}
}
let tmp = new ArrayData()
arr.forEach((item) => {
tmp.push(item)
})
tmp.insertSort()```

## 快速排序

```function ArrayData () {
// ......
this.quickSort = function () {
quick(ret, 0, ret.length - 1);
function quick(array, left, right) {
let index
if (array.length > 1) {
index = partition(array, left, right)
if (left < index - 1) {
quick(array, left, index - 1)
}
if (right > index) {
quick(array, index, right)
}
}
return array
}
function partition(array, left, right) {
let pivot = array[Math.floor((right + left) / 2)],
i = left,
j = right;
while (i <= j) {
while (array[i] < pivot) {
i++
}
while (array[j] > pivot) {
j--
}
if (i <= j) {
swap(array, i, j);
i++;
j--;
}
}
return i
}
function swap(array, i, j) {
[array[i], array[j]] = [array[j], array[i]]
}
}
}
let tmp = new ArrayData()
arr.forEach((item) => {
tmp.push(item)
})
tmp.quickSort()```

```function quickSort(arr) {
return arr.length <= 1 ? arr : quickSort(arr.slice(1).filter((item) => item <= arr[0])).concat(arr[0], quickSort(arr.slice(1).filter((item) => item > arr[0])))
}```

## 希尔排序

```function ArrayData () {
// ......
this.shellSort = function () {
let length = ret.length
for (let step = Math.floor(length / 2); step > 0; step = Math.floor(step / 2)) {
for (let i = 0; i < step; i++) {
shellInsertSort(i, step)
}
}
function shellInsertSort(index, step) {
let length = ret.length
let j
for (let i = index; i < length; i += step) {
let currentNumber = ret[i]
for (j = i - step; j >= 0 && ret[j] > currentNumber; j -= step) {
ret[j + step] = ret[j]
}
ret[j + step] = currentNumber
}
}
}
}
let tmp = new ArrayData()
arr.forEach((item) => {
tmp.push(item)
})
tmp.shellSort()```

## 归并排序

```function ArrayData () {
// ......
this.mergeSort = function () {
ret = mergeSortFun(ret)
function mergeSortFun(arr) {
let length = arr.length
if (length <= 1) {
return arr
}
let mid = Math.floor(length / 2),
left = arr.slice(0, mid),
right = arr.slice(mid, length)
return mengeConnect(mergeSortFun(left), mergeSortFun(right))
}
function mengeConnect(left, right) {
let
leftIndex = 0,
rightIndex = 0,
result = []
while (leftIndex < left.length && rightIndex < right.length) {
result.push(left[leftIndex] < right[rightIndex] ? left[leftIndex++] : right[rightIndex++])
}
while (leftIndex < left.length) {
result.push(left[leftIndex++])
}
while (rightIndex < right.length) {
result.push(right[rightIndex++])
}
return result
}
}
}
let tmp = new ArrayData()
arr.forEach((item) => {
tmp.push(item)
})
tmp.mergeSort()```

https://cloud.tencent.com/developer/article/1715165