Selection sort, bubble sort, bucket sort and quick sort

Select Sort of the way

Here we use an example from small to large order
first: the first is the assumed minimum term
Second: continue to turn a contrast with the back of the item using the first, if there are smaller items, recorded the smallest item and index
first three: after the completion of a cycle, the first item with the item exchange position.
Fourth: the second cycle starts from the second, and so on, to complete the cycle of the reciprocal of the second term is determined.
Code:

var arr = [11,2,32,4,54,6,3];
 for(var k=0;k<arr.length-1;k++){
            var min   = arr[k];
            var index = k;
            for(var i = k+1; i<arr.length;i++){
                
                if(arr[i]<min){
                    // 只记录数字不换位
                    min = arr[i];
                    index = i
                }
            }
            arr[index] = arr[k];
            arr[k] = min;
        }
        console.log(arr)

Here Insert Picture Description

Sort of bubble sort

Bubble sort sorting method is also an option, but at a different method employed to find a minimum value. Likewise in ascending order as an example
of the first: Compare the first and second size smaller than the first if the second exchange position;
second: iterative sequence, which would minimize the unsorted element into its correct position;
third: until the penultimate element first into the correct position;

	for(k = 0;k<arr.length - 1;k++){
            for(i=0;i<arr.length - 1-k; i++){
                if(arr[i]>arr[i+1]){
                    var team = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] = team;
        }
        }
        }
        console.log(arr);

Improved:
If this list has been ordered, and will continue to cycle more than ordering comparison, it is meaningless. We can before entering the loop, set a boolean variable to false, the event of the exchange operations in a loop, the Boolean value variables to true. If the end of the loop, the Boolean variable is still false, prove that this list is ordered, the whole process need not continue;

var bool = false;
        for(k = 0;k<arr.length - 1;k++){
            for(i=0;i<arr.length - 1-k; i++){
                if(arr[i]>arr[i+1]){
                    var team = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] = team;
                    bool = true;
        }
        }
        if( bool ===false){
            break;
        }
        }

Sort of the way the barrel

Bucket sort is an extreme sort, more memory footprint. We all know that the array subscript are ordered, no matter what, are not random array subscript placed
first: the need for a new array, with the array of old items as a new array subscript
second: is arbitrary assigning each new array
third: empty original array, a new array is taken out one by one to increase the original array index;

        var arr = [3,2,5,7,10]
        var bocket = [];
        for(var i = 0 ;  i < arr.length ; i ++){
            bocket[arr[i]] = 1;
        }
        arr.length = 0;
        // 用for in 取出更好,比for 循环节省执行次数;
        for(var attr in bocket){
            // 因为 for in 之中取出的 attr 是字符串,那么我们需要进行一波转换;
            arr.push(Number(attr));
        }
        console.log(arr);

Sort of the way fast

This is the basic strategy is recursive, the array into two, then two small arrays continue the same way, no need to divide up, a recursive digital more time for higher performance requirements;
first: find the focus, left and right. Find the midpoint of the index and the value
of the second: the establishment of two empty array, named so. Smaller than the midpoint of the array on the left than on the right side of the midpoint large array;
Third: continue down the minutes until only one array or array is empty, terminate recursively

 // 找中点 分左右!;
        var arr = [2,5,1,3,7,4];
        function quickSort( arr ){
            if(arr.length === 1 || arr.length === 0){
                return arr;
            }
            var mid_index = parseInt(arr.length / 2);
            var mid_num   = arr[mid_index];
            var left  = [];
            var right = [];
			for(var i = 0 ; i < arr.length ; i ++){
                // 不要和自己比对;
                if( i === mid_index){
                    continue;
                }
                if( arr[i] > mid_num ){
                    right.push(arr[i]);
                }else{
                    left.push(arr[i]);
                }
            } 
            //注意:concat第一个参数必须转换成数组; => [mid_num]
            return quickSort(left).concat( [mid_num] , quickSort(right) );
        }
        var res = quickSort( arr )
        console.log(res);
Released six original articles · won praise 17 · views 2452

Guess you like

Origin blog.csdn.net/htcvive/article/details/104741916