Fibonacci Sequence:
The Fibonacci sequence, also known as the Golden Section, refers to a sequence of numbers 0, 1, 1, 2, 3, 5, 8, 13, and 、...... In mathematics, the Fibonacci sequence is defined recursively as follows: F0=0,f1=1,fn=f (n-1) +f (n-2) (n>=2,n∈n*) in modern physics, quasi-crystal structure, chemistry and other fields, the Fibonacci sequence has a direct application, for this reason, From 1963, the American Mathematical Council published a mathematical magazine in the name of the Fibonacci series Quarterly, devoted to the research.
This sequence starts with the second item and each item is equal to the sum of the first two items.
function ABC (num) { if (num<=2) { return 1; } Return ABC (NUM-1) +abc (num-2); } A simple example.
It is implemented using recursion.
Schematic diagram of recursion:
is to implement the process of recursion, and then realize the return process.
Bubble Sort:
- compare adjacent elements. If the first one is bigger than the second one, swap them both.
-
-
- Repeats the above steps each time for fewer elements, until there is no pair of numbers to compare.
var arr=[2,5,1,0,10,20,4];function ABC2 (arr) {for (var i=0;i<arr. length;i++) {for (Var j=0;j<arr.length-1;j++) { arr=tocon (arr,j) } } return arr;} function Tocon (arr,index) { if (Arr[index]>arr[index+1]) { var a=arr[index+1]; Arr[index+1]=arr[index]; arr[index]=a; } return arr;} Alert (ABC2 (arr))
Select Sort:
Select sort (Selection sort) is a simple and intuitive sort of selection. It works by selecting the smallest (or largest) element of the data element to be sorted each time, storing it at the beginning of the sequence until all the data elements to be sorted are exhausted.
function Selects (arr) { if (arr.length==1) { return arr; } var min=arr[0]; var index=0; for (Var i=0;i<arr. length;i++) { if (min>arr[i]) { Min=arr[i]; index=i; } } var a=arr.splice (index,1); Return A.concat (Selects (arr));} Alert (Selects (arr));
Array de-weight
is to remove the duplicate values from the array
var arr2=[0,0,0,1,2,3,4,5,5,12,1,2];function delc (arr) { var obj={}; var newarr=[]; for (Var i=0;i<arr. length;i++) { if (!obj[arr[i]]) { obj[arr[i]]=1; Newarr.push (Arr[i]); } } return NEWARR;} Alert (DELC (ARR2))
Fibonacci sequence, bubble sort, select Sort, array go heavy