From 5891f2462e213eae4be41b034b4952997c38d1a6 Mon Sep 17 00:00:00 2001 From: polun <965076377@qq.com> Date: Sun, 22 Jan 2017 12:39:36 +0800 Subject: [PATCH 1/5] add buble sort implementation code --- code/bubbleSort.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 code/bubbleSort.js diff --git a/code/bubbleSort.js b/code/bubbleSort.js new file mode 100644 index 0000000..c91d7f8 --- /dev/null +++ b/code/bubbleSort.js @@ -0,0 +1,16 @@ +function bubbleSort(arr) { + const length = arr.length; + for (let i = 0; i < length - 1; i++) { + for (let j = 0; j < length - i - 1; j++) { + if (arr[j] > arr[j + 1]) { + let temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } +} + +const arr = [12, 3, 10, 11, 1]; +bubbleSort(arr); +console.log(arr); \ No newline at end of file From 2efdead49b4703e33bc86c5dc73ca5e463254ec5 Mon Sep 17 00:00:00 2001 From: polun <965076377@qq.com> Date: Sun, 22 Jan 2017 13:14:10 +0800 Subject: [PATCH 2/5] add selection sort --- 1.bubbleSort.md => 01.bubbleSort.md | 0 2.selectionSort.md => 02.selectionSort.md | 0 3.insertionSort.md => 03.insertionSort.md | 0 4.shellSort.md => 04.shellSort.md | 0 5.mergeSort.md => 05.mergeSort.md | 0 6.quickSort.md => 06.quickSort.md | 0 7.heapSort.md => 07.heapSort.md | 0 8.countingSort.md => 08.countingSort.md | 0 9.bucketSort.md => 09.bucketSort.md | 0 code/bubbleSort.js | 7 ++++--- code/selectionSort.js | 21 +++++++++++++++++++++ code/util.js | 12 ++++++++++++ 12 files changed, 37 insertions(+), 3 deletions(-) rename 1.bubbleSort.md => 01.bubbleSort.md (100%) rename 2.selectionSort.md => 02.selectionSort.md (100%) rename 3.insertionSort.md => 03.insertionSort.md (100%) rename 4.shellSort.md => 04.shellSort.md (100%) rename 5.mergeSort.md => 05.mergeSort.md (100%) rename 6.quickSort.md => 06.quickSort.md (100%) rename 7.heapSort.md => 07.heapSort.md (100%) rename 8.countingSort.md => 08.countingSort.md (100%) rename 9.bucketSort.md => 09.bucketSort.md (100%) create mode 100644 code/selectionSort.js create mode 100644 code/util.js diff --git a/1.bubbleSort.md b/01.bubbleSort.md similarity index 100% rename from 1.bubbleSort.md rename to 01.bubbleSort.md diff --git a/2.selectionSort.md b/02.selectionSort.md similarity index 100% rename from 2.selectionSort.md rename to 02.selectionSort.md diff --git a/3.insertionSort.md b/03.insertionSort.md similarity index 100% rename from 3.insertionSort.md rename to 03.insertionSort.md diff --git a/4.shellSort.md b/04.shellSort.md similarity index 100% rename from 4.shellSort.md rename to 04.shellSort.md diff --git a/5.mergeSort.md b/05.mergeSort.md similarity index 100% rename from 5.mergeSort.md rename to 05.mergeSort.md diff --git a/6.quickSort.md b/06.quickSort.md similarity index 100% rename from 6.quickSort.md rename to 06.quickSort.md diff --git a/7.heapSort.md b/07.heapSort.md similarity index 100% rename from 7.heapSort.md rename to 07.heapSort.md diff --git a/8.countingSort.md b/08.countingSort.md similarity index 100% rename from 8.countingSort.md rename to 08.countingSort.md diff --git a/9.bucketSort.md b/09.bucketSort.md similarity index 100% rename from 9.bucketSort.md rename to 09.bucketSort.md diff --git a/code/bubbleSort.js b/code/bubbleSort.js index c91d7f8..419774c 100644 --- a/code/bubbleSort.js +++ b/code/bubbleSort.js @@ -1,5 +1,8 @@ +const util = require('./util.js'); + function bubbleSort(arr) { const length = arr.length; + for (let i = 0; i < length - 1; i++) { for (let j = 0; j < length - i - 1; j++) { if (arr[j] > arr[j + 1]) { @@ -11,6 +14,4 @@ function bubbleSort(arr) { } } -const arr = [12, 3, 10, 11, 1]; -bubbleSort(arr); -console.log(arr); \ No newline at end of file +util(bubbleSort); \ No newline at end of file diff --git a/code/selectionSort.js b/code/selectionSort.js new file mode 100644 index 0000000..314828f --- /dev/null +++ b/code/selectionSort.js @@ -0,0 +1,21 @@ +const util = require('./util.js'); + +function selectionSort(arr) { + const length = arr.length; + let flag, temp; + + for (let i = 0; i < length - i; i++) { + flag = 0; + for (let j = 1; j < length - i; j++) { + if (arr[j] > arr[flag]) { + flag = j; + } + } + + temp = arr[length - i - 1]; + arr[length - i - 1] = arr[flag]; + arr[flag] = temp; + } +} + +util(selectionSort); \ No newline at end of file diff --git a/code/util.js b/code/util.js new file mode 100644 index 0000000..0b74d22 --- /dev/null +++ b/code/util.js @@ -0,0 +1,12 @@ +const os = require('os'); + +module.exports = function (sort) { + console.log(os.EOL + "排序名称:" + sort.name); + + const arr = [12, 3, 10, 11, 1]; + console.log(arr); + console.log('*'.repeat(20) + os.EOL); + + sort(arr); + console.log(arr); +}; \ No newline at end of file From b872ea80e351290571d5da948e6b83945dc52993 Mon Sep 17 00:00:00 2001 From: polun <965076377@qq.com> Date: Sun, 22 Jan 2017 14:27:25 +0800 Subject: [PATCH 3/5] add insertion sort --- code/{bubbleSort.js => 01.bubbleSort.js} | 2 ++ .../{selectionSort.js => 02.selectionSort.js} | 2 ++ code/03.insertionSort.js | 24 +++++++++++++++++++ code/util.js | 3 +-- 4 files changed, 29 insertions(+), 2 deletions(-) rename code/{bubbleSort.js => 01.bubbleSort.js} (90%) rename code/{selectionSort.js => 02.selectionSort.js} (91%) create mode 100644 code/03.insertionSort.js diff --git a/code/bubbleSort.js b/code/01.bubbleSort.js similarity index 90% rename from code/bubbleSort.js rename to code/01.bubbleSort.js index 419774c..77db6b0 100644 --- a/code/bubbleSort.js +++ b/code/01.bubbleSort.js @@ -12,6 +12,8 @@ function bubbleSort(arr) { } } } + + return arr; } util(bubbleSort); \ No newline at end of file diff --git a/code/selectionSort.js b/code/02.selectionSort.js similarity index 91% rename from code/selectionSort.js rename to code/02.selectionSort.js index 314828f..88c0434 100644 --- a/code/selectionSort.js +++ b/code/02.selectionSort.js @@ -16,6 +16,8 @@ function selectionSort(arr) { arr[length - i - 1] = arr[flag]; arr[flag] = temp; } + + return arr; } util(selectionSort); \ No newline at end of file diff --git a/code/03.insertionSort.js b/code/03.insertionSort.js new file mode 100644 index 0000000..73dadf4 --- /dev/null +++ b/code/03.insertionSort.js @@ -0,0 +1,24 @@ +const util = require('./util.js'); + +function insertionSort(arr) { + const length = arr.length; + + let prefixIndex, + current; + + for (let i = 1; i < length; i++) { + prefixIndex = i - 1; + current = arr[i]; + while (prefixIndex >= 0 && arr[prefixIndex] > current) { + arr[prefixIndex + 1] = arr[prefixIndex]; + + prefixIndex--; + } + + arr[prefixIndex + 1] = current; + } + + return arr; +} + +util(insertionSort); \ No newline at end of file diff --git a/code/util.js b/code/util.js index 0b74d22..4154a75 100644 --- a/code/util.js +++ b/code/util.js @@ -7,6 +7,5 @@ module.exports = function (sort) { console.log(arr); console.log('*'.repeat(20) + os.EOL); - sort(arr); - console.log(arr); + console.log(sort(arr)); }; \ No newline at end of file From db4a995e5653aba8946ec7d1443f7dcaf008c7e8 Mon Sep 17 00:00:00 2001 From: polun <965076377@qq.com> Date: Mon, 23 Jan 2017 10:48:34 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B8=8C=E5=B0=94?= =?UTF-8?q?=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/tasks.json | 9 +++++ code/01.bubbleSort.js | 8 +--- code/02.selectionSort.js | 8 +--- code/03.insertionSort.js | 8 +--- code/04.shellSort.js | 27 +++++++++++++ code/util.js | 84 ++++++++++++++++++++++++++++++++++------ 6 files changed, 115 insertions(+), 29 deletions(-) create mode 100644 .vscode/tasks.json create mode 100644 code/04.shellSort.js diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..7bbf960 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,9 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "0.1.0", + "command": "echo", + "isShellCommand": true, + "args": ["Hello World"], + "showOutput": "always" +} \ No newline at end of file diff --git a/code/01.bubbleSort.js b/code/01.bubbleSort.js index 77db6b0..67d1cd4 100644 --- a/code/01.bubbleSort.js +++ b/code/01.bubbleSort.js @@ -1,6 +1,4 @@ -const util = require('./util.js'); - -function bubbleSort(arr) { +module.exports = function bubbleSort(arr) { const length = arr.length; for (let i = 0; i < length - 1; i++) { @@ -14,6 +12,4 @@ function bubbleSort(arr) { } return arr; -} - -util(bubbleSort); \ No newline at end of file +}; \ No newline at end of file diff --git a/code/02.selectionSort.js b/code/02.selectionSort.js index 88c0434..0f8de65 100644 --- a/code/02.selectionSort.js +++ b/code/02.selectionSort.js @@ -1,6 +1,4 @@ -const util = require('./util.js'); - -function selectionSort(arr) { +module.exports = function selectionSort(arr) { const length = arr.length; let flag, temp; @@ -18,6 +16,4 @@ function selectionSort(arr) { } return arr; -} - -util(selectionSort); \ No newline at end of file +}; \ No newline at end of file diff --git a/code/03.insertionSort.js b/code/03.insertionSort.js index 73dadf4..1f3e37b 100644 --- a/code/03.insertionSort.js +++ b/code/03.insertionSort.js @@ -1,6 +1,4 @@ -const util = require('./util.js'); - -function insertionSort(arr) { +module.exports = function insertionSort(arr) { const length = arr.length; let prefixIndex, @@ -19,6 +17,4 @@ function insertionSort(arr) { } return arr; -} - -util(insertionSort); \ No newline at end of file +}; \ No newline at end of file diff --git a/code/04.shellSort.js b/code/04.shellSort.js new file mode 100644 index 0000000..519cfb6 --- /dev/null +++ b/code/04.shellSort.js @@ -0,0 +1,27 @@ +function shellSort(arr) { + const length = arr.length; + let h, + current, + prefixIndex; + + + h = length >> 1; + + while (h >= 1) { + for (let i = h; i < length; i += h) { + prefixIndex = i - h; + current = arr[i]; + for (; prefixIndex >= 0 && current < arr[prefixIndex]; prefixIndex -= h) { + arr[prefixIndex + h] = arr[prefixIndex]; + } + + arr[prefixIndex + h] = current; + } + + h >>= 1; + } + + return arr; +} + +module.exports = shellSort; \ No newline at end of file diff --git a/code/util.js b/code/util.js index 4154a75..9f13c5f 100644 --- a/code/util.js +++ b/code/util.js @@ -1,11 +1,73 @@ -const os = require('os'); - -module.exports = function (sort) { - console.log(os.EOL + "排序名称:" + sort.name); - - const arr = [12, 3, 10, 11, 1]; - console.log(arr); - console.log('*'.repeat(20) + os.EOL); - - console.log(sort(arr)); -}; \ No newline at end of file +const os = require('os'); +const bubbleSort = require('./01.bubbleSort.js'); +const selectionSort = require('./02.selectionSort.js'); +const insertionSort = require('./03.insertionSort.js'); +const shellSort = require('./04.shellSort.js'); + +const arrayLength = 10000; +const testCount = 100; + +function randomArray() { + const arr = []; + + for (let i = 0; i < arrayLength; i++) { + arr.push(Math.floor(Math.random() * 1000)); + } + + return arr; +} + +function getItemCounter(arr) { + const map = new Map(); + + arr.forEach(item => { + map.has(item) ? map.set(item, map.get(item) + 1) : map.set(item, 1); + }); + + return map; +} + +/** + * + * + * @param {number[]} arr + * @param {number[]} sortedArr + */ +function check(arr, sortedArr) { + for (let i = 0; i < sortedArr.length - 1; i++) { + if (sortedArr[i] > sortedArr[i + 1]) { + throw new Error('升序排序错误'); + } + } + + const arrMap = getItemCounter(arr); + const sortedArrMap = getItemCounter(sortedArr); + + for (let key of arrMap.keys()) { + if (sortedArrMap.get(key) === undefined + || sortedArrMap.get(key) !== arrMap.get(key)) { + throw new Error('排序错误'); + } + } + + return true; +} + +function test(sort, toBeSortedArr) { + const innerSorted = toBeSortedArr.slice(); + + const sortedArr = sort(innerSorted.slice()); + check(innerSorted, sortedArr); + + const sTime = Date.now(); + for (let i = 0; i <= testCount; i++) { + sort(innerSorted.slice()); + } + + console.log(`${arrayLength} elements ${testCount} times test ${sort.name} cost ${Date.now() - sTime} ms`); +}; + +const toBeSortedArr = randomArray(); +// test(bubbleSort, toBeSortedArr); +test(insertionSort, toBeSortedArr); +test(shellSort, toBeSortedArr); \ No newline at end of file From c9ca66ea6cebce286bf96d907312dce38c8320b0 Mon Sep 17 00:00:00 2001 From: zhenqiang Date: Mon, 23 Oct 2017 15:33:21 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0binarysearch.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/search/binarysearch.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 code/search/binarysearch.js diff --git a/code/search/binarysearch.js b/code/search/binarysearch.js new file mode 100644 index 0000000..0e685f0 --- /dev/null +++ b/code/search/binarysearch.js @@ -0,0 +1,29 @@ +function binarySearch(arr, target) { + if (!Array.isArray(arr)) return arr; + + arr.sort((l, r) => l - r); + + let left = 0; + let right = arr.length - 1; + let middle; + + while (left <= right) { + middle = left + parseInt((right - left) / 2); + + if (arr[middle] < target) { + left = middle + 1; + } else if (arr[middle] > target) { + right = middle - 1; + } else { + return middle; + } + } + + return -1; +} + +console.log(binarySearch([1, 2, 5], 5)); +console.log(binarySearch([1], 5)); +console.log(binarySearch([1, 5], 1)); +console.log(binarySearch([1, 5], 5)); +console.log(binarySearch([])); \ No newline at end of file