Looking for general feedback and praise. This is for learning an not implementation as I would expect the built is sort algos to be much faster.
Addressed issues here:
/***************************************************************************************************
**ALGORITHMS
***************************************************************************************************/
// self used to hold client or server side global
(function (self) {
"use strict";
// holds (Pub)lic properties
var Pub = {},
// holds (Priv)ate properties
Priv = {},
// holds "imported" library properties
$A;
(function manageGlobal() {
// Priv.g holds the single global variable, used to hold all packages
Priv.g = '$A';
if (self[Priv.g] && self[Priv.g].pack && self[Priv.g].pack.utility) {
self[Priv.g].pack.algo = true;
$A = self[Priv.g];
} else {
throw new Error("algo requires utility module");
}
}());
Pub.swap = function (arr, i, j) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
};
// checks to see if sorted
Pub.isSorted = function (arr) {
var i,
length = arr.length;
for (i = 1; i < length; i++) {
if (arr[i] < arr[i - 1]) {
return false;
}
}
return true;
};
// repeatedly orders two items ( a bubble ) at a time
Pub.bubbleSort = function (arr) {
var index_outer,
index_inner,
swapped = false,
length = arr.length;
for (index_outer = 0; index_outer < length; index_outer++) {
swapped = false;
for (index_inner = 0; index_inner < length - index_outer; index_inner++) {
if (arr[index_inner] > arr[index_inner + 1]) {
Pub.swap(arr, index_inner, index_inner + 1);
swapped = true;
}
}
if (!swapped) {
break;
}
}
return arr;
};
// repeatedly finds minimum and places it the next index
Pub.selectionSort = function (arr) {
var index_outer,
index_inner,
index_min,
length = arr.length;
for (index_outer = 1; index_outer < length; index_outer++) {
index_min = index_outer;
for (index_inner = index_outer + 1; index_inner < length; index_inner++) {
if (arr[index_inner] < arr[index_min]) {
index_min = index_inner;
}
}
if (index_outer !== index_min) {
Pub.swap(arr, index_outer, index_min);
}
}
return arr;
};
// repeatedly places next item in correct spot using a "shift"
Pub.insertionSort = function (arr) {
var index_outer,
index_inner,
value,
length = arr.length;
for (index_outer = 0; index_outer < length; index_outer++) {
value = arr[index_outer];
// JavaScript optimization - index_inner >=0 is removed
// as the array index will return undefined for a negative index
for (index_inner = index_outer - 1; (arr[index_inner] > value);
index_inner--) {
arr[index_inner + 1] = arr[index_inner];
}
arr[index_inner + 1] = value;
}
return arr;
};
// module complete, release to outer scope
self[Priv.g] = $A.extendSafe(self[Priv.g], Pub);
}(this));
sort()
, and if not, what is the purpose of this library ? \$\endgroup\$