Skip to content

Commit e932c32

Browse files
committed
JSDoc added to the algorithms
1 parent 5261c30 commit e932c32

6 files changed

Lines changed: 105 additions & 34 deletions

File tree

src/sorting/bubblesort.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
var array = [3,5,2,4,7,9,6,4,5];
22

3+
/**
4+
* The bubblesort algorithm. Complexity O(n^2).
5+
*
6+
* @public
7+
* @param {array} array Input array
8+
* @returns {array} array Sorted array
9+
*/
310
function bubbleSort(array) {
411
var temp;
512
for (var i = 0; i < array.length; i += 1) {

src/sorting/heapsort.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1+
var array = [3,4,6,2,3,6,8,9];
2+
3+
/**
4+
* The heapsort algorithm. It's complexity is O(nlog n).
5+
*
6+
* @public
7+
*/
18
var heapSort = (function () {
9+
10+
/**
11+
* Finds the correct place of given element in given max heap.
12+
*
13+
* @private
14+
* @param {array} array Array
15+
* @param {number} index Index of the element which palce in the max heap should be find
16+
*/
217
function heapify(array, index, heapSize) {
318
var left = 2 * index + 1,
419
right = 2 * index + 2,
@@ -18,13 +33,27 @@ var heapSort = (function () {
1833
}
1934
}
2035

36+
/**
37+
* Builds max heap from a given array.
38+
*
39+
* @private
40+
* @param {array} array Array which should be turned into max heap
41+
* @returns {array} array Array turned into max heap
42+
*/
2143
function buildMaxHeap(array) {
2244
for (var i = Math.floor(array.length / 2); i >= 0; i -= 1) {
2345
heapify(array, i, array.length);
2446
}
2547
return array;
2648
}
2749

50+
/**
51+
* Heapsort. Turns the input array into a max heap and after that sorts it.
52+
*
53+
* @public
54+
* @param {array} array Input array
55+
* @returns {array} array Sorted array
56+
*/
2857
return function (array) {
2958
var size = array.length,
3059
temp;
@@ -40,3 +69,4 @@ var heapSort = (function () {
4069
};
4170
}());
4271

72+
console.log(heapSort(array));

src/sorting/insertion-binary-sort.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
var array = [5,6,3,3,6,8,9,4,3];
22

3+
/**
4+
* Modified version of insertionsort. It uses binary search for finding
5+
* where the current element should be inserted. It's correct because
6+
* the binary search looks just in the first part of the array
7+
* which is actually sorted. It's complexity is O(n^2)
8+
*
9+
* @public
10+
* @param {array} array Input array
11+
* @param {array} array Sorted array
12+
*/
313
function insertionBinarySort(array) {
414
var current,
515
middle,

src/sorting/insertionsort.js

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
var array0 = [2,3,5,1,2,4,7,9,0,3,3];
2-
var array1 = [2,3,5,1,2,4,7,9,0,3,3];
3-
var array2 = [2,3,5,1,2,4,7,9,0,3,3];
4-
1+
var array = [2,3,5,1,2,4,7,9,0,3,3];
52

3+
/**
4+
* Insertionsort algorithm. It's complexity is O(n^2).
5+
*
6+
* @public
7+
* @param {array} array Input array
8+
* @returns {array} array Sorted array
9+
*/
610
function insertionSort(array) {
711
var current,
812
j;
@@ -18,32 +22,4 @@ function insertionSort(array) {
1822
return array;
1923
}
2024

21-
/* Works in JS because of the functional scope */
22-
function insertionSort2(array) {
23-
var key;
24-
for (var i = 0; i < array.length; i += 1) {
25-
key = array[i];
26-
for (var j = i - 1; j >= 0 && key < array[j]; j -= 1) {
27-
array[j + 1] = array[j];
28-
}
29-
array[j + 1] = key;
30-
}
31-
return array;
32-
}
33-
34-
/* Works in JS because of the functional scope */
35-
function insertionSortDesc(array) {
36-
var key;
37-
for (var i = 1; i < array.length; i += 1) {
38-
key = array[i];
39-
for (var j = i - 1; i >= 0 && array[j] < key; j -= 1) {
40-
array[j + 1] = array[j];
41-
}
42-
array[j + 1] = key;
43-
}
44-
return array;
45-
}
46-
47-
console.log(insertionSort(array0));
48-
console.log(insertionSort2(array1));
49-
console.log(insertionSortDesc(array2));
25+
console.log(insertionSort(array));

src/sorting/quicksort.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1+
var array = [3,4,7,3,3,5,8,3,34,3,7,9];
2+
3+
/**
4+
* The quicksort algorithm. It's complexity is O(nlog n).
5+
*
6+
* @public
7+
*/
18
var quickSort = (function () {
29

10+
/**
11+
* Partitions given subarray.
12+
*
13+
* @private
14+
* @param {array} array Input array
15+
* @param {number} left The start of the subarray
16+
* @param {number} right The end of the subarray
17+
*/
318
function partition(array, left, right) {
419
var cmp = array[right - 1],
520
minEnd = left,
@@ -14,13 +29,31 @@ var quickSort = (function () {
1429
return minEnd;
1530
}
1631

32+
/**
33+
* Swap the places of two elements
34+
*
35+
* @private
36+
* @param {array} array The array which contains the elements
37+
* @param {number} i The index of the first element
38+
* @param {number} j The index of the second element
39+
* @returns {array} array The array with swaped elements
40+
*/
1741
function swap(array, i, j) {
1842
var temp = array[i];
1943
array[i] = array[j];
2044
array[j] = temp;
2145
return array;
2246
}
23-
47+
48+
/**
49+
* Sorts given array.
50+
*
51+
* @private
52+
* @param {array} array Array which should be sorted
53+
* @param {number} left The start of the subarray which should be handled
54+
* @param {number} right The end of the subarray which should be handled
55+
* @returns {array} array Sorted array
56+
*/
2457
function quickSort(array, left, right) {
2558
if (left < right) {
2659
var p = partition(array, left, right);
@@ -30,8 +63,16 @@ var quickSort = (function () {
3063
return array;
3164
}
3265

66+
/**
67+
* Calls the quicksort function with it's initial values.
68+
*
69+
* @public
70+
* @param {array} array The input array which should be sorted
71+
* @returns {array} array Sorted array
72+
*/
3373
return function (array) {
3474
return quickSort(array, 0, array.length);
3575
};
3676
}());
3777

78+
console.log(quickSort(array));

src/sorting/recursive-insertionsort.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
var array = [4,6,2,2,4,56,7,7,51,23,5,7];
22

3+
/**
4+
* Recursive version of insertionsort. Complexity O(n^2).
5+
*
6+
* @public
7+
* @param {array} array Input array
8+
* @param {number} [max] Index of the element which place we should find in the current function call
9+
*/
310
function recursiveInsertionSort(array, max) {
411
if (max <= 0)
512
return array;

0 commit comments

Comments
 (0)