Knowledge Expansion:
Time complexity: The time complexity of the algorithm is a function, which describes the running time of the algorithm. The lower the complexity of time, the higher the efficiency.
Self-understanding: An algorithm, run a few times the complexity of how many, such as the operation of N times, the time complexity of O (n).
1. Bubble sort
Resolution: 1. Compare adjacent two elements, and swap positions if the previous one is larger than the latter.
2. The last element of the first round should be the largest one.
3. Follow the steps one to compare the adjacent two elements, this time because the last element is already the largest, so the last element does not have to be compared.
function sort (elements) {for
(Var i=0;i<elements.length-1;i++) {for
(var j=0;j<elements.length-i-1;j+ +) {
if (elements[j]>elements[j+1]) {
var swap=elements[j];
ELEMENTS[J]=ELEMENTS[J+1];
Elements[j+1]=swap
}
}}} var elements = [3, 1, 5, 7, 2, 4, 9, 6, 8];
Console.log (' before: ' + elements);
Sort (elements);
Console.log (' after: ' + elements);
2. Quick Sort
Parsing: A quick sort is an improvement to the bubble sort, which divides the data into two parts, some of which are smaller than all the data in the other part. Then recursive invocation, on both sides of the implementation of a quick sort.
function QuickSort (elements) {
if (elements.length <= 1) {return elements;}
var pivotindex = Math.floor (ELEMENTS.LENGTH/2);
var pivot = Elements.splice (pivotindex, 1) [0];
var left = [];
var right = [];
for (var i = 0; i < elements.length i++) {
if (Elements[i] < pivot) {
Left.push (elements[i));
else {
Right.push (elements[i]);
}
return
QuickSort (left). Concat ([pivot], quickSort (right);
};
var elements=[5,6,2,1,3,8,7,1.2,5.5,4.5];
Alert (QuickSort (elements));
3. Insert Sort
Analytical:
(1) Starting with the first element, the element can be considered to have been sorted
(2) Take out the next element and scan backwards in the sorted sequence of elements
(3) If the element (sorted) is greater than the new element, move the element to the next position
(4) Repeat step 3 until you find the sorted element is less than or equal to the new element's position
(5) Insert the new element into the next position
(6) Repeat step 2
Insertsort:function (elements) {
var i = 1,
j, step, key, Len = Elements.length;
for (; i < Len; i++) {Step
= j = i;
key = Elements[j];
while (--j >-1) {
if (Elements[j] > key) {
Elements[j + 1] = Elements[j];
} else {break
;
}
}
Elements[j + 1] = key;
}
return elements;
}
2. Two-point search
Parsing: Two-point lookup, also for binary lookup. First you have to find an intermediate value, by comparing it to the median, the big one and the small to the left. Look for intermediate values on both sides and continue until you find the location.
(1) Recursive method
function BinarySearch (data,item,start,end) {
var end=end | | data.length-1;
var Start=start | | 0;
var M=math.floor ((start+end)/2);
if (Item==data[m]) {return
m;
} else if (item<data[m) {return
BinarySearch (data,item,start,m-1)//Recursive call}else{return
BinarySearch ( data,item,m+1,end);
}
return false;
}
var arr=[34,12,5,123,2,745,32,4];
Binary (arr,5);
(2) Non-recursive method
function BinarySearch (data, item) {
var h = data.length-1,
l = 0;
while (l <= h) {
var m = Math.floor ((H + L)/2);
if (data[m] = = Item) {return
m;
}
if (item > Data[m]) {
L = m + 1;
} else{
h = m-1;
}
}
return false;
}
var arr=[34,12,5,123,2,745,32,4];
BinarySearch (arr,5);
The above is a small series for everyone to bring the JS basic algorithm: Bubble sort, a simple example of the second find all the content, I hope that many support cloud Habitat Community ~