Skip to content

Commit 67ddd52

Browse files
authored
Merge pull request egonSchiele#4 from kevinwin/master
javascript code for grokking_algorithms [ch 2 - 9]
2 parents 0a19df0 + 8181b26 commit 67ddd52

File tree

22 files changed

+389
-0
lines changed

22 files changed

+389
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
// Finds the smallest value in an array
4+
function findSmallest(arr) {
5+
let smallest = arr[0]; // Stores the smallest value
6+
let smallest_index = 0; // Stores the index of the smallest value
7+
for (let i = 1; i < arr.length; i++) {
8+
if (arr[i] < smallest) {
9+
smallest = arr[i];
10+
smallest_index = i;
11+
}
12+
}
13+
return smallest_index;
14+
}
15+
16+
// Sort array
17+
function selectionSort(arr) {
18+
const newArr = [];
19+
for (let i = 0, length = arr.length; i < length; i++) {
20+
// Finds the smallest element in the array and adds it to the new array
21+
let smallest = findSmallest(arr);
22+
newArr.push(arr.splice(smallest, 1)[0]);
23+
}
24+
return newArr;
25+
}
26+
27+
console.log(selectionSort([5, 3, 6, 2, 10])); // [2, 3, 5, 6, 10]

03_recursion/01_countdown.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function countdown(i) {
2+
console.log(i);
3+
// base case
4+
if (i <= 0) {
5+
return;
6+
} else {
7+
countdown(i-1);
8+
}
9+
}
10+
11+
countdown(5);

03_recursion/02_greet.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function greet2(name) {
2+
console.log('how are you, ' + name + '?');
3+
}
4+
5+
function bye() {
6+
console.log('ok bye!');
7+
}
8+
9+
function greet(name) {
10+
console.log('hello, ' + name + '!');
11+
greet2(name);
12+
console.log('getting ready to say bye...');
13+
bye();
14+
}
15+
16+
greet('adit');

03_recursion/03_factorial.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function fact(x) {
2+
if (x === 1) {
3+
return 1;
4+
} else {
5+
return x * fact(x-1);
6+
}
7+
}
8+
9+
console.log(fact(5));

04_quicksort/01_loop_sum.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
function sum(arr) {
4+
let total = 0;
5+
for (let x = 0; x < arr.length; x++) {
6+
total += arr[x];
7+
}
8+
return total;
9+
}
10+
11+
console.log(sum([1, 2, 3, 4])) // 10

04_quicksort/02_recursive_sum.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
function sum(list) {
4+
if (list.length === 0) {
5+
return 0;
6+
}
7+
return list[0] + sum(list.slice(1));
8+
}
9+
10+
console.log(sum([1, 2, 3, 4])) // 10

04_quicksort/03_recursive_count.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
function count(list) {
4+
if (list.length === 0) {
5+
return 0;
6+
}
7+
return 1 + count(list.slice(1));
8+
}
9+
10+
console.log(count([0, 1, 2, 3, 4, 5])); // 6

04_quicksort/04_recursive_max.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
function max(list) {
4+
if (list.length === 2) {
5+
return list[0] > list[1] ? list[0] : list[1];
6+
}
7+
let sub_max = max(list.slice(1));
8+
return list[0] > sub_max ? list[0] : sub_max;
9+
}
10+
11+
console.log(max([1, 5, 10, 25, 16, 1])); // 25

04_quicksort/05_quicksort.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
function quicksort(array) {
4+
if (array.length < 2) {
5+
// base case, arrays with 0 or 1 element are already "sorted"
6+
return array;
7+
} else {
8+
// recursive case
9+
let pivot = array[0];
10+
// sub-array of all the elements less than the pivot
11+
let less = array.slice(1).filter(function(el) { return el <= pivot; });
12+
// sub-array of all the elements greater than the pivot
13+
let greater = array.slice(1).filter(function(el) { return el > pivot; });
14+
return quicksort(less).concat([pivot], quicksort(greater));
15+
}
16+
}
17+
18+
console.log(quicksort([10, 5, 2, 3])); // [2, 3, 5, 10]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
function sum(arr) {
4+
let total = 0;
5+
for (let x = 0; x < arr.length; x++) {
6+
total += arr[x];
7+
}
8+
return total;
9+
}
10+
11+
console.log(sum([1, 2, 3, 4])) // 10

0 commit comments

Comments
 (0)