希尔排序(Shell Sort)
希尔排序(Shell Sort)
希尔排序(Shell's Sort)是插入排序的一种又称“缩小增量排序”(Diminishing Increment Sort),是直接插入排序算法的一种更高效的改进版本。希尔排序由D.L.Shell在1959年提出。
示例
3
5
7
8
4
1
6
9
2
点我排序
vue
<script setup lang="ts">
import {reactive} from "vue";
const ShellSortList = reactive({
ShellSortList:[3,5,7,8,4,1,6,9,0]
})
const ShellSortClick= ()=>{
ShellSort(ShellSortList.ShellSortList)
}
function ShellSort(arr) {
let n = arr.length;
for (let gap = Math.floor(n / 2); gap > 0; gap = Math.floor(gap / 2)) {
for (let i = gap; i < n; i++) {
let temp = arr[i];
let j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
}
return arr;
}
</script>
<template>
<p>{{JSON.stringify(ShellSortList.ShellSortList)}}</p>
<h4 @click="ShellSortClick">点我排序</h4>
</template>
<style scoped lang="scss">
</style>