phymooc / algorithm

0 stars 0 forks source link

shellSort #8

Open phymo opened 2 years ago

phymo commented 2 years ago
function shellSort(originalArray) {
  let arr = [...originalArray];
  let len = arr.length;
  let gap = Math.floor(len / 2);
  let temp;

  while (gap > 0) {
    for (let i = gap; i < len; i++) {
      temp = arr[i];
      for (let j = i - gap; j >= 0 && arr[j] > temp; j -= gap) {
        [arr[j + gap], arr[j]] = [arr[j], arr[j + gap]];
      }
    }
    gap = Math.floor(gap / 2);
  }
  return arr;
}

const arr1 = [2, 4, 1, 3, 5];
const arr2 = [2, 7, 8, 4, 6, 1, 3, 5, 6, 7, 9, 10, 0, -5];

console.log(shellSort(arr2), arr2);