Skip to content

Commit 3d67cd4

Browse files
Implemented primitive sorts (#10 IP)
Implemented Bubble and Selection sort. Added some README resources.
1 parent 3ab0f66 commit 3d67cd4

File tree

8 files changed

+133
-1
lines changed

8 files changed

+133
-1
lines changed

Algorithms/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717

1818
## Resources
1919
- [Visualizing Data Structures & Algorithms](https://visualgo.net/en)
20+
- [Unicode Characters: RapidTables](https://www.rapidtables.com/code/text/unicode-characters.html)
21+
- [The Big-O Algorithm Complexity Cheat Sheet](https://www.bigocheatsheet.com/ "Big O Cheat Sheet")
22+
23+
### Sorting
24+
- [Array.prototype.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)
25+
- [Animated Sorting: Toptal](https://www.toptal.com/developers/sorting-algorithms)
2026

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

Algorithms/Sorting/Basics.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const spanish = ['único', 'árbol', 'cosas', 'fútbol'];
2+
3+
spanish.sort(function(a,b) {
4+
return a.localeCompare(b, 'es');
5+
});
6+
7+
console.log(spanish);
8+
9+
// OUTPUT: [ "cosas", "fútbol", "árbol", "único" ]
10+
11+
// RUN: deno run Algorithms/Sorting/Basics.ts

Algorithms/Sorting/BubbleSort.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
function bubbleSort(numbersArr: Array<number>) {
2+
3+
for (let i=0; i < numbersArr.length-1; ++i) {
4+
for (let j=0; j < numbersArr.length-1-i; ++j) {
5+
if (numbersArr[j] > numbersArr[j+1]) {
6+
numbersArr[j+1] = numbersArr[j] + numbersArr[j+1];
7+
numbersArr[j] = numbersArr[j+1] - numbersArr[j];
8+
numbersArr[j+1] = numbersArr[j+1] - numbersArr[j];
9+
}
10+
}
11+
}
12+
return numbersArr;
13+
}
14+
15+
function bubbleSortStrings(stringArr: Array<string>) {
16+
17+
let placeholder: string;
18+
19+
for (let i=0; i < stringArr.length-1; ++i) {
20+
for (let j=0; j < stringArr.length-1-i; ++j) {
21+
if (stringArr[j] > stringArr[j+1]) {
22+
placeholder = stringArr[j];
23+
stringArr[j] = stringArr[j+1];
24+
stringArr[j+1] = placeholder;
25+
}
26+
}
27+
}
28+
return stringArr;
29+
}
30+
31+
32+
//---------------------------------------------------------------------
33+
// ---------- MAIN PROGRAM ----------
34+
//---------------------------------------------------------------------
35+
if (import.meta.main) {
36+
37+
const numbers1 = [9,6,5,3,1,8,7,2,4];
38+
const numbers2 = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];
39+
const colors = ["white", "black", "green", "blue", "orange"];
40+
41+
console.log(bubbleSort(numbers1));
42+
console.log(bubbleSort(numbers2));
43+
console.log(bubbleSortStrings(colors));
44+
45+
// RUN: deno run Algorithms/Sorting/BubbleSort.ts
46+
}
47+
48+
// --------------------------- Terminal Output: ---------------------------
49+
// [
50+
// 1, 2, 3, 4, 5,
51+
// 6, 7, 8, 9
52+
// ]
53+
// [
54+
// 0, 1, 2, 4, 5,
55+
// 6, 44, 63, 87, 99,
56+
// 283
57+
// ]
58+
// [ "black", "blue", "green", "orange", "white" ]

Algorithms/Sorting/InsertionSort.ts

Whitespace-only changes.

Algorithms/Sorting/MergeSort.ts

Whitespace-only changes.

Algorithms/Sorting/QuickSort.ts

Whitespace-only changes.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
function swap(pos1: number, pos2: number, inputArr: Array<any>): void {
2+
const placeholder = inputArr[pos1];
3+
inputArr[pos1] = inputArr[pos2];
4+
inputArr[pos2] = placeholder;
5+
}
6+
7+
function insertionSort(inputArr: Array<number> | Array<string>): Array<any> {
8+
9+
let minValue: number | string;
10+
let minIndex: number;
11+
12+
for (let i=0; i < inputArr.length-1; ++i) {
13+
minValue = inputArr[i];
14+
minIndex = i;
15+
for (let j=i+1; j <= inputArr.length-1; ++j) {
16+
if (inputArr[j] < minValue) {
17+
minValue = inputArr[j];
18+
minIndex = j;
19+
}
20+
}
21+
swap(i, minIndex, inputArr);
22+
}
23+
24+
return inputArr;
25+
}
26+
27+
28+
//---------------------------------------------------------------------
29+
// ---------- MAIN PROGRAM ----------
30+
//---------------------------------------------------------------------
31+
if (import.meta.main) {
32+
33+
const numbers1 = [9,6,5,3,1,8,7,2,4];
34+
const numbers2 = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];
35+
const colors = ["white", "black", "green", "blue", "orange"];
36+
37+
console.log(insertionSort(numbers1));
38+
console.log(insertionSort(numbers2));
39+
console.log(insertionSort(colors));
40+
41+
// RUN: deno run Algorithms/Sorting/SelectionSort.ts
42+
}
43+
44+
// --------------------------- Terminal Output: ---------------------------
45+
// [
46+
// 1, 2, 3, 4, 5,
47+
// 6, 7, 8, 9
48+
// ]
49+
// [
50+
// 0, 1, 2, 4, 5,
51+
// 6, 44, 63, 87, 99,
52+
// 283
53+
// ]
54+
// [ "black", "blue", "green", "orange", "white" ]

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,7 @@
3636
- [Comprehensive List of Data Structures](https://en.wikipedia.org/wiki/List_of_data_structures "Wikipedia: DS List")
3737
- [Visualizing Data Structures & Algorithms](https://visualgo.net/en)
3838
- [The Big-O Algorithm Complexity Cheat Sheet](https://www.bigocheatsheet.com/ "Big O Cheat Sheet")
39-
- [Roadmap: Core Data Structures & Algorithms](https://coggle.it/diagram/W5E5tqYlrXvFJPsq/t/master-the-interview-click-here-for-course-link "Course and Mindmap by Andrei Neagoie")
39+
- [Roadmap: Core Data Structures & Algorithms](https://coggle.it/diagram/W5E5tqYlrXvFJPsq/t/master-the-interview-click-here-for-course-link "Course and Mindmap by Andrei Neagoie")
40+
41+
### Curated Articles
42+
- [Algorithms & DS – Real Use Cases: PragmaticEngineer](https://blog.pragmaticengineer.com/data-structures-and-algorithms-i-actually-used-day-to-day/)

0 commit comments

Comments
 (0)