pwstrick / daily

一份搜集的前端面试题目清单、面试相关以及各类学习的资料(不局限于前端)
2.39k stars 242 forks source link

希尔排序 #987

Open pwstrick opened 4 years ago

pwstrick commented 4 years ago
function shellSort(arr) {
  var len = arr.length,
    gap = 0,
    temp;
  while (gap < len / 5) {       //动态定义步长序列
    gap = gap * 5 + 1;
  }
  for (; gap > 0; gap = Math.floor(gap / 5)) {
    for (var i = gap; i < len; i++) {
      temp = arr[i];
      for (var j = i - gap; j >= 0 && arr[j] > temp; j -= gap) {
        arr[j + gap] = arr[j];
      }
      arr[j + gap] = temp;
    }
  }
  return arr;
}