Summarize 4 Common sorts (quick, select sort, bubble sort, insert sort)

Source: Internet
Author: User
Tags sorts
First, select the sort
    1. Concept Understanding:
      In an array of length 3, 3 data is traversed on the first pass to find the smallest value exchanged with the first element ;
      The second pass traverses 2 data to find the smallest element and the first number Exchange (note: The first number here refers to the first number of the traversal, which is essentially the second number of the array)
      And the third trip is to compare with yourself, position or the original position

    2. Complexity of:
      Average time complexity: O (n^2)

    3. Example:
//选择排序function selectionSortFn(arr){    console.log('原数组:['+ arr + ']')        for (var i = 0; i < arr.length; i++) {        for (var j = i+1; j < arr.length; j++) {            if (arr[i] > arr[j]) {                var temp = arr[i];                arr[i] = arr[j];                arr[j] = temp;            }        }        console.log(arr);    }    return arr;}var initArr = [10, 4, 8, 3];selectionSortFn(initArr);

Let's look at the printed results:
[Select Sort]

原数组:[10,4,8,3][3, 10, 8, 4][3, 4, 10, 8][3, 4, 8, 10][3, 4, 8, 10]

The concept of integration is well understood.

Second, bubble sort
    1. Concept Understanding:
      Compare adjacent two numbers in turn, place decimals ahead, and large numbers behind.
      First trip: First compare the first and second numbers, put the decimal before, the large number is put, and then compare the second number and the third number before the decimal place, after the large number is put, so continue until the last two numbers are compared, the decimal place before the large number, and then the first trip to the end.
      On the second pass: The comparison is still starting from the first logarithm (since the 1th number is no longer less than the 2nd number due to the 2nd and 3rd number of interchanges), the decimal point is placed before, and after the large number is placed, it is compared to the second last number (which is already the largest in the penultimate position) and the second end. Get a new maximum number in the penultimate position (actually the second largest number in the whole sequence).
      So go on, repeat the above process until the final sort is finished.
    2. Complexity of
      Complexity of Time: O (n^2)

    3. Example
//冒泡排序function bubbleSortFn(arr){    console.log('原数组:['+arr + ']')        for (var i = 0; i < arr.length-1; i++) {        for (var j = 0; j < arr.length-1-i; j++) {        //每次比较都会确定一个最小数,所以j < arr.length-1-i            if (arr[j] > arr[j+1]) {                var temp = arr[j];                arr[j] = arr[j+1];                arr[j+1] = temp;            }        }        console.log(arr)    }    return arr;}var initArr = [10, 4, 8, 3];bubbleSortFn(initArr);

Let's look at the printed results:

原数组:[10,4,8,3][4, 8, 3, 10][4, 3, 8, 10][3, 4, 8, 10]
Three, quick sort
    1. Concept Understanding:
      • Any element in the sorted element as a datum (usually the first one), called a datum element;
      • b partition the element with the order, the element that is larger than the datum element on his right side, and the smaller on the left;
      • C to the left and right two partitions repeat the above steps (recursion) directly to all elements in order;
    2. Complexity of
      Time Complex: O (NLOGN)
    3. Example
//快速排序function quickSortFn(arr){    console.log('原数组:['+arr + ']')    // 如果数组长度<=1 ,则直接返回    if (arr.length <= 1) return arr;    //    var bisectionIndex = Math.floor(arr.length/2);    // 找基准,把基准从原数组中删除    var bisection = arr.splice(bisection,1)[0];    // console.log(bisection);    // 定义作用数组    var left = [];    var right = [];    // 比基准小的放left ,比基准大的放right    for (var i = 0; i < arr.length; i++) {        if (arr[i] <= bisection) {            left.push(arr[i]);        }else{            right.push(arr[i]);        }        console.log(arr);    }    //递归    return quickSortFn(left).concat([bisection],quickSortFn(right));}var initArr = [10, 4, 8, 3];quickSortFn(initArr);

Let's look at the print results:

[4, 8, 3][4, 8, 3][4, 8, 3][8, 3][8, 3]
Iv. Insert Sort
    1. Concept Understanding:
      Check the number I, if the number on the left of it is larger than it, to exchange, this action continues until the number on the left is smaller than it, it can be stopped.

    2. Complexity of
      Complexity of Time: O (n^2)

    3. Example
//插入排序function insertSortFn(arr){    console.log('原数组:['+arr + ']')    for (var i = 1; i < arr.length; i++) {        var temp = arr[i];        for (var j = i-1; j >=0 && temp < arr[j]; j--) {            arr[j+1] = arr[j];            arr[j] = temp;        }        console.log(arr)    }    return arr;}var initArr = [10, 4, 8, 3];insertSortFn(initArr);

Let's look at the print results:

原数组:[10,4,8,3][4, 10, 8, 3][4, 8, 10, 3][3, 4, 8, 10]
Iv. Summary
    1. Bubble sort is the simplest of sorts, but the performance is also the worst, the number of small time can also, the number of many, is very slow.
    2. bubbling, selecting, inserting three sorting complexities are the same (slow)
    3. Fast sorting is the most efficient. Average time complexity is O (nlog2n), Worst is O (n*n), Space complexity O (nlog2n)

Attach a small source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.