-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
529 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
title: 归并排序 | ||
author: lvzl | ||
--- | ||
|
||
## 归并排序 | ||
|
||
归并排序是对分治思想的典型应用,它按照如下的思路对分治思想“三步走”的框架进行了填充: | ||
|
||
1. 分解子问题:将需要被排序的数组从中间分割为两半,然后再将分割出来的每个子数组各分割为两半,重复以上操作,直到单个子数组只有一个元素为止。 | ||
2. 求解每个子问题:从粒度最小的子数组开始,两两合并、确保每次合并出来的数组都是有序的。(这里的“子问题”指的就是对每个子数组进行排序)。 | ||
3. 合并子问题的解,得出大问题的解:当数组被合并至原有的规模时,就得到了一个完全排序的数组 | ||
|
||
举个例子:[4,3,2,1] | ||
分割:[4,3]| [2,1] -> [4] | [3] | [2] | [1] | ||
合并:[4] | [3] | [2] | [1] -> [3,4]|[1,2] -> [1,2,3,4] | ||
|
||
**先不断的分割(想到递归),终止条件就是数组不能再继续分割 length <= 1,分割的逻辑也很简单,先找到中点,然后 slice。分割后产生左边右边,要将分割后的左右合并成有序的一个数组。** | ||
|
||
```js | ||
function mergeSort(arr) { | ||
if (arr.length <= 1) return arr | ||
|
||
const mergeArr = (arrL, arrR) => { | ||
const l1 = arrL.length, | ||
l2 = arrR.length | ||
let i = l1 - 1, | ||
j = l2 - 1, | ||
k = i + j + 1 | ||
while (i >= 0 && j >= 0) { | ||
if (arrR[j] > arrL[i]) { | ||
arrL[k] = arrR[j] | ||
j-- | ||
k-- | ||
} else { | ||
arrL[k] = arrL[i] | ||
i-- | ||
k-- | ||
} | ||
} | ||
while (j >= 0) { | ||
arrL[k] = arrR[j] | ||
j-- | ||
k-- | ||
} | ||
return arrL | ||
} | ||
|
||
const mid = Math.floor(arr.length / 2) | ||
const arrL = mergeSort(arr.slice(0, mid)) | ||
const arrR = mergeSort(arr.slice(mid, arr.length)) | ||
|
||
return mergeArr(arrL, arrR) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--- | ||
title: 快速排序 | ||
author: lvzl | ||
--- | ||
|
||
## 快速排序 | ||
|
||
快速排序在基本思想上和归并排序是一致的,仍然坚持“分而治之”的原则不动摇。区别在于,快速排序并不会把真的数组分割开来再合并到一个新数组中去,而是直接在原有的数组内部进行排序。 | ||
|
||
分的原则是什么?先找一个基准值 mid,两个指针: l = 0, r = length - 1 | ||
如果 arr[l] < arr[mid] l++ | ||
如果 arr[r] > arr[mid] r-- | ||
如果 l <= r 则意味着基准值左边存在较大元素或右边存在较小元素,交换两个元素确保左右两侧有序 | ||
|
||
```js | ||
function quickSort(arr, left = 0, right = arr.length - 1) { | ||
// 定义递归边界,若数组只有一个元素,则没有排序必要 | ||
if (arr.length > 1) { | ||
// lineIndex表示下一次划分左右子数组的索引位 | ||
const lineIndex = partition(arr, left, right) | ||
// 如果左边子数组的长度不小于1,则递归快排这个子数组 | ||
if (left < lineIndex - 1) { | ||
// 左子数组以 lineIndex-1 为右边界 | ||
quickSort(arr, left, lineIndex - 1) | ||
} | ||
// 如果右边子数组的长度不小于1,则递归快排这个子数组 | ||
if (lineIndex < right) { | ||
// 右子数组以 lineIndex 为左边界 | ||
quickSort(arr, lineIndex, right) | ||
} | ||
} | ||
return arr | ||
} | ||
// 以基准值为轴心,划分左右子数组的过程 | ||
function partition(arr, left, right) { | ||
// 基准值默认取中间位置的元素 | ||
let pivotValue = arr[Math.floor(left + (right - left) / 2)] | ||
// 初始化左右指针 | ||
let i = left | ||
let j = right | ||
// 当左右指针不越界时,循环执行以下逻辑 | ||
while (i <= j) { | ||
// 左指针所指元素若小于基准值,则右移左指针 | ||
while (arr[i] < pivotValue) { | ||
i++ | ||
} | ||
// 右指针所指元素大于基准值,则左移右指针 | ||
while (arr[j] > pivotValue) { | ||
j-- | ||
} | ||
|
||
// 若i<=j,则意味着基准值左边存在较大元素或右边存在较小元素,交换两个元素确保左右两侧有序 | ||
if (i <= j) { | ||
swap(arr, i, j) | ||
i++ | ||
j-- | ||
} | ||
} | ||
} | ||
|
||
const swap = (arr, i, j) => [arr[i], arr[j]] = [arr[j], arr[i]] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module.exports = { | ||
navText: 'Vue', | ||
sideText: ['Vue2'], | ||
sideText: ['Vue Router', 'Vue2', 'Vuex'], | ||
sort: 5, | ||
} |
Oops, something went wrong.