/* * Class Seraching algorithms * Category:Search algorithms * Linear Search TODO ... * Binary Search * Tree Search * Genetic Algorithm * Combinatorial optimization * Hashing * Internet search algorithms * Metaheuristics * String matching algorithms * * Linear Search * Binary Search * Jump Search * Interpolation Search * Exponential Search * Sublist Search (Search a linked list in another list) * Fibonacci Search * The Ubiquitous Binary Search * Recursive program to linearly search an element in a given array * Recursive function to do substring search * Unbounded Binary Search Example (Find the point where a monotonically increasing function becomes positive first time) * * * Author: Amir Hatami */ var displaymode = "No"; // by defualt No details display var code = new Array(); module.exports = function(inputArray,searchElement,fromIndex,searchLong, outputOptions,displayStatus,callback) { arrLen = inputArray.length; searchElement = typeof searchElement !== 'undefined' ? searchElement : "" ; fromIndex = typeof fromIndex !== 'undefined' ? fromIndex : 0 ; searchLong = typeof searchLong !== 'undefined' ? searchLong : "END" ; outputOptions = typeof outputOptions !== 'undefined' ? outputOptions : ""; displaymode = displayStatus; try { //code = getArrayInfo(inputArray); // code[0]:max element Code[1]:bucket size square of lenght code[2] array lenght code[3] min element //if (displaymode=="Yes") console.log("max",code[0],"lenght square",code[1],"array lenght",code[2],"min",code[3]); if (!inputArray || arrLen === undefined) { throw new Error("Input array is not valid !"); // } else if (code[3] < 0 ) { // throw new Error("negative element not accepted ! "); } else callback(null, { linearSearch:function () { if (searchElement=="" ) throw new Error("There is nothing to serach for !"); if (displaymode=="Yes") console.log("Searching in Array",inputArray,"with Linear Search"); return linearSearchLocal(inputArray,searchElement,fromIndex,searchLong,arrLen,outputOptions); }, binarySearch:function () { if (searchElement=="" ) throw new Error("There is nothing to serach for !"); if (displaymode=="Yes") console.log("Searching in Array",inputArray,"with Binary Search"); return binarySearchLocal(inputArray,searchElement,fromIndex,searchLong,arrLen,outputOptions); }, defaultSearch:function () { if (searchElement=="" ) throw new Error("There is nothing to serach for !"); if (displaymode=="Yes") console.log("Searching in Array",inputArray,"with default Javascript Searching!"); return defaultSearchLocal(inputArray,searchElement,fromIndex,searchLong,arrLen,outputOptions); } }); } catch (error) { callback(error,null); } } /* * General Functions : * to be used by most of algoritmss * * getArrayInfo * sortNumber * * * * * Author: Pooya Hatami */ // Function to : // find bigest element // creat bucket size // find Array lenght // ******* original for Bead Sort but used by most of algoritms getArrayInfo = function(input) { var output = new Array(); var arrlen = input.length; var max = input[0]; var min = input[0]; for (i = 1; i < input.length; i++) { if (max < input[i]) { max = input[i]; } if (min > input[i]) { min = input[i]; } } output[0] = Math.floor(max); output[1] = Math.floor(Math.sqrt(input.length)); output[2] = arrlen; output[3] = Math.floor(min); output['max'] = Math.floor(max); output['bucketsizeoffer'] = Math.floor(Math.sqrt(input.length)); output['length'] = arrlen; output['min'] = Math.floor(min); //console.log(output); return output; } // Function to : // find bigest element // creat bucket size // find Array lenght // ******* original for Bead Sort but used by most of algoritms serachArrayOptions = function(outputOptions,arrlen) { outputOptions = typeof outputOptions !== 'undefined' ? outputOptions : ""; outputOptions=outputOptions.toLowerCase(); outputOptions=removeOrdinalNumberSuffix(outputOptions); //if (displaymode=="Yes") console.log(outputOptions); var isRange = /^\d{1,10}-\d{1,10}$/; var isCases = /^\d{1,10}&\d{1,10}/; var isNum = /^\d{1,10}$/; if (isRange.test(outputOptions)) { pulledOutNum = outputOptions.split("-"); optionResult = [parseInt(pulledOutNum[0]),parseInt(pulledOutNum[1])]; optionMessage = "return range of "; optionMessage += addOrdinalNumberSuffix(parseInt(pulledOutNum[0])); optionMessage += " to "; optionMessage += addOrdinalNumberSuffix(parseInt(pulledOutNum[1])); optionMessage += " matche elemets"; if (displaymode=="Yes") console.log("Yes this is a range : "+optionMessage); return [optionResult,optionMessage,'range']; } else if (isCases.test(outputOptions)) { pulledOutNum = outputOptions.split("&"); optionResult = []; optionMessage = "return "; perimeterStr = ""; for(i=0 ; i arrLen ) ? arrLen-fromIndex : searchLong ; if (fromIndex >= arrLen ) return -1; if (displaymode == "Yes") console.log(arrLen, searchElement,outputOptions); serachGuidance = serachArrayOptions(outputOptions,arrLen); if (displaymode == "Yes") console.log(serachGuidance[2],serachGuidance); switch(serachGuidance[2]) { case "one": strInfo = getArrayInfo(serachGuidance[0]); start = serachGuidance[0][0]; end = serachGuidance[0][1]; break; case "range": strInfo = getArrayInfo(serachGuidance[0]); start = strInfo['min']; end = strInfo['max']; break; case "cases": strInfo = getArrayInfo(serachGuidance[0]); start = strInfo['min']; end = strInfo['max']; break; default: start = 1 ; end = 1 ; } message = serachGuidance[1]; if (displaymode == "Yes") console.log("start: ",start," end: ",end,"fromIndex",fromIndex,"searchLong",searchLong, " Message:",message," arrLen:",arrLen); foundCounter = 1; foundTag = false; foundBuff = ""; var findings = new Array(); for (i = fromIndex; i < fromIndex+searchLong; i++) { // Return the index of the element if the element // is found if (input[i] == searchElement){ foundBuff = i; foundTag = true; if (start<=foundCounter && foundCounter<=end && serachGuidance[2]=='cases') { for(j=0; j