Skip to content

Commit

Permalink
feat(site/blog): add some code snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
qhanw committed Apr 30, 2024
1 parent 56d50b7 commit 7aca7b2
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 190 deletions.
98 changes: 0 additions & 98 deletions algorithms/bubbkeSort.js

This file was deleted.

Empty file removed algorithms/bubbkeSort.test.js
Empty file.
46 changes: 0 additions & 46 deletions algorithms/deepClone.js

This file was deleted.

44 changes: 0 additions & 44 deletions algorithms/unique.js

This file was deleted.

2 changes: 1 addition & 1 deletion site/blog/md/code-snippets/ant-design.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Ant Design 弹窗简化代码示例
date: 2024-04-10T20:08:56+08:00
date: 2024-04-30T20:08:56+08:00
category: antd
tags: [antd]
---
Expand Down
10 changes: 10 additions & 0 deletions site/blog/md/code-snippets/deduplication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: 数组去重
date: 2024-04-08T20:08:56+08:00
category: js
tags: [js]
---

```ts
const arr =[...new Set([1,2,3,4,4,4,3,5])]
```
51 changes: 51 additions & 0 deletions site/blog/md/code-snippets/deep-clone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: 对象深度克隆
date: 2024-04-10T20:08:56+08:00
category: js
tags: [js]
---

```ts
function clone(obj) {
let buf;
switch (Object.prototype.toString.call(obj)) {
case "[object Array]":
buf = [];
let i = obj.length;
while (i--) {
buf[i] = clone(obj[i]);
}
break;
case "[object JSON]":
if (window.JSON) {
buf = {};
for (let i in obj) {
buf[i] = clone(obj[i]);
}
return buf;
}
break;
default:
return obj;
break;
}

/* if(obj instanceof Array){
buf = [];
let i = obj.length;
while (i--){
buf[i] = clone(obj[i]);
}
return buf;
}else if(obj instanceof Object){
buf = {};
for (let k in obj){
buf[k] = clone(obj[k]);
}
return buf;
}else{
return obj;
}*/
}

```
2 changes: 1 addition & 1 deletion site/blog/md/code-snippets/delete-key.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: 删除对像中的键值对
date: 2024-04-10T20:08:56+08:00
date: 2024-04-20T20:08:56+08:00
category: js
tags: [js]
---
Expand Down
115 changes: 115 additions & 0 deletions site/blog/md/code-snippets/js-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
---
title: JavaScript版几种常见排序算法
date: 2024-04-08T20:08:56+08:00
category: js
tags: [js]
---

* 系统方法:在`firefox`下系统的这个方法非常快
```ts
const systemSort = (arr: number[]) => arr.sort((a, b) => a - b);
```

* 冒泡排序:最简单,也最慢,貌似长度小于7最优
```ts
const bubbleSort = (arr: number[]) => {
var i = arr.length,
j,
tempExchangeVal;
while (i > 0) {
for (j = 0; j < i - 1; j++) {
if (arr[j] > arr[j + 1]) {
tempExchangeVal = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tempExchangeVal;
}
}
i--;
}
return arr;
};
```


* 插入排序:比冒泡快,比快速排序和希尔排序慢,较小数据有优势
```ts
const insertSort = (arr: number[]) => {
let i = 1,
j,
len = arr.length,
key;
for (; i < len; i++) {
j = i;
key = arr[j];
while (--j > -1) {
if (arr[j] > key) {
arr[j + 1] = arr[j];
} else {
break;
}
}
arr[j + 1] = key;
}
return arr;
};
```

* 快速排序:这是一个非常快的排序方式,V8的sort方法就使用快速排序和插入排序的结合
```ts
const quickSort = (arr: number[]) => {
function sort(prev: number, numSize: number) {
var nonius = prev;
var j = numSize - 1;
var flag = arr[prev];
if (numSize - prev > 1) {
while (nonius < j) {
for (; nonius < j; j--) {
if (arr[j] < flag) {
arr[nonius++] = arr[j]; //a[i] = a[j]; i += 1;
break;
}
}
for (; nonius < j; nonius++) {
if (arr[nonius] > flag) {
arr[j--] = arr[nonius];
break;
}
}
}
arr[nonius] = flag;
sort(0, nonius);
sort(nonius + 1, numSize);
}
}

sort(0, arr.length);
return arr;
};
```

* 希尔排序:在非chrome下数组长度小于1000,希尔排序比快速更快

```ts
const shellSort = (arr: number[]) => {
let len = arr.length;
for (
let fraction = Math.floor(len / 2);
fraction > 0;
fraction = Math.floor(fraction / 2)
) {
for (let i = fraction; i < len; i++) {
for (
let j = i - fraction;
j >= 0 && arr[j] > arr[fraction + j];
j -= fraction
) {
let temp = arr[j];
arr[j] = arr[fraction + j];
arr[fraction + j] = temp;
}
}
}

return arr;
};
```

0 comments on commit 7aca7b2

Please sign in to comment.