Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f4dcea9

Browse files
Implemented insertion sort (#10 IP)
1 parent 3d67cd4 commit f4dcea9

File tree

4 files changed

+94
-4
lines changed

4 files changed

+94
-4
lines changed

‎Algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
### Sorting
2424
- [Array.prototype.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)
2525
- [Animated Sorting: Toptal](https://www.toptal.com/developers/sorting-algorithms)
26+
- [Dancing Algorithms: AlgoRythmics](https://www.youtube.com/user/AlgoRythmics/videos)
2627

2728
### Recursion
2829
- [Tail Call Optimization: ES6](https://2ality.com/2015/06/tail-call-optimization.html)

‎Algorithms/Sorting/InsertionSort.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import LinkedList from '../../Data-Structures/Linked-Lists/LinkedList.ts';
2+
3+
4+
function insertionSort(inputArr: number[] | string[]): Array<any> {
5+
const sorted = new LinkedList<any>();
6+
7+
sorted.append(inputArr[0]);
8+
9+
for (let i=1; i < inputArr.length; ++i) {
10+
11+
let currentElement = inputArr[i];
12+
let currentNode = sorted.getHead();
13+
14+
// Element is less than the lowest value
15+
if (currentElement < sorted.getHeadValue()) {
16+
sorted.prepend(currentElement);
17+
}
18+
// Element is greater than the highest value
19+
else if (currentElement > sorted.getTailValue()) {
20+
sorted.append(currentElement)
21+
}
22+
// Otherwise insert in its proper, sorted position
23+
else {
24+
for (let j=0; j < sorted.getLength()-1; ++j) {
25+
if (currentNode.getValue() < currentElement && currentNode.getNext().getValue() > currentElement) {
26+
sorted.insert(currentElement, j+1);
27+
break;
28+
}
29+
currentNode = currentNode.getNext();
30+
}
31+
}
32+
}
33+
return sorted.toArray();
34+
}
35+
36+
37+
//---------------------------------------------------------------------
38+
// ---------- MAIN PROGRAM ----------
39+
//---------------------------------------------------------------------
40+
if (import.meta.main) {
41+
42+
const numbers1 = [9,6,5,3,1,8,7,2,4];
43+
const numbers2 = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];
44+
const colors = ["white", "black", "green", "blue", "orange"];
45+
46+
console.log(insertionSort(numbers1));
47+
console.log(insertionSort(numbers2));
48+
console.log(insertionSort(colors));
49+
50+
// RUN: deno run Algorithms/Sorting/InsertionSort.ts
51+
}
52+
53+
// --------------------------- Terminal Output: ---------------------------
54+
// [
55+
// 1, 2, 3, 4, 5,
56+
// 6, 7, 8, 9
57+
// ]
58+
// [
59+
// 0, 1, 2, 4, 5,
60+
// 6, 44, 63, 87, 99,
61+
// 283
62+
// ]
63+
// [ "black", "blue", "green", "orange", "white" ]

‎Algorithms/Sorting/SelectionSort.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function swap(pos1: number, pos2: number, inputArr: Array<any>): void {
44
inputArr[pos2] = placeholder;
55
}
66

7-
function insertionSort(inputArr: Array<number> | Array<string>): Array<any> {
7+
function selectionSort(inputArr: Array<number> | Array<string>): Array<any> {
88

99
let minValue: number | string;
1010
let minIndex: number;
@@ -34,9 +34,9 @@ if (import.meta.main) {
3434
const numbers2 = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];
3535
const colors = ["white", "black", "green", "blue", "orange"];
3636

37-
console.log(insertionSort(numbers1));
38-
console.log(insertionSort(numbers2));
39-
console.log(insertionSort(colors));
37+
console.log(selectionSort(numbers1));
38+
console.log(selectionSort(numbers2));
39+
console.log(selectionSort(colors));
4040

4141
// RUN: deno run Algorithms/Sorting/SelectionSort.ts
4242
}

‎Data-Structures/Linked-Lists/LinkedList.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ export default class LinkedList<T> {
1515
return this.length;
1616
}
1717

18+
public getHead(): Node<T> | any {
19+
return this.head;
20+
}
21+
22+
public getHeadValue(): T | any {
23+
return this.head?.getValue();
24+
}
25+
26+
public getTailValue(): T | any {
27+
return this.tail?.getValue();
28+
}
29+
1830
public isEmpty(): boolean {
1931
return this.length === 0;
2032
}
@@ -166,6 +178,20 @@ export default class LinkedList<T> {
166178
return true;
167179
}
168180

181+
public toArray(): Array<T> | any {
182+
if (this.length <= 0) return null;
183+
184+
const array = new Array<T>(this.length);
185+
186+
let currentNode = this.head;
187+
188+
for (let i=0; !!currentNode; ++i) {
189+
array[i] = currentNode.getValue();
190+
currentNode = currentNode.getNext();
191+
}
192+
return array;
193+
}
194+
169195
public toString(nodesPerGroup?: number): string {
170196
if (this.length === 0 || !this.head) {
171197
return "";

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /