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

希尔排序 #186

Open
louzhedong opened this issue Jan 1, 2020 · 0 comments
Open

希尔排序 #186

louzhedong opened this issue Jan 1, 2020 · 0 comments

Comments

@louzhedong
Copy link
Owner

算法名称

希尔排序

实现思路

  • 是插入排序的一个优化,插入排序可以看作增量为1的希尔排序
  • 插入排序的特点是数组有序程度越高越高效,数组量越小效率越高
  • 希尔排序是将较大的数据集合拆分成若干个小组,每个小组进行排序。对于每个小组来说,数据量较少,效率较高
  • 增量在每一轮结束后缩小为原来的一半,数组量变多,但数据也更加有序

算法分析

时间复杂度根据不同增量有所不同,最大的时间复杂度为O(n^2)

算法实现

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

  for (var gap = Math.floor(length / 2); gap > 0; gap = Math.floor(gap / 2)) {
    for (var i = gap; i < length; i++) {
      insertI(array, gap, i);
    }
  }
}

function insertI(array, gap, i) {
  var inserted = array[i];
  var j;
  for (j = i - gap; j >= 0 && inserted < array[j]; j -= gap) {
    array[j + gap] = array[j];
  }

  array[j + gap] = inserted;
}
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