Javascript implementation of sorting algorithms (used)

Realization of the sorting algorithm of Javascript

Man of few words said, directly code.

1. Bubble Sort

1, followed by two numbers compare adjacent, if a former than the latter large, the exchange of position between the two, otherwise the same position

2. The method of operation of the first step is repeated before the length-1 numbers, until the last number

Graphic examples

code show as below:

function bubbleSort(nums) {
            var key = 0;
            for (var i = 0; i < nums.length - 1; i++) {
                for (var j = i + 1; j < nums.length; j++) {
                    if (nums[i] > nums[j]) {
                        key = nums[i];
                        nums[i] = nums[j];
                        nums[j] = key;
                    }
                }
            }
            return nums;
        }

2. Select Sort

1, find the smallest sequence number, and the number of exchange position in front of the

Graphic examples

code show as below:

function select(nums){
            var item =0;
            for(var i = 0;i<nums.length-1;i++){
                var min = i;                                //把最小索引给min
                for(var j = i+1;j<nums.length;j++){
                    if(nums[min] > nums[j]) {               //对比,如果最小索引的vlaue大于其他value,则将该索引给min
                        min = j                             
                    }
                }               
                item = nums[min];                           //最小索引给他
                nums[min] = nums[i];
                nums[i] = item;
            }
            return nums;
        }

3, Quick Sort

1, to find that the number of intermediate, as a reference, through the array, is less than the reference number of the array on the left, on the right is larger than the reference number of the array

2, then the same way, these two arrays are sorted

3, Recursion

Graphic examples:

code show as below:

var quickSort = function(nums) {
            if (nums.length <= 1) return nums;
            var pivotIndex = Math.floor(nums.length / 2);
            var pivot = nums.splice(pivotIndex, 1)[0]; //splice返回的是一个数组,所以用[0]取出来
            var left = [];
            var right = [];
            for (var i = 0; i < nums.length; i++) {
                if (nums[i] < pivot) {
                    left.push(nums[i]);
                } else {
                    right.push(nums[i]);
                }
            }

            return quickSort(left).concat(pivot, quickSort(right)); //使用递归完成快排
        }

4, insertion sort

1, the array is divided into a sorted [] and [] unsorted two portions, a first set number [sorted], [remainder] unsorted

2, is withdrawn from the first [] number of unsorted, sorted sequentially forwardly and [section], and if the number is larger than the extraction, the comparison continues forward, or a point after the inserted

Graphic examples:

code show as below:

function insertionSort(nums) {
            var i,j,value;
            var len  = nums.length;
            for(i = 0;i<len;i++){
                value = nums[i];
                for(j = i-1;j>=0&&nums[j]>value;j--){
                    nums[j+1] = nums[j];                        //向后移一位
                }
                nums[j+1] = value;
            }
            return nums
        }

Guess you like

Origin www.cnblogs.com/seanxushuo/p/11461743.html