Sort array collapse array de redo callback function
Bubble sorting
significance
It is to adjust an out of order array into an ordered array through the execution of code
Logic:
1. Traverse the array, compare two by two, and move the relatively large number backward
2. After traversing, the result is that the largest number must be at the end
3. Repeat step 1 and continue the comparison. After two rounds, the two largest numbers must be in the last two positions
4. No matter how many numbers, Just compare the total length -1 round, Then the array is sorted
code
var arr = [ 9, 1, 2, 3, 4, 5, 6, 7, 8 ] // Purpose: [1, 2, 3, 4, 5, 6, 7, 8, 9] console.log('Original array : ', arr) for (var j = 0; j < arr.length - 1; j++) { for (var i = 0; i < arr.length - 1 - j; i++) { console.log('process : ', arr[i], ' > ', arr[i + 1], arr[i] > arr[i + 1]) if (arr[i] > arr[i + 1]) { var tmp = arr[i] arr[i] = arr[i + 1] arr[i + 1] = tmp tmp = null } } console.log('Once again : ', arr) } console.log('After sorting : ', arr)
Count sort
significance
An algorithm of array sorting
logic
1. Prepare a new array
(1) traverse the original array, take the original number as the index of the new array, and fill the new array with data
(2) the data of the new array is arranged by count
2. Traverse new array
(1) put the index into the original array as data
(2) premise: empty the original array
// Count sort var origin = [ 9, 3, 100, 6, 4, 100, 1, 9, 8, 7, 2, 2, 5, 100, 3, 32, 55 ] var newarr = [] // 1. Use data as an index for (var i = 0; i < origin.length; i++) { // origin[i] is data newarr[ origin[i] ] ? newarr[ origin[i] ]++ : newarr[ origin[i] ] = 1 } // 2. Index data in newarr // How many values are there in newarr // 2-1. Empty origin origin.length = 0 // 2-2. Fill back for (var i = 0; i < newarr.length; i++) { // i represents real data // newarr[i] indicates how many there are if (!newarr[i]) continue // Fill back // Fill newarr[i] I for (var k = 0; k < newarr[i]; k++) origin[origin.length] = i } console.log('After sorting : ', origin)
Select sort
Logic:
1. Traverse the array, find the smallest number in the array, and swap with the first number
2. From the second round, skip the first one and repeat step 1 for the rest
Round x | Outer loop control variable | hypothesis | Start of inner circulation | Swap | |
---|---|---|---|---|---|
1 | 0 | 0 | 1 | [0] and [minIndex] | |
2 | 1 | 1 | 2 | [1] And [minIndex] | |
3 | 2 | 2 | 3 | [2] And [minIndex] |
var arr = [ 8, 4, 6, 1, 3, 9, 5, 2, 7 ] console.log('Original array : ', arr) for (var j = 0; j < arr.length - 1; j++) { // Complete writing step 1 // hypothesis var minIndex = j // Start of inner circulation for (var i = j + 1; i < arr.length; i++) { if (arr[i] < arr[minIndex]) minIndex = i } // Swap if (j === minIndex) continue // When the code is executed here, it shows that j is different from minIndex var tmp = arr[j] arr[j] = arr[minIndex] arr[minIndex] = tmp tmp = null } console.log('After sorting : ', arr)
Array collapse
explain
1. When you delete the data in front of the array
2. Starting from the data you delete at the location, the index of all subsequent data will be decremented previously
3. We call this behavior array collapse
Problem solving direction
1. Don't collapse: delete from the back and loop the array upside down
2. Take the loop control variable one step back: i –
// Requirements: the array contains 4 data, which are traversed. The current one is deleted one at a time, and the array is deleted var arr = [ 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4 ] console.log('Original array : ', arr) // Collapse problem // for (var i = 0; i < arr.length; i++) { // //i is the index of the current data // //Aplice (start index, how many) // arr.splice(i, 1) // } // Solution 1 // Inverted loop array for (var i = arr.length - 1; i >= 0; i--) { // //i is still the index of each in the array, but it is inverted arr.splice(i, 1) } // Solution 2: // i-- // As long as you execute splice, execute an i-- for (var i = 0; i < arr.length; i++) { if (arr[i] % 2) { arr.splice(i, 1) i-- } } console.log('After deletion : ', arr)
Array de duplication: remove duplicates in the array
Scheme:
includes()
Syntax: array Includes (data)
Function: judge whether there is this data in the array
Return value: Boolean
true, indicating yes
false, indicating no
// Original array var arr = [ 1, 2, 3, 4, 3, 2, 1, 3, 4, 2, 1, 5, 6, 4, 3, 2, 5 ] console.log('Original array : ', arr) // Prepare an empty array // Traverse the original array and insert it into the new array in turn // Make a judgment before inserting. If there is this data in the new array, it will not be inserted. If there is no data, it will be inserted var newarr = [] for (var i = 0; i < arr.length; i++) { // //arr[i] is every data in the array // //If there is 1 after arr[1], delete one if (!newarr.includes( arr[i] )) { newarr.push(arr[i]) } } console.log('After weight removal : ', newarr)
Callback function callback
concept
1. Pass function A as an argument into function B
2. Call function A as A formal parameter in function B
At this point, we will say that function A is the callback function of function B
effect
1. Encapsulating and traversing related content
2. Encapsulating asynchronous related content