Skip to content
This repository was archived by the owner on Mar 27, 2022. It is now read-only.

Commit e469e09

Browse files
committed
feat: ✨ implement sorted array class
1 parent 0a9c109 commit e469e09

File tree

4 files changed

+190
-31
lines changed

4 files changed

+190
-31
lines changed

src/SortedArray.ts

Lines changed: 175 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import type { Index, Size, CompareFunction } from './types';
2-
import { binarySearch } from './binarySearch';
32
import { bubbleSort } from './bubbleSort';
3+
import { binarySearch } from "./binarySearch";
44

55
/**
66
*
77
*/
8-
export class SortedArray<T> {
9-
private array: T[] = [];
8+
export class SortedArray<T = number> {
9+
private readonly array: T[] = [];
10+
private readonly compare: CompareFunction<T>;
1011

1112
/**
1213
*
1314
* @param array -
1415
* @param compareFunction -
1516
*/
16-
constructor(array: T[], compareFunction?: CompareFunction<T>) {
17-
if (compareFunction) {
18-
this.compare = compareFunction;
19-
}
20-
17+
constructor(array: T[], compareFunction: CompareFunction<T>) {
18+
this.compare = compareFunction;
2119
this.array = bubbleSort(array, this.compare);
2220
}
2321

@@ -27,45 +25,194 @@ export class SortedArray<T> {
2725
* @param unique -
2826
*/
2927
insert(element: T, unique = true): Index {
30-
let pos = binarySearch(this.array, element, this.compare, true);
28+
let high = this.array.length - 1;
29+
let low = 0;
30+
let pos = -1;
31+
let index = 0;
32+
let ordering = 0;
33+
34+
while (high >= low) {
35+
index = (high + low) / 2 >>> 0;
36+
ordering = this.compare(this.array[index], element);
37+
if (ordering < 0) {
38+
low = index + 1;
39+
} else if (ordering > 0) {
40+
high = index - 1;
41+
} else {
42+
pos = index;
43+
break;
44+
}
45+
}
46+
47+
if (pos === -1) {
48+
pos = high;
49+
}
3150

3251
pos++;
3352

34-
let high = this.array.length - 1;
53+
high = this.array.length-1;
3554
while ((pos < high) && (this.compare(element, this.array[pos]) === 0)){
3655
pos++;
3756
}
3857

39-
let mid = this.array.length;
58+
index = this.array.length;
4059
this.array.push(element);
41-
while (mid > pos) {
42-
this.array[mid] = this.array[--mid];
60+
while (index > pos) {
61+
this.array[index] = this.array[--index];
4362
}
4463

4564
this.array[pos] = element;
4665

4766
return pos;
4867
}
49-
delete(element: T, all = true): Index {}
5068

51-
set(index: Index, element: T): T {}
52-
get(index: Index): T {}
69+
/**
70+
*
71+
* @param element -
72+
* @param all -
73+
*/
74+
delete(element: T, all = true): Index {
75+
const index = binarySearch(this.array, element, this.compare);
5376

54-
search(element: T): Index {}
55-
has(element: T): boolean {}
77+
if (index === -1) {
78+
return index;
79+
}
5680

57-
slice(from: Index, to = this.array.length): SortedArray<T> {}
58-
splice(from: Index, to = this.array.length): SortedArray<T> {}
59-
concat(array: T[] | SortedArray<T>): SortedArray<T>{}
60-
merge(array: T[] | SortedArray<T>): Size {}
81+
this.array.splice(index, 1);
6182

62-
peek(index: Index): T {}
63-
first(): T {}
64-
last(): T {}
83+
return index;
84+
}
6585

66-
join(separator: string): string {}
86+
/**
87+
*
88+
* @param index -
89+
*/
90+
get(index: Index): T {
91+
return this.array[index];
92+
}
6793

68-
toArray(): T[] {}
94+
/**
95+
*
96+
* @param element -
97+
*/
98+
search(element: T): Index {
99+
const index = binarySearch(this.array, element, this.compare);
69100

70-
private compare(a: T, b: T): number {}
101+
return index;
102+
}
103+
104+
/**
105+
*
106+
* @param element -
107+
*/
108+
has(element: T): boolean {
109+
const index = binarySearch(this.array, element, this.compare);
110+
111+
return (index !== -1);
112+
}
113+
114+
/**
115+
*
116+
* @param from -
117+
* @param to -
118+
*/
119+
slice(from: Index, to = this.array.length): SortedArray<T> {
120+
const slice = this.array.slice(from, to);
121+
const sortedArray = new SortedArray<T>(slice, this.compare);
122+
123+
return sortedArray;
124+
}
125+
126+
/**
127+
*
128+
* @param from -
129+
* @param to -
130+
*/
131+
splice(from: Index, to = this.array.length): SortedArray<T> {
132+
const splice = this.array.splice(from, to);
133+
const sortedArray = new SortedArray<T>(splice, this.compare);
134+
135+
return sortedArray;
136+
}
137+
138+
/**
139+
*
140+
* @param array -
141+
*/
142+
concat(array: T[] | SortedArray<T>): SortedArray<T>{
143+
const one = array instanceof SortedArray ? array.toArray() : array;
144+
const two = this.array.slice(0);
145+
const tree = one.concat(two);
146+
const sortedArray = new SortedArray<T>(tree, this.compare);
147+
148+
return sortedArray;
149+
}
150+
151+
/**
152+
*
153+
* @param array -
154+
*/
155+
merge(array: T[] | SortedArray<T>): Size {
156+
for (const element of array) {
157+
this.insert(element);
158+
}
159+
160+
return this.array.length;
161+
}
162+
163+
/**
164+
*
165+
*/
166+
first(): T | undefined {
167+
return this.array.at(0);
168+
}
169+
170+
/**
171+
*
172+
*/
173+
last(): T | undefined {
174+
return this.array.at(-1);
175+
}
176+
177+
/**
178+
*
179+
* @param separator
180+
*/
181+
join(separator: string): string {
182+
return this.array.join(separator);
183+
}
184+
185+
/**
186+
*
187+
*/
188+
toArray(): T[] {
189+
return this.array.slice(0);
190+
}
191+
192+
get length() {
193+
return this.array.length;
194+
}
195+
196+
[Symbol.iterator]() {
197+
let i = 0;
198+
const array = this.array;
199+
200+
return {
201+
next() {
202+
if (i < array.length - 1) {
203+
i = i + 1;
204+
205+
return {
206+
value: array[i],
207+
done: false
208+
}
209+
}
210+
211+
return {
212+
value: array[i],
213+
done: true
214+
}
215+
}
216+
}
217+
}
71218
}

src/binarySearch.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ export function binarySearch<T>(
1010
let high = array.length - 1;
1111
let low = 0;
1212
let pos = -1;
13+
let mid = 0;
14+
let ordering = 0;
1315

1416
while (high >= low) {
15-
const mid = (high + low) / 2 >>> 0;
16-
const ordering = compare(array[mid], element);
17+
mid = (high + low) / 2 >>> 0;
18+
ordering = compare(array[mid], element);
1719
if (ordering < 0) {
1820
low = mid + 1;
1921
} else if (ordering > 0) {

src/bubbleSort.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import type { CompareFunction } from './types';
22

3+
/**
4+
* Bubble sort.
5+
*
6+
* @param array -
7+
* @param compare -
8+
*
9+
* @returns - new sorted array.
10+
*/
311
export function bubbleSort<T>(array: T[], compare: CompareFunction<T>): T[] {
412
const sorted = array.slice(0);
513
let len = sorted.length ;

src/compareNumbers.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export function compareNumbers(a: number, b: number): number {
1+
import { CompareFunction } from "./types";
2+
3+
export const compareNumbers: CompareFunction<number> = (a, b) => {
24
if (a < b) {
35
return -1;
46
} else if (a > b) {

0 commit comments

Comments
 (0)