Skip to content

Commit

Permalink
feat: 添加内容
Browse files Browse the repository at this point in the history
  • Loading branch information
梁怀刚 committed Jul 12, 2024
1 parent 05d699c commit df0a650
Show file tree
Hide file tree
Showing 23 changed files with 3,709 additions and 0 deletions.
46 changes: 46 additions & 0 deletions blog/2018-09-28-前端基础算法——二分查找.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
authors: Harry
tags: [daily]
---

## 前端基础算法——二分查找


> 二分查找,其查找的列表必须有序。时间复杂度为:O(log n)
![](https://cdn.jsdelivr.net/gh/HarryPoint/oss@main/uPic/2024-07-12_14:26:24_82tOfj.gif)

二分查询/简单查询对比(查找数23)

![](https://cdn.jsdelivr.net/gh/HarryPoint/oss@main/uPic/2024-07-12_14:27:28_XAt7yO.gif)

二分查询/简单查询对比(查找数1)

<!--kg-card-begin: markdown-->

```
function binarySearch (list, item) {
// 保存最小与最大位置
let min = 0
let max = list.length - 1
// 最小位置是否小与等于最大位置
while (min <= max) {
let index = Math.floor((min + max) / 2)
let val = list[index]
if (val === item) {
return index
} else if (val > item) {
max = index - 1
} else {
min = index + 1
}
}
return null
}
```

<!--kg-card-end: markdown-->

[binarySearch](https://codepen.io/ZhelinCheng/embed/preview/dQjjZK?height=300\&slug-hash=dQjjZK\&default-tabs=js,result\&host=https://codepen.io)
40 changes: 40 additions & 0 deletions blog/2018-10-01-前端基础算法——选择排序.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## 前端基础算法——选择排序


<!--kg-card-begin: markdown-->

> 选择排序,运行时间:O(n^2)
```
// 找出数组中最小的数字
function findSmallest (arr) {
let minValue = arr[0]
let minIndex = 0
for (let i in arr) {
let item = arr[i]
if (item >= minValue) { continue }
minIndex = i
minValue = item
}
return minIndex
}
// 对数字进行排序
function selectionSort (arr) {
let len = arr.length
let newArr = []
for (let i = 0; i < len; i++) {
let minIndex = findSmallest(arr)
newArr.push(arr[minIndex])
arr.splice(minIndex, 1)
}
return newArr
}
```

<!--kg-card-end: markdown-->

[Selection Sort](https://codepen.io/ZhelinCheng/embed/preview/WYaZOX?height=300\&slug-hash=WYaZOX\&default-tabs=js,result\&host=https://codepen.io)
Loading

0 comments on commit df0a650

Please sign in to comment.