diff --git a/algorithms/bubbkeSort.js b/algorithms/bubbkeSort.js deleted file mode 100644 index 48a4731..0000000 --- a/algorithms/bubbkeSort.js +++ /dev/null @@ -1,98 +0,0 @@ -/** - * JavaScript版几种常见排序算法 - * - * 冒泡排序:最简单,也最慢,貌似长度小于7最优 - * 插入排序:比冒泡快,比快速排序和希尔排序慢,较小数据有优势 - * 快速排序:这是一个非常快的排序方式,V8的sort方法就使用快速排序和插入排序的结合 - * 希尔排序:在非chrome下数组长度小于1000,希尔排序比快速更快 - * 系统方法:在forfox下系统的这个方法非常快 - * 人**/ - -var jsSort = { - // 利用sort进行排序 - systemSort: function (arr) { - return arr.sort(function (a, b) { - return a - b; - }); - }, - // 冒泡排序 - bubbleSort: function (arr) { - var i = arr.length, j, tempExchangVal; - while (i > 0) { - for (j = 0; j < i - 1; j++) { - if (arr[j] > arr[j + 1]) { - tempExchangVal = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = tempExchangVal; - } - } - i--; - } - return arr; - }, - - // 快速排序 - quickSort: function (arr) { - function sort(prev, numsize) { - 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; - }, - //插入排序 - insertSort: function (arr) { - var 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; - }, - // 希尔排序 - shellSort: function (arr) { - var len = arr.length; - for (var fraction = Math.floor(len / 2); fraction > 0; fraction = Math.floor(fraction / 2)) { - for (var i = fraction; i < len; i++) { - for (var j = i - fraction; j >= 0 && arr[j] > arr [fraction + j]; j -= fraction) { - var temp = arr[j]; - arr[j] = arr[fraction + j]; - arr[fraction + j] = temp; - } - } - } - - return arr; - } -} \ No newline at end of file diff --git a/algorithms/bubbkeSort.test.js b/algorithms/bubbkeSort.test.js deleted file mode 100644 index e69de29..0000000 diff --git a/algorithms/deepClone.js b/algorithms/deepClone.js deleted file mode 100644 index 86edc66..0000000 --- a/algorithms/deepClone.js +++ /dev/null @@ -1,46 +0,0 @@ -/** - * 对象深度克隆 - * */ - -function clone(obj){ - var buf; - switch (Object.prototype.toString.call(obj)){ - case '[object Array]': - buf = []; - var i = obj.length; - while (i--){ - buf[i] = clone(obj[i]); - } - break; - case '[object JSON]': - if(window.JSON){ - buf ={}; - for(var i in obj){ - buf[i] = clone(obj[i]) - } - return buf; - } - break; - default: - return obj; - break; - } - - - /* if(obj instanceof Array){ - buf = []; - var i = obj.length; - while (i--){ - buf[i] = clone(obj[i]); - } - return buf; - }else if(obj instanceof Object){ - buf = {}; - for (var k in obj){ - buf[k] = clone(obj[k]); - } - return buf; - }else{ - return obj; - }*/ -} \ No newline at end of file diff --git a/algorithms/unique.js b/algorithms/unique.js deleted file mode 100644 index d0b490f..0000000 --- a/algorithms/unique.js +++ /dev/null @@ -1,44 +0,0 @@ -Array.prototype.unique1 = function(){ - var arr = []; //一个新的临时数组 - - //遍历当前数组 - for(var i = 0; i < this.length; i++){ - // 如果当前数组的第i已经保存进了临时数组,那么跳过, - // 否则把当前项push到临时数组里面 - if(arr.indexOf(this[i]) == -1) arr.push(this[i]); - } - return arr; -} - -Array.prototype.unique2 = function(){ - var n = {}, r = []; // n为hash表,r为临时数组 - for(var i = 0; i < this.length; i++){ - if(!n[this[i]]){ //如果hash表中没有当前项 - n[this[i]] = true; //存入hash表 - r.push(this[i]); //把当前数组的当前项push到临时数组里面 - } - } - return r; -} - -Array.prototype.unique3 = function(){ - var n = [this[0]]; // 结果数组 - for(var i = 1; i< this.length; i++){ //从第二项开始遍历 - //如果当前数组的第i项在当前数组中第一次出现的位置不是i, - //那么表示第i项是重复的,忽略掉。否则存入结果数组 - if(this.indexOf(this[i]) == i) n.push(this[i]); - } - return n; -} - -Array.prototype.unique4 = function(){ - this.sort(); - - var re = [this[0]]; - for(var i = 1; i< this.length; i++){ - if(this[i] !== re[re.length-1]){ - re.push(this[i]); - } - } - return re; -} \ No newline at end of file diff --git a/site/blog/md/code-snippets/ant-design.md b/site/blog/md/code-snippets/ant-design.md index bd8777a..6d4edaa 100644 --- a/site/blog/md/code-snippets/ant-design.md +++ b/site/blog/md/code-snippets/ant-design.md @@ -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] --- diff --git a/site/blog/md/code-snippets/deduplication.md b/site/blog/md/code-snippets/deduplication.md new file mode 100644 index 0000000..50c2ae1 --- /dev/null +++ b/site/blog/md/code-snippets/deduplication.md @@ -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])] +``` diff --git a/site/blog/md/code-snippets/deep-clone.md b/site/blog/md/code-snippets/deep-clone.md new file mode 100644 index 0000000..b79e3c4 --- /dev/null +++ b/site/blog/md/code-snippets/deep-clone.md @@ -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; + }*/ +} + +``` diff --git a/site/blog/md/code-snippets/delete-key.md b/site/blog/md/code-snippets/delete-key.md index 7ed728f..20e3282 100644 --- a/site/blog/md/code-snippets/delete-key.md +++ b/site/blog/md/code-snippets/delete-key.md @@ -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] --- diff --git a/site/blog/md/code-snippets/js-sort.md b/site/blog/md/code-snippets/js-sort.md new file mode 100644 index 0000000..122d859 --- /dev/null +++ b/site/blog/md/code-snippets/js-sort.md @@ -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; +}; +```