sortism 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/README.md +84 -0
- package/bun.lock +26 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +28 -0
- package/dist/sorts/bubble.d.ts +3 -0
- package/dist/sorts/bubble.js +27 -0
- package/dist/sorts/merge.d.ts +3 -0
- package/dist/sorts/merge.js +48 -0
- package/dist/sorts/quick.d.ts +3 -0
- package/dist/sorts/quick.js +34 -0
- package/dist/sorts/selection.d.ts +3 -0
- package/dist/sorts/selection.js +27 -0
- package/package.json +26 -0
- package/src/index.ts +41 -0
- package/src/sorts/bubble.ts +36 -0
- package/src/sorts/merge.ts +69 -0
- package/src/sorts/quick.ts +46 -0
- package/src/sorts/selection.ts +37 -0
- package/tests/index.ts +27 -0
- package/tsconfig.json +31 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2026 Pro203S
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# sortism
|
|
2
|
+
|
|
3
|
+
 
|
|
4
|
+
|
|
5
|
+
자신에게 맞는 방법으로 배열을 정렬해보세요!
|
|
6
|
+
이 모듈에는 여러가지의 정렬 방식이 있습니다.
|
|
7
|
+
자신에게 맞는 정렬을 사용하시면 됩니다.
|
|
8
|
+
|
|
9
|
+
## 사용법
|
|
10
|
+
|
|
11
|
+
1. 패키지 설치
|
|
12
|
+
```sh
|
|
13
|
+
npm i sortism
|
|
14
|
+
|
|
15
|
+
bun i sortism
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
2. 모듈 가져오기
|
|
19
|
+
```typescript
|
|
20
|
+
import sortism from 'sortism';
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
3. 정렬 사용하기
|
|
24
|
+
```typescript
|
|
25
|
+
const arr = [5,2,3,1,4];
|
|
26
|
+
|
|
27
|
+
// 버블 정렬, 오름차순
|
|
28
|
+
// 1,2,3,4,5
|
|
29
|
+
sortism(arr).bubble("ascending");
|
|
30
|
+
|
|
31
|
+
// 선택 정렬, 내림차순
|
|
32
|
+
// 5,4,3,2,1
|
|
33
|
+
sortism(arr).selection("descending");
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## 자세한 사용법
|
|
37
|
+
|
|
38
|
+
- Object에서 정렬하기
|
|
39
|
+
```typescript
|
|
40
|
+
const arr = [
|
|
41
|
+
{
|
|
42
|
+
"index": 2
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"index": 0
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
"index": 2
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"index": 6
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"index": 4
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
"index": 3
|
|
58
|
+
}
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
// 버블 정렬, 오름차순
|
|
62
|
+
sortism(arr).bubble("ascending");
|
|
63
|
+
|
|
64
|
+
/*
|
|
65
|
+
returns:
|
|
66
|
+
[
|
|
67
|
+
{
|
|
68
|
+
index: 0,
|
|
69
|
+
}, {
|
|
70
|
+
index: 2,
|
|
71
|
+
}, {
|
|
72
|
+
index: 2,
|
|
73
|
+
}, {
|
|
74
|
+
index: 3,
|
|
75
|
+
}, {
|
|
76
|
+
index: 4,
|
|
77
|
+
}, {
|
|
78
|
+
index: 6,
|
|
79
|
+
}
|
|
80
|
+
]
|
|
81
|
+
*/
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
지금 바로 설치해서 사용해보세요!
|
package/bun.lock
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"lockfileVersion": 1,
|
|
3
|
+
"configVersion": 1,
|
|
4
|
+
"workspaces": {
|
|
5
|
+
"": {
|
|
6
|
+
"name": "sortism",
|
|
7
|
+
"devDependencies": {
|
|
8
|
+
"@types/bun": "latest",
|
|
9
|
+
},
|
|
10
|
+
"peerDependencies": {
|
|
11
|
+
"typescript": "^5",
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
"packages": {
|
|
16
|
+
"@types/bun": ["@types/bun@1.3.11", "", { "dependencies": { "bun-types": "1.3.11" } }, "sha512-5vPne5QvtpjGpsGYXiFyycfpDF2ECyPcTSsFBMa0fraoxiQyMJ3SmuQIGhzPg2WJuWxVBoxWJ2kClYTcw/4fAg=="],
|
|
17
|
+
|
|
18
|
+
"@types/node": ["@types/node@25.5.0", "", { "dependencies": { "undici-types": "~7.18.0" } }, "sha512-jp2P3tQMSxWugkCUKLRPVUpGaL5MVFwF8RDuSRztfwgN1wmqJeMSbKlnEtQqU8UrhTmzEmZdu2I6v2dpp7XIxw=="],
|
|
19
|
+
|
|
20
|
+
"bun-types": ["bun-types@1.3.11", "", { "dependencies": { "@types/node": "*" } }, "sha512-1KGPpoxQWl9f6wcZh57LvrPIInQMn2TQ7jsgxqpRzg+l0QPOFvJVH7HmvHo/AiPgwXy+/Thf6Ov3EdVn1vOabg=="],
|
|
21
|
+
|
|
22
|
+
"typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],
|
|
23
|
+
|
|
24
|
+
"undici-types": ["undici-types@7.18.2", "", {}, "sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w=="],
|
|
25
|
+
}
|
|
26
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type InternalSortFunction = <T>(arr: T[], func: (v: T) => number, order: "ascending" | "descending") => T[];
|
|
2
|
+
export type ExportSortFunction<T> = (order: "ascending" | "descending", func?: (v: T) => number) => T[];
|
|
3
|
+
/**
|
|
4
|
+
* You can use sortism through this function.
|
|
5
|
+
*/
|
|
6
|
+
export default function sortism<T = any>(array: T[], func?: (v: T) => number): {
|
|
7
|
+
/**
|
|
8
|
+
* Time: O(n^2)
|
|
9
|
+
*/
|
|
10
|
+
bubble: ExportSortFunction<T>;
|
|
11
|
+
/**
|
|
12
|
+
* Time: O(n^2)
|
|
13
|
+
*/
|
|
14
|
+
selection: ExportSortFunction<T>;
|
|
15
|
+
/**
|
|
16
|
+
* Time: O(n log n)
|
|
17
|
+
*/
|
|
18
|
+
merge: ExportSortFunction<T>;
|
|
19
|
+
/**
|
|
20
|
+
* Time: O(n log n)
|
|
21
|
+
*/
|
|
22
|
+
quick: ExportSortFunction<T>;
|
|
23
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import bubble from "./sorts/bubble";
|
|
2
|
+
import merge from "./sorts/merge";
|
|
3
|
+
import quick from "./sorts/quick";
|
|
4
|
+
import selection from "./sorts/selection";
|
|
5
|
+
/**
|
|
6
|
+
* You can use sortism through this function.
|
|
7
|
+
*/
|
|
8
|
+
export default function sortism(array, func) {
|
|
9
|
+
const f = func ?? (v => v);
|
|
10
|
+
return {
|
|
11
|
+
/**
|
|
12
|
+
* Time: O(n^2)
|
|
13
|
+
*/
|
|
14
|
+
"bubble": (order => bubble(array, f, order)),
|
|
15
|
+
/**
|
|
16
|
+
* Time: O(n^2)
|
|
17
|
+
*/
|
|
18
|
+
"selection": (order => selection(array, f, order)),
|
|
19
|
+
/**
|
|
20
|
+
* Time: O(n log n)
|
|
21
|
+
*/
|
|
22
|
+
"merge": (order => merge(array, f, order)),
|
|
23
|
+
/**
|
|
24
|
+
* Time: O(n log n)
|
|
25
|
+
*/
|
|
26
|
+
"quick": (order => quick(array, f, order)),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const bubble = (arr, func, order) => {
|
|
2
|
+
const indexMap = arr.map(v => ({
|
|
3
|
+
"index": func(v),
|
|
4
|
+
"value": v
|
|
5
|
+
}));
|
|
6
|
+
const isAsc = order === "ascending";
|
|
7
|
+
for (let i = 0; i < indexMap.length - 1; i++) {
|
|
8
|
+
let swapped = false;
|
|
9
|
+
for (let j = 0; j < indexMap.length - 1 - i; j++) {
|
|
10
|
+
const current = indexMap[j];
|
|
11
|
+
const next = indexMap[j + 1];
|
|
12
|
+
const shouldSwap = isAsc
|
|
13
|
+
? current.index > next.index
|
|
14
|
+
: current.index < next.index;
|
|
15
|
+
if (shouldSwap) {
|
|
16
|
+
const temp = indexMap[j];
|
|
17
|
+
indexMap[j] = indexMap[j + 1];
|
|
18
|
+
indexMap[j + 1] = temp;
|
|
19
|
+
swapped = true;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (!swapped)
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
return indexMap.map(v => v.value);
|
|
26
|
+
};
|
|
27
|
+
export default bubble;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const merge = (arr, func, order) => {
|
|
2
|
+
const indexMap = arr.map(v => ({
|
|
3
|
+
index: func(v),
|
|
4
|
+
value: v
|
|
5
|
+
}));
|
|
6
|
+
const isAsc = order === "ascending";
|
|
7
|
+
const mergeSort = (array) => {
|
|
8
|
+
if (array.length <= 1)
|
|
9
|
+
return array;
|
|
10
|
+
const mid = Math.floor(array.length / 2);
|
|
11
|
+
const left = mergeSort(array.slice(0, mid));
|
|
12
|
+
const right = mergeSort(array.slice(mid));
|
|
13
|
+
return mergeArray(left, right);
|
|
14
|
+
};
|
|
15
|
+
const mergeArray = (left, right) => {
|
|
16
|
+
const result = [];
|
|
17
|
+
let i = 0;
|
|
18
|
+
let j = 0;
|
|
19
|
+
while (i < left.length && j < right.length) {
|
|
20
|
+
const l = left[i];
|
|
21
|
+
const r = right[j];
|
|
22
|
+
const shouldTakeLeft = isAsc
|
|
23
|
+
? l.index <= r.index
|
|
24
|
+
: l.index >= r.index;
|
|
25
|
+
if (shouldTakeLeft) {
|
|
26
|
+
result.push(l);
|
|
27
|
+
i++;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
result.push(r);
|
|
31
|
+
j++;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
// 남은 요소 처리
|
|
35
|
+
while (i < left.length) {
|
|
36
|
+
result.push(left[i]);
|
|
37
|
+
i++;
|
|
38
|
+
}
|
|
39
|
+
while (j < right.length) {
|
|
40
|
+
result.push(right[j]);
|
|
41
|
+
j++;
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
};
|
|
45
|
+
const sorted = mergeSort(indexMap);
|
|
46
|
+
return sorted.map(v => v.value);
|
|
47
|
+
};
|
|
48
|
+
export default merge;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const quick = (arr, func, order) => {
|
|
2
|
+
const indexMap = arr.map(v => ({
|
|
3
|
+
index: func(v),
|
|
4
|
+
value: v
|
|
5
|
+
}));
|
|
6
|
+
const isAsc = order === "ascending";
|
|
7
|
+
const quickSort = (array) => {
|
|
8
|
+
if (array.length <= 1)
|
|
9
|
+
return array;
|
|
10
|
+
const pivot = array[0]; // 첫 요소 기준
|
|
11
|
+
const left = [];
|
|
12
|
+
const right = [];
|
|
13
|
+
for (let i = 1; i < array.length; i++) {
|
|
14
|
+
const current = array[i];
|
|
15
|
+
const shouldGoLeft = isAsc
|
|
16
|
+
? current.index < pivot.index
|
|
17
|
+
: current.index > pivot.index;
|
|
18
|
+
if (shouldGoLeft) {
|
|
19
|
+
left.push(current);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
right.push(current);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return [
|
|
26
|
+
...quickSort(left),
|
|
27
|
+
pivot,
|
|
28
|
+
...quickSort(right)
|
|
29
|
+
];
|
|
30
|
+
};
|
|
31
|
+
const sorted = quickSort(indexMap);
|
|
32
|
+
return sorted.map(v => v.value);
|
|
33
|
+
};
|
|
34
|
+
export default quick;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const selection = (arr, func, order) => {
|
|
2
|
+
const indexMap = arr.map(v => ({
|
|
3
|
+
index: func(v),
|
|
4
|
+
value: v
|
|
5
|
+
}));
|
|
6
|
+
const isAsc = order === "ascending";
|
|
7
|
+
for (let i = 0; i < indexMap.length - 1; i++) {
|
|
8
|
+
let targetIndex = i;
|
|
9
|
+
for (let j = i + 1; j < indexMap.length; j++) {
|
|
10
|
+
const current = indexMap[j];
|
|
11
|
+
const target = indexMap[targetIndex];
|
|
12
|
+
const shouldUpdate = isAsc
|
|
13
|
+
? current.index < target.index
|
|
14
|
+
: current.index > target.index;
|
|
15
|
+
if (shouldUpdate) {
|
|
16
|
+
targetIndex = j;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (targetIndex !== i) {
|
|
20
|
+
const temp = indexMap[i];
|
|
21
|
+
indexMap[i] = indexMap[targetIndex];
|
|
22
|
+
indexMap[targetIndex] = temp;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return indexMap.map(v => v.value);
|
|
26
|
+
};
|
|
27
|
+
export default selection;
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sortism",
|
|
3
|
+
"description": "The easiest way to sort an array.",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"version": "1.0.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"sort",
|
|
9
|
+
"algorithm"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/Pro203S/sortism#readme",
|
|
12
|
+
"scripts": {
|
|
13
|
+
"watch": "tsc --watch"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/Pro203S/sortism.git"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/bun": "latest"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"typescript": "^5"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import bubble from "./sorts/bubble";
|
|
2
|
+
import merge from "./sorts/merge";
|
|
3
|
+
import quick from "./sorts/quick";
|
|
4
|
+
import selection from "./sorts/selection";
|
|
5
|
+
|
|
6
|
+
export type InternalSortFunction = <T>(
|
|
7
|
+
arr: T[],
|
|
8
|
+
func: (v: T) => number,
|
|
9
|
+
order: "ascending" | "descending",
|
|
10
|
+
) => T[];
|
|
11
|
+
|
|
12
|
+
export type ExportSortFunction<T> = (
|
|
13
|
+
order: "ascending" | "descending",
|
|
14
|
+
func?: (v: T) => number,
|
|
15
|
+
) => T[];
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* You can use sortism through this function.
|
|
19
|
+
*/
|
|
20
|
+
export default function sortism<T = any>(array: T[], func?: (v: T) => number) {
|
|
21
|
+
const f = func ?? (v => v as number);
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
/**
|
|
25
|
+
* Time: O(n^2)
|
|
26
|
+
*/
|
|
27
|
+
"bubble": (order => bubble(array, f, order)) as ExportSortFunction<T>,
|
|
28
|
+
/**
|
|
29
|
+
* Time: O(n^2)
|
|
30
|
+
*/
|
|
31
|
+
"selection": (order => selection(array, f, order)) as ExportSortFunction<T>,
|
|
32
|
+
/**
|
|
33
|
+
* Time: O(n log n)
|
|
34
|
+
*/
|
|
35
|
+
"merge": (order => merge(array, f, order)) as ExportSortFunction<T>,
|
|
36
|
+
/**
|
|
37
|
+
* Time: O(n log n)
|
|
38
|
+
*/
|
|
39
|
+
"quick": (order => quick(array, f, order)) as ExportSortFunction<T>,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { InternalSortFunction } from "..";
|
|
2
|
+
|
|
3
|
+
const bubble: InternalSortFunction = (arr, func, order) => {
|
|
4
|
+
const indexMap = arr.map(v => ({
|
|
5
|
+
"index": func(v),
|
|
6
|
+
"value": v
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
const isAsc = order === "ascending";
|
|
10
|
+
|
|
11
|
+
for (let i = 0; i < indexMap.length - 1; i++) {
|
|
12
|
+
let swapped = false;
|
|
13
|
+
|
|
14
|
+
for (let j = 0; j < indexMap.length - 1 - i; j++) {
|
|
15
|
+
const current = indexMap[j]!;
|
|
16
|
+
const next = indexMap[j + 1]!;
|
|
17
|
+
|
|
18
|
+
const shouldSwap = isAsc
|
|
19
|
+
? current.index > next.index
|
|
20
|
+
: current.index < next.index;
|
|
21
|
+
|
|
22
|
+
if (shouldSwap) {
|
|
23
|
+
const temp = indexMap[j]!;
|
|
24
|
+
indexMap[j] = indexMap[j + 1]!;
|
|
25
|
+
indexMap[j + 1] = temp;
|
|
26
|
+
swapped = true;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!swapped) break;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return indexMap.map(v => v.value);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default bubble;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { InternalSortFunction } from "..";
|
|
2
|
+
|
|
3
|
+
const merge: InternalSortFunction = (arr, func, order) => {
|
|
4
|
+
const indexMap = arr.map(v => ({
|
|
5
|
+
index: func(v),
|
|
6
|
+
value: v
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
const isAsc = order === "ascending";
|
|
10
|
+
|
|
11
|
+
const mergeSort = (
|
|
12
|
+
array: typeof indexMap
|
|
13
|
+
): typeof indexMap => {
|
|
14
|
+
if (array.length <= 1) return array;
|
|
15
|
+
|
|
16
|
+
const mid = Math.floor(array.length / 2);
|
|
17
|
+
|
|
18
|
+
const left = mergeSort(array.slice(0, mid));
|
|
19
|
+
const right = mergeSort(array.slice(mid));
|
|
20
|
+
|
|
21
|
+
return mergeArray(left, right);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const mergeArray = (
|
|
25
|
+
left: typeof indexMap,
|
|
26
|
+
right: typeof indexMap
|
|
27
|
+
): typeof indexMap => {
|
|
28
|
+
const result: typeof indexMap = [];
|
|
29
|
+
|
|
30
|
+
let i = 0;
|
|
31
|
+
let j = 0;
|
|
32
|
+
|
|
33
|
+
while (i < left.length && j < right.length) {
|
|
34
|
+
const l = left[i]!;
|
|
35
|
+
const r = right[j]!;
|
|
36
|
+
|
|
37
|
+
const shouldTakeLeft = isAsc
|
|
38
|
+
? l.index <= r.index
|
|
39
|
+
: l.index >= r.index;
|
|
40
|
+
|
|
41
|
+
if (shouldTakeLeft) {
|
|
42
|
+
result.push(l);
|
|
43
|
+
i++;
|
|
44
|
+
} else {
|
|
45
|
+
result.push(r);
|
|
46
|
+
j++;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 남은 요소 처리
|
|
51
|
+
while (i < left.length) {
|
|
52
|
+
result.push(left[i]!);
|
|
53
|
+
i++;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
while (j < right.length) {
|
|
57
|
+
result.push(right[j]!);
|
|
58
|
+
j++;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return result;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const sorted = mergeSort(indexMap);
|
|
65
|
+
|
|
66
|
+
return sorted.map(v => v.value);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export default merge;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { InternalSortFunction } from "..";
|
|
2
|
+
|
|
3
|
+
const quick: InternalSortFunction = (arr, func, order) => {
|
|
4
|
+
const indexMap = arr.map(v => ({
|
|
5
|
+
index: func(v),
|
|
6
|
+
value: v
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
const isAsc = order === "ascending";
|
|
10
|
+
|
|
11
|
+
const quickSort = (
|
|
12
|
+
array: typeof indexMap
|
|
13
|
+
): typeof indexMap => {
|
|
14
|
+
if (array.length <= 1) return array;
|
|
15
|
+
|
|
16
|
+
const pivot = array[0]!; // 첫 요소 기준
|
|
17
|
+
const left: typeof indexMap = [];
|
|
18
|
+
const right: typeof indexMap = [];
|
|
19
|
+
|
|
20
|
+
for (let i = 1; i < array.length; i++) {
|
|
21
|
+
const current = array[i]!;
|
|
22
|
+
|
|
23
|
+
const shouldGoLeft = isAsc
|
|
24
|
+
? current.index < pivot.index
|
|
25
|
+
: current.index > pivot.index;
|
|
26
|
+
|
|
27
|
+
if (shouldGoLeft) {
|
|
28
|
+
left.push(current);
|
|
29
|
+
} else {
|
|
30
|
+
right.push(current);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return [
|
|
35
|
+
...quickSort(left),
|
|
36
|
+
pivot,
|
|
37
|
+
...quickSort(right)
|
|
38
|
+
];
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const sorted = quickSort(indexMap);
|
|
42
|
+
|
|
43
|
+
return sorted.map(v => v.value);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export default quick;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { InternalSortFunction } from "..";
|
|
2
|
+
|
|
3
|
+
const selection: InternalSortFunction = (arr, func, order) => {
|
|
4
|
+
const indexMap = arr.map(v => ({
|
|
5
|
+
index: func(v),
|
|
6
|
+
value: v
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
const isAsc = order === "ascending";
|
|
10
|
+
|
|
11
|
+
for (let i = 0; i < indexMap.length - 1; i++) {
|
|
12
|
+
let targetIndex = i;
|
|
13
|
+
|
|
14
|
+
for (let j = i + 1; j < indexMap.length; j++) {
|
|
15
|
+
const current = indexMap[j]!;
|
|
16
|
+
const target = indexMap[targetIndex]!;
|
|
17
|
+
|
|
18
|
+
const shouldUpdate = isAsc
|
|
19
|
+
? current.index < target.index
|
|
20
|
+
: current.index > target.index;
|
|
21
|
+
|
|
22
|
+
if (shouldUpdate) {
|
|
23
|
+
targetIndex = j;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (targetIndex !== i) {
|
|
28
|
+
const temp = indexMap[i]!;
|
|
29
|
+
indexMap[i] = indexMap[targetIndex]!;
|
|
30
|
+
indexMap[targetIndex] = temp;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return indexMap.map(v => v.value);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export default selection;
|
package/tests/index.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import sortism from './../dist';
|
|
2
|
+
|
|
3
|
+
const arr = [
|
|
4
|
+
{
|
|
5
|
+
"index": 0
|
|
6
|
+
},
|
|
7
|
+
{
|
|
8
|
+
"index": 2
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
"index": 2
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"index": 6
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"index": 4
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"index": 3
|
|
21
|
+
}
|
|
22
|
+
];
|
|
23
|
+
const sort = sortism(arr, (v) => v.index);
|
|
24
|
+
|
|
25
|
+
const result = sort.selection("ascending");
|
|
26
|
+
|
|
27
|
+
console.log(result);
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
// Environment setup & latest features
|
|
4
|
+
"lib": ["ESNext"],
|
|
5
|
+
"target": "ESNext",
|
|
6
|
+
"module": "Preserve",
|
|
7
|
+
"moduleDetection": "force",
|
|
8
|
+
"jsx": "react-jsx",
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
"rootDir": "./src",
|
|
11
|
+
"outDir": "./dist",
|
|
12
|
+
|
|
13
|
+
// Bundler mode
|
|
14
|
+
"moduleResolution": "bundler",
|
|
15
|
+
"verbatimModuleSyntax": true,
|
|
16
|
+
"declaration": true,
|
|
17
|
+
|
|
18
|
+
// Best practices
|
|
19
|
+
"strict": true,
|
|
20
|
+
"skipLibCheck": true,
|
|
21
|
+
"noFallthroughCasesInSwitch": true,
|
|
22
|
+
"noUncheckedIndexedAccess": true,
|
|
23
|
+
"noImplicitOverride": true,
|
|
24
|
+
|
|
25
|
+
// Some stricter flags (disabled by default)
|
|
26
|
+
"noUnusedLocals": false,
|
|
27
|
+
"noUnusedParameters": false,
|
|
28
|
+
"noPropertyAccessFromIndexSignature": false
|
|
29
|
+
},
|
|
30
|
+
"exclude": ["tests", "dist"]
|
|
31
|
+
}
|