algorithm
An algorithm is a well-defined sequence of specific computational steps. An algorithm is a repeatable solution to a problem by a person or machine. Computer scientists use the complexity of the algorithm (also known as zero-notation) to indicate its efficiencyCopy the code
Look at the code to understand the algorithm
Find the smaller of the two numbers
Array [a,b] = 2; array [a,b] = 2; Expression:let minof2 = (numbers)=>{
if(numbers[0]<numbers[1]){
return numbers[0]
}else{
returnNumbers [1]}letminof2 = numbers => numbers[0]<numbers[1] ? Numbers [0] : Numbers [1] destructor assignment writingletminof2 =([a,b]) => a<b ? a : B call minof2([1,2]) // 1 Common call minof2.call(null,[1,2]) // 1 Call math.min (JS built-in API) math.min (1,2) // 1 Math.min.call(null,[1,2]) // 1 math.min. apply(null,[1,2]) // 1 Looks like Math is a constructor like ObjectCopy the code
The smallest of the three numbers
let minof3 = ([a,b,c])=> {
return minof2([minof2([a,b]),c])}
let minof3 = ([a,b,c])=> {
return minof2([a,minof2([b,c])])}
Copy the code
The smallest of the four numbers
let minof4 = ([a,b,c,d])=> {
return minof2([a,minof3([b,c,d])])}
Copy the code
Find the smallest of N numbers
let min = (numbers)=>{
if(numbers.length>2){
return min([numbers[0],min(numbers.slice(1))])
}else{
returnMath.min. Apply (null,numbers)}} numbers. Slice (null,numbers) applyCopy the code
recursive
A recursive function is called if the function itself is called directly or indirectly. A recursive function cannot be defined as an inline function that calls itself repeatedly, each time with slightly different parameters. When a simple condition is met, a simple call is implemented, and the final result is calculated. The code for finding the minimum value in any number of length is recursionCopy the code
Use substitution to understand recursion
,4,3,1,4,3,1 [2] min ([2]) = min ([2, min (,3,1 [4])]) = min ([2, min ([4 min ([3, 4]]]]]) = min2, min4, Math. Min. Apply (null, [3, 1]) = min2, (min4, 1) = min2, 1 = 1Copy the code
Sort an array of positive integers from smallest to largest (implemented recursively)
Select sort to sort an array of length 2let sort2=([a,b])=>{
if(a<b){
return [a,b]
}else{
return[b,a]}let sort2=([a,b])=> a<b ? [a,b] : [b,a]
Copy the code
Sort an array of length 3
let sort3 = (numbers) => {
let index =minIndex([a,b,c])
let min = numbers[index]
numbers.splice(index,x)
return[min]. Concat (sort2(numbers))} index (min, sort2(numbers)) Min (the first array),concat concatenation,sort2(numbers) the second array, get sorted new arrayCopy the code
Sort an array of length 4
let sort4 = (numbers) => {
let index =minIndex(numbers)
let min = numbers[index]
numbers.splice(index,1)
return [min].concat(sort3(numbers))
}
Copy the code
Sort an array of any length
let min = (numbers) =>{
if(numbers.length>2){
return min([numbers[0],min(numbers.slice(1))])
}else{
return Math.min.apply(null,numbers)
}
}
let minIndex = (numbers) => numbers.indexOf(min(numbers))
let sort = (numbers) => {
if(numbers.length>2){
let index =minIndex(numbers)
let min = numbers[index]
numbers.splice(index,1)
return [min].concat(sort(numbers))
}else{
returnnumbers[0]<numbers[1] ? Numbers: numbers. Revese ()}} numbers. Revese () take the opposite array of this array and switch a and BCopy the code
Sample,5,8,7,9 [12]
Sort (,5,8,7,8 [12]) = [5] + sort (,8,7,9 [12]) = [5] + [[7] + sort ([12.8.9])) = [5] + [[7] + [[8] + sort ([12, 9]))) (= [5] + [7] + [[8] + [[9, 12]))) =,7,8,9,12 [5]Copy the code