Giter VIP home page Giter VIP logo

Comments (2)

santa955 avatar santa955 commented on August 25, 2024

冒泡排序算法

  • 平均时间复杂度是O(n^2)
  • 最优时间复杂度是O(n)
  • 空间复杂度O(n)
var bubbleSort= function (nums) {
  var len = nums.length
  for (var i = 0; i < len - 1; i++) {
    for (var j = 0; j < len - i - 1; j++) {
      if (nums[j] > nums[j + 1]) {
        var temp = nums[j]
        nums[j] = nums[j + 1]
        nums[j + 1] = temp
      }
    }
  }
  return nums
};

鸡尾酒排序算法

鸡尾酒排序算法是冒泡排序算法的优化,优化点是采用双向比较

  • 平均时间复杂度是O(n^2)
  • 最优时间复杂度是O(n)
  • 空间复杂度O(n)
function cockTailSort(nums) {
    var start = 0
    var end = nums.length - 1;
    var temp;
    while (start < end) {
        //注意i的取值范围[star, end), 半开半闭
        for (var i = start; i < end; i++) {
            if (nums[i] > nums[i + 1]) {
                temp = nums[i];
                nums[i] = nums[i + 1];
                nums[i + 1] = temp;
            }
        }
        end--;
        //注意i的取值范围(star, end], 半开半闭
        for (var i = end; i > start; i--) {
            if (nums[i - 1] > nums[i]) {
                temp = nums[i];
                nums[i] = nums[i - 1];
                nums[i - 1] = temp;
            }
        }
        start++;
    }
    return nums
}

from blog.

santa955 avatar santa955 commented on August 25, 2024

插入排序算法

Insertion Sort 和打扑克牌时,从牌桌上逐一拿起扑克牌,在手上排序的过程相同。
举例:

Input: {5 2 4 6 1 3}。
首先拿起第一张牌, 手上有 {5}。
拿起第二张牌 2, 把 2 insert 到手上的牌 {5}, 得到 {2 5}。
拿起第三张牌 4, 把 4 insert 到手上的牌 {2 5}, 得到 {2 4 5}。
以此类推

一般来说,插入排序都采用in-place在数组上实现。具体算法描述如下:

  1. 从第一个元素开始,该元素可以认为已经被排序
  2. 取出下一个元素,在已经排序的元素序列中从后向前扫描
  3. 如果该元素(已排序)大于新元素,将该元素移到下一位置
  4. 重复步骤3,直到找到已排序的元素小于或者等于新元素的位置
  5. 将新元素插入到该位置后
  6. 重复步骤2~5
  • 平均时间复杂度是O(n^2)
  • 最优时间复杂度是O(n)
  • 空间复杂度O(n)
function insertionSort (nums) {
    var len = nums.length
    for (var i = 0; i < len; i++) {
        var num = nums[i]
        //注意j的起始值
        var j = i - 1
        while (j >= 0 && nums[j] > num) {
            nums[j + 1] = nums[j]
            j--
        }
        //注意此处的下标
        nums[j + 1] = num
    }
    return nums
};

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.