Skip to content

Commit db4a995

Browse files
author
polun
committed
添加希尔排序
1 parent b872ea8 commit db4a995

File tree

6 files changed

+115
-29
lines changed

6 files changed

+115
-29
lines changed

.vscode/tasks.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "0.1.0",
5+
"command": "echo",
6+
"isShellCommand": true,
7+
"args": ["Hello World"],
8+
"showOutput": "always"
9+
}

code/01.bubbleSort.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const util = require('./util.js');
2-
3-
function bubbleSort(arr) {
1+
module.exports = function bubbleSort(arr) {
42
const length = arr.length;
53

64
for (let i = 0; i < length - 1; i++) {
@@ -14,6 +12,4 @@ function bubbleSort(arr) {
1412
}
1513

1614
return arr;
17-
}
18-
19-
util(bubbleSort);
15+
};

code/02.selectionSort.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const util = require('./util.js');
2-
3-
function selectionSort(arr) {
1+
module.exports = function selectionSort(arr) {
42
const length = arr.length;
53
let flag, temp;
64

@@ -18,6 +16,4 @@ function selectionSort(arr) {
1816
}
1917

2018
return arr;
21-
}
22-
23-
util(selectionSort);
19+
};

code/03.insertionSort.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const util = require('./util.js');
2-
3-
function insertionSort(arr) {
1+
module.exports = function insertionSort(arr) {
42
const length = arr.length;
53

64
let prefixIndex,
@@ -19,6 +17,4 @@ function insertionSort(arr) {
1917
}
2018

2119
return arr;
22-
}
23-
24-
util(insertionSort);
20+
};

code/04.shellSort.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function shellSort(arr) {
2+
const length = arr.length;
3+
let h,
4+
current,
5+
prefixIndex;
6+
7+
8+
h = length >> 1;
9+
10+
while (h >= 1) {
11+
for (let i = h; i < length; i += h) {
12+
prefixIndex = i - h;
13+
current = arr[i];
14+
for (; prefixIndex >= 0 && current < arr[prefixIndex]; prefixIndex -= h) {
15+
arr[prefixIndex + h] = arr[prefixIndex];
16+
}
17+
18+
arr[prefixIndex + h] = current;
19+
}
20+
21+
h >>= 1;
22+
}
23+
24+
return arr;
25+
}
26+
27+
module.exports = shellSort;

code/util.js

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,73 @@
1-
const os = require('os');
2-
3-
module.exports = function (sort) {
4-
console.log(os.EOL + "排序名称:" + sort.name);
5-
6-
const arr = [12, 3, 10, 11, 1];
7-
console.log(arr);
8-
console.log('*'.repeat(20) + os.EOL);
9-
10-
console.log(sort(arr));
11-
};
1+
const os = require('os');
2+
const bubbleSort = require('./01.bubbleSort.js');
3+
const selectionSort = require('./02.selectionSort.js');
4+
const insertionSort = require('./03.insertionSort.js');
5+
const shellSort = require('./04.shellSort.js');
6+
7+
const arrayLength = 10000;
8+
const testCount = 100;
9+
10+
function randomArray() {
11+
const arr = [];
12+
13+
for (let i = 0; i < arrayLength; i++) {
14+
arr.push(Math.floor(Math.random() * 1000));
15+
}
16+
17+
return arr;
18+
}
19+
20+
function getItemCounter(arr) {
21+
const map = new Map();
22+
23+
arr.forEach(item => {
24+
map.has(item) ? map.set(item, map.get(item) + 1) : map.set(item, 1);
25+
});
26+
27+
return map;
28+
}
29+
30+
/**
31+
*
32+
*
33+
* @param {number[]} arr
34+
* @param {number[]} sortedArr
35+
*/
36+
function check(arr, sortedArr) {
37+
for (let i = 0; i < sortedArr.length - 1; i++) {
38+
if (sortedArr[i] > sortedArr[i + 1]) {
39+
throw new Error('升序排序错误');
40+
}
41+
}
42+
43+
const arrMap = getItemCounter(arr);
44+
const sortedArrMap = getItemCounter(sortedArr);
45+
46+
for (let key of arrMap.keys()) {
47+
if (sortedArrMap.get(key) === undefined
48+
|| sortedArrMap.get(key) !== arrMap.get(key)) {
49+
throw new Error('排序错误');
50+
}
51+
}
52+
53+
return true;
54+
}
55+
56+
function test(sort, toBeSortedArr) {
57+
const innerSorted = toBeSortedArr.slice();
58+
59+
const sortedArr = sort(innerSorted.slice());
60+
check(innerSorted, sortedArr);
61+
62+
const sTime = Date.now();
63+
for (let i = 0; i <= testCount; i++) {
64+
sort(innerSorted.slice());
65+
}
66+
67+
console.log(`${arrayLength} elements ${testCount} times test ${sort.name} cost ${Date.now() - sTime} ms`);
68+
};
69+
70+
const toBeSortedArr = randomArray();
71+
// test(bubbleSort, toBeSortedArr);
72+
test(insertionSort, toBeSortedArr);
73+
test(shellSort, toBeSortedArr);

0 commit comments

Comments
 (0)