// Bubble sortfunction bubbleSort(arr){
	for(var i=0; i<arr.length; I++){// the outer layer controls the number of cyclesfor(var j=0; j<arr.length-1-i; J++){// switch between the number of inner loop controlsif(arr[j+1]<arr[j]){ var temp; Temp =arr[J]; arr[j]=arr[j+1]; arr[j+1]=temp; }}}return arr;
	}
Copy the code
// merge sortfunction merge(leftArr,rightArr){
        while(leftArr.length&&rightArr.length){
            if(leftArr[0]<rightArr[0]){
                result.push(leftArr.shift())
            }else{
                result.push(rightArr.shift())
            }
        }
        return result.concat(leftArr).concat(rightArr)
    }
    function mergeSort(arr){
        if(arr.length===1){
            return arr;
        }
        var middleIndex=Math.floor(arr.length/2)
        var left=arr.slice(0,middleIndex);
        var right=arr.slice(middleIndex)
        return merge(mergeSort(left),mergeSort(right))
    }
Copy the code
// QuicksortfunctionquickSort(arr){ var middleIndex=Math.floor(arr.length/2); Var middle=arr.splice(middleIndex,1); Var left=[]; var right=[];for(var i=0; i<arr.length; i++){if(arr[i]<middle){
                left.push(arr[i])
            }else{
                right.push(arr[i])
            }
        }
        return quickSort(left).concat(middle,quickSort(right));
    }
Copy the code
// recursive searchfunction binaraySearch(arr,item,start,end){
        var start=start||0
        var end=end||arr.length-1
        if(start>end){
            return '404';
        }
        var middle=parseInt((start+end)/2)
        if(arr[middle]===item){
            return middle;
        }else if(arr[middle]>item){
            end=middle-1;
            return binaraySearch(arr,item,start,end)
        }else if(arr[middle]<item){
            start=middle+1;
            return binaraySearch(arr,item,start,end)
        }
    }
Copy the code
/ / the recursionfunction binaraySearch1(arr,item){
        var start=0;
        var end=arr.length-1;
        while(start<=end){
            var middle=parseInt((start+end)/2)
            if(item===middle){
                return middle
            }else if(item>arr[middle]){
                start=middle+1;
            }else if(item<arr[middle]){
                end=middle-1;
            }else{
                return- 1; }}}Copy the code