/ Web Design

# Algorithms In Javascript

Going through the Kahn Academy Computer Science Algorithms. All code is released under the MIT license.

Binary Search Of Array

Finding a numerical search target in an array by binary search. This means that each time the search is performed, the algorithm splits the guess in half just like when you play the "number guessing game."

For example, pick a number from 1 to 20 (answer is 14).

"10."

"Too low."

"15."

"Too high."

"13."

"Too low."

"14."

"Got it."

Usually more efficient than going linearly (i.e. - "1,2,3,4,5 . . ."). Here's a thought, if you want to mess with a friend that's picking a number, choose "2." Probably, will take them longer. Anyway, here's the binary search algorithm. It finds the spot in the array where the targeted value is located.

/* Returns either the index of the location in the array,
or -1 if the array did not contain the targetValue */

var doSearch = function(array, targetValue) {
var min = 0; //starts with 0 because the first item in an array begins at spot #0 (i.e. - 5 items in array are in spots "0,1,2,3,4".
var max = array.length - 1; //nameOfArray.length gives # of items (elements) in the array. The number is length minus one because numbering on arrays begin at "0" and not "1".
var guess;
while(max >= min){
guess =  Math.floor((min+max)/2); //Math.floor rounds down to integer.
if(array[guess]===targetValue){
return guess;
}
else if(array[guess]<targetValue){
min = guess+1;
}
else {
max = guess-1;
}
}
return -1;
};


Finding Index Of Minimum In SubArray
The purpose of this algorithm is to find the spot where the minimum value of the array is located

 var indexOfMinimum = function(array, startIndex) {
// Set initial values for minValue and minIndex,
// based on the leftmost entry in the subarray:
var minValue = array[startIndex];

var minIndex = startIndex;

// Loop over items starting with startIndex,
// updating minValue and minIndex as needed:
for (var i = minIndex+1;i<array.length;i++){
if (array[i]<minValue){
minIndex = i;
minValue = array[i];
}
}
return minIndex;
};


Selection Sort

var swap = function(array, firstIndex, secondIndex) {
var temp = array[firstIndex];
array[firstIndex] = array[secondIndex];
array[secondIndex] = temp;
};

var indexOfMinimum = function(array, startIndex) {

var minValue = array[startIndex];
var minIndex = startIndex;

for(var i = minIndex + 1; i < array.length; i++) {
if(array[i] < minValue) {
minIndex = i;
minValue = array[i];
}
}
return minIndex;
};

var selectionSort = function(array) {
var  minIndex;
for (var i=0;i<array.length;i++){
minIndex=indexOfMinimum(array,i);
swap(array,i,minIndex);
}
};

var array = [22, 11, 99, 88, 9, 7, 42];
selectionSort(array);
println("Array after sorting:  " + array);



Different Ways To Sum Arithmetic Series

**Formula**
var array = [1,2,3,4,5,6,7,8,9,10,11,12,13];
var n =array.length;
var sum = (n*n)/2+n/2;

**For Loop**
sum2 = 0;
var n =array.length;
for (var i=0;i<n;i++){
var sum2 = sum2 + array[i];
}