sortism 1.0.0 → 1.2.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/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # sortism
2
2
 
3
- ![license](https://img.shields.io/npm/l/sortism) ![stars](https://img.shields.io/github/stars/Pro203S/sortism)
3
+ [ENGLISH](./README_EN.md)
4
+
5
+ ![license](https://img.shields.io/npm/l/sortism)
6
+ ![stars](https://img.shields.io/github/stars/Pro203S/sortism)
4
7
 
5
8
  자신에게 맞는 방법으로 배열을 정렬해보세요!
6
9
  이 모듈에는 여러가지의 정렬 방식이 있습니다.
package/README_EN.md ADDED
@@ -0,0 +1,89 @@
1
+ # sortism
2
+
3
+ ![license](https://img.shields.io/npm/l/sortism)
4
+ ![stars](https://img.shields.io/github/stars/Pro203S/sortism)
5
+
6
+ Sort arrays in the way that suits you best!
7
+ This module provides various sorting methods.
8
+ Just choose the one that fits your needs.
9
+
10
+ ## Usage
11
+
12
+ 1. Install the package
13
+
14
+ ``` sh
15
+ npm i sortism
16
+
17
+ bun i sortism
18
+ ```
19
+
20
+ 2. Import the module
21
+
22
+ ``` typescript
23
+ import sortism from 'sortism';
24
+ ```
25
+
26
+ 3. Use sorting
27
+
28
+ ``` typescript
29
+ const arr = [5, 2, 3, 1, 4];
30
+
31
+ // Bubble sort, ascending
32
+ // 1,2,3,4,5
33
+ sortism(arr).bubble("ascending");
34
+
35
+ // Selection sort, descending
36
+ // 5,4,3,2,1
37
+ sortism(arr).selection("descending");
38
+ ```
39
+
40
+ ## Detailed Usage
41
+
42
+ - Sorting objects
43
+
44
+ ```typescript
45
+ const arr = [
46
+ {
47
+ "index": 2
48
+ },
49
+ {
50
+ "index": 0
51
+ },
52
+ {
53
+ "index": 2
54
+ },
55
+ {
56
+ "index": 6
57
+ },
58
+ {
59
+ "index": 4
60
+ },
61
+ {
62
+ "index": 3
63
+ }
64
+ ];
65
+
66
+ // Bubble sort, ascending
67
+ sortism(arr).bubble("ascending");
68
+
69
+ /*
70
+ returns:
71
+ [
72
+ {
73
+ index: 0,
74
+ }, {
75
+ index: 2,
76
+ }, {
77
+ index: 2,
78
+ }, {
79
+ index: 3,
80
+ }, {
81
+ index: 4,
82
+ }, {
83
+ index: 6,
84
+ }
85
+ ]
86
+ */
87
+ ```
88
+
89
+ Install it now and start using it!
package/dist/index.d.ts CHANGED
@@ -20,4 +20,20 @@ export default function sortism<T = any>(array: T[], func?: (v: T) => number): {
20
20
  * Time: O(n log n)
21
21
  */
22
22
  quick: ExportSortFunction<T>;
23
+ /**
24
+ * Time: O(n * n!)
25
+ */
26
+ bogo: ExportSortFunction<T>;
27
+ /**
28
+ * Time: O(N)
29
+ */
30
+ stalin: ExportSortFunction<T>;
31
+ /**
32
+ * Time: O(Infinity)
33
+ */
34
+ miracle: ExportSortFunction<T>;
35
+ /**
36
+ * Time: O(N)
37
+ */
38
+ thanos: ExportSortFunction<T>;
23
39
  };
package/dist/index.js CHANGED
@@ -1,7 +1,11 @@
1
+ import bogo from "./sorts/bogo";
1
2
  import bubble from "./sorts/bubble";
2
3
  import merge from "./sorts/merge";
4
+ import miracle from "./sorts/miracle";
3
5
  import quick from "./sorts/quick";
4
6
  import selection from "./sorts/selection";
7
+ import stalin from "./sorts/stalin";
8
+ import thanos from "./sorts/thanos";
5
9
  /**
6
10
  * You can use sortism through this function.
7
11
  */
@@ -24,5 +28,21 @@ export default function sortism(array, func) {
24
28
  * Time: O(n log n)
25
29
  */
26
30
  "quick": (order => quick(array, f, order)),
31
+ /**
32
+ * Time: O(n * n!)
33
+ */
34
+ "bogo": (order => bogo(array, f, order)),
35
+ /**
36
+ * Time: O(N)
37
+ */
38
+ "stalin": (order => stalin(array, f, order)),
39
+ /**
40
+ * Time: O(Infinity)
41
+ */
42
+ "miracle": (order => miracle(array, f, order)),
43
+ /**
44
+ * Time: O(N)
45
+ */
46
+ "thanos": (order => thanos(array, f, order)),
27
47
  };
28
48
  }
@@ -0,0 +1,3 @@
1
+ import type { InternalSortFunction } from "..";
2
+ declare const bogo: InternalSortFunction;
3
+ export default bogo;
@@ -0,0 +1,32 @@
1
+ const bogo = (arr, func, order) => {
2
+ const indexMap = arr.map(v => ({
3
+ index: func(v),
4
+ value: v
5
+ }));
6
+ const isAsc = order === "ascending";
7
+ const isSorted = (array) => {
8
+ for (let i = 0; i < array.length - 1; i++) {
9
+ const current = array[i];
10
+ const next = array[i + 1];
11
+ const isValid = isAsc
12
+ ? current.index <= next.index
13
+ : current.index >= next.index;
14
+ if (!isValid)
15
+ return false;
16
+ }
17
+ return true;
18
+ };
19
+ const shuffle = (array) => {
20
+ for (let i = array.length - 1; i > 0; i--) {
21
+ const j = Math.floor(Math.random() * (i + 1));
22
+ const temp = array[i];
23
+ array[i] = array[j];
24
+ array[j] = temp;
25
+ }
26
+ };
27
+ while (!isSorted(indexMap)) {
28
+ shuffle(indexMap);
29
+ }
30
+ return indexMap.map(v => v.value);
31
+ };
32
+ export default bogo;
@@ -0,0 +1,3 @@
1
+ import type { InternalSortFunction } from "..";
2
+ declare const miracle: InternalSortFunction;
3
+ export default miracle;
@@ -0,0 +1,22 @@
1
+ const miracle = (arr, func, order) => {
2
+ const indexMap = arr.map(v => ({
3
+ index: func(v),
4
+ value: v
5
+ }));
6
+ const isAsc = order === "ascending";
7
+ const isSorted = (array) => {
8
+ for (let i = 0; i < array.length - 1; i++) {
9
+ const current = array[i];
10
+ const next = array[i + 1];
11
+ const isValid = isAsc
12
+ ? current.index <= next.index
13
+ : current.index >= next.index;
14
+ if (!isValid)
15
+ return false;
16
+ }
17
+ return true;
18
+ };
19
+ while (!isSorted(indexMap)) { }
20
+ return indexMap.map(v => v.value);
21
+ };
22
+ export default miracle;
@@ -0,0 +1,3 @@
1
+ import type { InternalSortFunction } from "..";
2
+ declare const stalin: InternalSortFunction;
3
+ export default stalin;
@@ -0,0 +1,22 @@
1
+ const stalin = (arr, func, order) => {
2
+ const indexMap = arr.map(v => ({
3
+ index: func(v),
4
+ value: v
5
+ }));
6
+ const isAsc = order === "ascending";
7
+ if (indexMap.length === 0)
8
+ return [];
9
+ const result = [indexMap[0]];
10
+ for (let i = 1; i < indexMap.length; i++) {
11
+ const current = indexMap[i];
12
+ const last = result[result.length - 1];
13
+ const shouldKeep = isAsc
14
+ ? current.index >= last.index
15
+ : current.index <= last.index;
16
+ if (shouldKeep) {
17
+ result.push(current);
18
+ }
19
+ }
20
+ return result.map(v => v.value);
21
+ };
22
+ export default stalin;
@@ -0,0 +1,3 @@
1
+ import type { InternalSortFunction } from "..";
2
+ declare const thanos: InternalSortFunction;
3
+ export default thanos;
@@ -0,0 +1,33 @@
1
+ const thanos = (arr, func, order) => {
2
+ let indexMap = arr.map(v => ({
3
+ index: func(v),
4
+ value: v
5
+ }));
6
+ const isAsc = order === "ascending";
7
+ const isSorted = (array) => {
8
+ for (let i = 0; i < array.length - 1; i++) {
9
+ const current = array[i];
10
+ const next = array[i + 1];
11
+ const isValid = isAsc
12
+ ? current.index <= next.index
13
+ : current.index >= next.index;
14
+ if (!isValid)
15
+ return false;
16
+ }
17
+ return true;
18
+ };
19
+ const snap = (array) => {
20
+ const result = [];
21
+ for (let i = 0; i < array.length; i++) {
22
+ if (Math.random() < 0.5) {
23
+ result.push(array[i]);
24
+ }
25
+ }
26
+ return result;
27
+ };
28
+ while (!isSorted(indexMap)) {
29
+ indexMap = snap(indexMap);
30
+ }
31
+ return indexMap.map(v => v.value);
32
+ };
33
+ export default thanos;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "sortism",
3
3
  "description": "The easiest way to sort an array.",
4
4
  "main": "./dist/index.js",
5
- "version": "1.0.0",
5
+ "version": "1.2.0",
6
6
  "type": "module",
7
7
  "keywords": [
8
8
  "sort",
package/src/index.ts CHANGED
@@ -1,7 +1,11 @@
1
+ import bogo from "./sorts/bogo";
1
2
  import bubble from "./sorts/bubble";
2
3
  import merge from "./sorts/merge";
4
+ import miracle from "./sorts/miracle";
3
5
  import quick from "./sorts/quick";
4
6
  import selection from "./sorts/selection";
7
+ import stalin from "./sorts/stalin";
8
+ import thanos from "./sorts/thanos";
5
9
 
6
10
  export type InternalSortFunction = <T>(
7
11
  arr: T[],
@@ -37,5 +41,21 @@ export default function sortism<T = any>(array: T[], func?: (v: T) => number) {
37
41
  * Time: O(n log n)
38
42
  */
39
43
  "quick": (order => quick(array, f, order)) as ExportSortFunction<T>,
44
+ /**
45
+ * Time: O(n * n!)
46
+ */
47
+ "bogo": (order => bogo(array, f, order)) as ExportSortFunction<T>,
48
+ /**
49
+ * Time: O(N)
50
+ */
51
+ "stalin": (order => stalin(array, f, order)) as ExportSortFunction<T>,
52
+ /**
53
+ * Time: O(Infinity)
54
+ */
55
+ "miracle": (order => miracle(array, f, order)) as ExportSortFunction<T>,
56
+ /**
57
+ * Time: O(N)
58
+ */
59
+ "thanos": (order => thanos(array, f, order)) as ExportSortFunction<T>,
40
60
  };
41
61
  }
@@ -0,0 +1,42 @@
1
+ import type { InternalSortFunction } from "..";
2
+
3
+ const bogo: 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 isSorted = (array: typeof indexMap): boolean => {
12
+ for (let i = 0; i < array.length - 1; i++) {
13
+ const current = array[i]!;
14
+ const next = array[i + 1]!;
15
+
16
+ const isValid = isAsc
17
+ ? current.index <= next.index
18
+ : current.index >= next.index;
19
+
20
+ if (!isValid) return false;
21
+ }
22
+ return true;
23
+ };
24
+
25
+ const shuffle = (array: typeof indexMap): void => {
26
+ for (let i = array.length - 1; i > 0; i--) {
27
+ const j = Math.floor(Math.random() * (i + 1));
28
+
29
+ const temp = array[i]!;
30
+ array[i] = array[j]!;
31
+ array[j] = temp;
32
+ }
33
+ };
34
+
35
+ while (!isSorted(indexMap)) {
36
+ shuffle(indexMap);
37
+ }
38
+
39
+ return indexMap.map(v => v.value);
40
+ };
41
+
42
+ export default bogo;
@@ -0,0 +1,30 @@
1
+ import type { InternalSortFunction } from "..";
2
+
3
+ const miracle: 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 isSorted = (array: typeof indexMap): boolean => {
12
+ for (let i = 0; i < array.length - 1; i++) {
13
+ const current = array[i]!;
14
+ const next = array[i + 1]!;
15
+
16
+ const isValid = isAsc
17
+ ? current.index <= next.index
18
+ : current.index >= next.index;
19
+
20
+ if (!isValid) return false;
21
+ }
22
+ return true;
23
+ };
24
+
25
+ while (!isSorted(indexMap)) { }
26
+
27
+ return indexMap.map(v => v.value);
28
+ };
29
+
30
+ export default miracle;
@@ -0,0 +1,31 @@
1
+ import type { InternalSortFunction } from "..";
2
+
3
+ const stalin: 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
+ if (indexMap.length === 0) return [];
12
+
13
+ const result: typeof indexMap = [indexMap[0]!];
14
+
15
+ for (let i = 1; i < indexMap.length; i++) {
16
+ const current = indexMap[i]!;
17
+ const last = result[result.length - 1]!;
18
+
19
+ const shouldKeep = isAsc
20
+ ? current.index >= last.index
21
+ : current.index <= last.index;
22
+
23
+ if (shouldKeep) {
24
+ result.push(current);
25
+ }
26
+ }
27
+
28
+ return result.map(v => v.value);
29
+ };
30
+
31
+ export default stalin;
@@ -0,0 +1,44 @@
1
+ import type { InternalSortFunction } from "..";
2
+
3
+ const thanos: InternalSortFunction = (arr, func, order) => {
4
+ let indexMap = arr.map(v => ({
5
+ index: func(v),
6
+ value: v
7
+ }));
8
+
9
+ const isAsc = order === "ascending";
10
+
11
+ const isSorted = (array: typeof indexMap): boolean => {
12
+ for (let i = 0; i < array.length - 1; i++) {
13
+ const current = array[i]!;
14
+ const next = array[i + 1]!;
15
+
16
+ const isValid = isAsc
17
+ ? current.index <= next.index
18
+ : current.index >= next.index;
19
+
20
+ if (!isValid) return false;
21
+ }
22
+ return true;
23
+ };
24
+
25
+ const snap = (array: typeof indexMap): typeof indexMap => {
26
+ const result: typeof indexMap = [];
27
+
28
+ for (let i = 0; i < array.length; i++) {
29
+ if (Math.random() < 0.5) {
30
+ result.push(array[i]!);
31
+ }
32
+ }
33
+
34
+ return result;
35
+ };
36
+
37
+ while (!isSorted(indexMap)) {
38
+ indexMap = snap(indexMap);
39
+ }
40
+
41
+ return indexMap.map(v => v.value);
42
+ };
43
+
44
+ export default thanos;
package/tests/index.ts CHANGED
@@ -1,27 +1,8 @@
1
1
  import sortism from './../dist';
2
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);
3
+ const arr = [0,15,126,74,4,2234,235,23,574,4578,2];
4
+ const sort = sortism(arr);
24
5
 
25
- const result = sort.selection("ascending");
6
+ const result = sort.thanos("ascending");
26
7
 
27
8
  console.log(result);