A collection of 24 array methods of QuickSort, InsertionSort and JS of front-end study notes (17)

Quick sort, insertion sort, array method

QuickSort

The
principle of recursive binary sorting (quick sort) : the
repeated operation is: constantly take the middle element, and then traverse the array, put the one smaller than the middle element on the left, and the one larger than the middle element on the right.
Through this operation, two arrays are separated, and the previous operations are performed on these two arrays, and the arrays are continuously separated, and the previous operations are performed on each array to decompose, until the division is indivisible, the array is sorted .

var arr = [81, 14, 32, 22, 16, 7, 1, 5, 9]
        function quickSort(arr){
    
    
            if(arr.length<1){
    
    
                return arr
            }
            var centerIndex = parseInt(arr.length/2)
            var center = arr.splice(centerIndex,1)[0]
            var left = []
            var right = []
            for(var i = 0 ; i < arr.length ; i++){
    
    
                if(arr[i] < center){
    
    
                    left.push(arr[i])
                }else{
    
    
                    right.push(arr[i])
                }
            }
            return quickSort(left).concat(center,quickSort(right))
        }
        var res = quickSort(arr)
        console.log(res)

InsertionSort

Insertion sorting principle:
first use the variable temp to store the value of the element arr[i] used to insert each time, then set j=i-1, j always represents the subscript of the previous element of the element used for insertion, then The next step is the key while loop, which is also the core idea of ​​insertion sort: as long as the front is bigger than me, I will go forward, until there is no bigger than me, I stop comparing.
Because j is always the subscript of the previous one, when you want to stop, just assign the value to arr[j+1] and you can stop.

var arr = [12,3,45,12,6]        
for(var i = 1 ; i < arr.length ; i++){
    
    
            var temp = arr[i]
            var j = i-1  //j表示用来插入的元素的前一位
            while(arr[j]>temp){
    
    
                arr[j+1] = arr[j]
                j--
            }
            arr[j+1]=temp
        }
        console.log(arr)

Detailed annotation version:
Insert picture description here

Array Methods

Basic method:
push() adds all the parameters to the end of the array in order
pop() deletes the last element of the
array shift() deletes the first element of
the array unshift() inserts some data at the front of the array

ES3.0 provides methods:
reverse () reverse the array
sort () array sort
splice () 1. intercept the array 2. replace the new content

The above seven all directly manipulate the original array, and the latter method does not change the original array.

concat() concatenate the array
slice() get some data in the array
join() connect each element in the array with the connection symbol

ES5.0 provides methods:
indexOf() view the index of the specified data in the array
lastIndexOf() reverse view the index of the specified data in the
array forEach() traverse the array
map() map the array
filter() filter the original array, and filter the original array Put it in the new array
every() to determine whether each of the original arrays meets the condition,
some() to determine whether there are any in the original array

After ES6:
find() finds the data that meets the conditions in the array according to the conditions.
findIndex() finds the subscripts of the data that meet the conditions in the array according to the conditions.
flat() flatten the array
flatMap() flatten the array, but only one layer of
fill () Use the specified data to fill the array
includes () Check whether there is a certain data in the
array copyWithin () Replace the contents of the array with the contents of the array

Guess you like

Origin blog.csdn.net/qq_42698576/article/details/107865902