In web development, JavaScript is important and algorithms are important. Here's a look at some of the common algorithms implemented under JavaScript, including dichotomy, string length, array de-weight, insert sort, select Sort, hill sort, quick sort, bubble method, and so on. Just for the sake of practiced hand, does not guarantee the efficiency and the beauty, perhaps also has the bug, has the time to complete again. Ruyang County First High School
Two-part method:
function binary (items,value) {var startindex=0, stopindex=items.length-1, midlleindex= (startindex+ Stopindex) >>>1; while (Items[middleindex]!=value && startindex<stopindex) { if (items[middleindex]>value) { stopindex=middleindex-1; } else{ startindex=middleindex+1; } middleindex= (Startindex+stopindex) >>>1; } Return items[middleindex]!=value? False:true;}
Random generation of hexadecimal color values:
function Randomcolor () {var arrhex=["0", "2", "3", "4", "5", "6", "7", "8", "9", "a", "B", "C", "D"], strhex= "#", index; for (Var i=0;i < 6; i++) { index=math.round (math.random () *15); Strhex+=arrhex[index]; } return strhex;}
A method for finding the length of a string:
function GetBytes (str) {var len=str.length, Bytes=len; for (Var i=0;i < len;i++) { if (str. charcodeat>255) { bytes++;}} return bytes;}
JS implements the array de-weight:
Array.protype.delrepeat=function () { var newarray=new Array (); var len=this.length; for (Var i=0;i < len;i++) {for (var j=i+1;j < len;j++) { if (This[i]==this[j]) { ++i; } } Newarray.push (This[i]); } return NewArray;}
Insert Sort. The so-called insertion order is to treat the first element in the sequence as an ordered subsequence, and then compare the interchange to compare the exchange between the two.
function Insertsort (arr) { var key; for (var j = 1; j < Arr.length; J + +) { //well-ordered var i = j-1; key = Arr[j]; while (i >= 0 && arr[i] > key) { Arr[i + 1] = Arr[i]; I--; } Arr[i + 1] = key; } return arr;}
Select Sort. In fact, the basic idea is to select the smallest or largest from the array to be sorted, put it in the starting position, and then choose the smallest or largest number of rows from the remaining array.
function Selectionsort (data) {var I, j, Min, temp, count=data.length;for (i = 0; i < count-1; i++) {/ * Find the Minimum */ min = i; for (j = i+1; J < Count; J + +) { if (Data[j] < data[min]) {min = j;} } /* Swap data[i] and data[min] */ temp = data[i]; Data[i] = data[min]; Data[min] = temp;} return data;}
Hill sort, also called descending incremental sorting algorithm. In fact, it is also a variant of the insertion sort.
function Shellsort (array) {var Steparr = [1750, 701, 301, 1, 4,];//reverse () on the wiki see this optimal step size of the smaller array var i = 0; var steparrlength = steparr.length; var len = array.length; var len2 = parseint (LEN/2); for (; i < steparrlength; i++) {if (Steparr[i] > Len2) {continue; } stepsort (Steparr[i]); }//Sort one step function stepsort (step) {//console.log (step) using the step count var i = 0, j = 0, F , TEM, key; var Steplen = len%step > 0? parseint (len/step) + 1:len/step; for (; i < step; i++) {//Loop column for (J=1;/*j < Steplen && */step * j + i < Len; j + +) {//loop each column sequentially Tem per line = f = Step * j + i; key = Array[f]; while ((Tem-=step) >= 0) {//up-looking for if (Array[tem] > key) {array[tem+ Step] = Array[tem]; }else{break; }} Array[tem + step] = key; }}} return array;}
Quick Sort. In fact, in the final analysis, the fast sorting algorithm is a kind of improvement of the bubble sort, which is the idea of dividing and administering recursion in the algorithm theory, and the way to say it is that it is to divide the records to be sorted into two parts through a sort of sequencing, and some of the recorded values are smaller than the other part of the records. You can proceed to sort the two parts of the record separately, and implement the above two operations in a recursive order, thus realizing the sorting of the record values.
function QuickSort (arr,l,r) {if (L < R) {var mid=arr[parseint ((l+r)/2)],i=l-1,j=r+1;while (True) {while (Arr[++i] < mid); while (arr[--j]>mid); if (i>=j) Break;var temp=arr[i];arr[i]=arr[j];arr[j]=temp;} QuickSort (arr,l,i-1); QuickSort (arr,j+1,r);} return arr;}
Bubbling method:
function Bullsort (array) {var temp;for (var i=0;i < array.length;i++) {for (var j=array.length-1;j > i;j--) { if (Array[j] < array[j-1]) {temp = array[j];array[j]=array[j-1];array[j-1]=temp; }} } return array;
JavaScript implementations of some common algorithms