Skip to content

Commit

Permalink
feat(Vuex & Vue Router)
Browse files Browse the repository at this point in the history
  • Loading branch information
lv-z-l committed Sep 15, 2023
1 parent 6eb8ff8 commit 1a33ed3
Show file tree
Hide file tree
Showing 6 changed files with 529 additions and 3 deletions.
7 changes: 5 additions & 2 deletions articles/algorithm/sort/insert-sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ function insertSort(arr) {
while (i <= arr.length - 1) {
const temp = arr[i]
let j = i
while (j > 0 && arr[j - 1] > temp) { // 找位置,如果前面的比它小,就一直往前面找,并且这些比它大的值都往后挪位置,因为它 temp 的位置是空的,单独拎出去等着了,原来在它前面比它大的就可以往后占它的位置,直到给它找到合适的位置。
arr[j] = arr[j - 1]
// 找位置,如果前面的比它小,就一直往前面找,并且这些比它大的值都往后挪位置
// 因为它 temp 的位置是空的,单独拎出去等着了,在它前面比它大的就可以往后占它的位置,
// 直到给它找到合适的位置。
while (j > 0 && arr[j - 1] > temp) {
arr[j] = arr[j - 1] // 往后挪位置
j--
}
arr[j] = temp
Expand Down
55 changes: 55 additions & 0 deletions articles/algorithm/sort/merge-sort.md
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)
}
```
62 changes: 62 additions & 0 deletions articles/algorithm/sort/quick-sort.md
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]]
```
2 changes: 1 addition & 1 deletion articles/vue/nav.js
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,
}
Loading

0 comments on commit 1a33ed3

Please sign in to comment.