Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

插入排序 #185

Open
louzhedong opened this issue Dec 31, 2019 · 0 comments
Open

插入排序 #185

louzhedong opened this issue Dec 31, 2019 · 0 comments

Comments

@louzhedong
Copy link
Owner

算法名称

插入排序

实现思路

  • 将数组前面部分看做有序数组
  • 每次将后面部分的第一个与已排序数组作比较,插入到合适的位置
  • 有序数组初始状态有1个数字
  • 最后的数组是降序排列的

算法分析

时间复杂度为O(n^2)

算法实现

function InsertSort(array) {
  var length = array.length;

  for (var i = 1; i < length; i++) {
    for (var j = 0; j < i; j++) {
      if (array[i] > array[j]) {
        var temp = array[i];
        for (var k = i; k > j; k--) {
          array[k] = array[k - 1];
        }
        array[j] = temp;
      }
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant