//创建列表类
function ArrayList() {
this.array = []
ArrayList.prototype.insert = function (item) {
this.array.push(item)
}
ArrayList.prototype.toString = function () {
return this.array.join('-')
}
//交换两个数据的位置
ArrayList.prototype.swap = function (m, n) {
var temp = this.array[m]
this.array[m] = this.array[n]
this.array[n] = temp
}
ArrayList.prototype.shell = function () {
var length = this.array.length
//初始化增量
var gap = Math.floor(length / 2)
// while循环gap减小
while (gap >= 1) {
//以gap为间隔进行分组进行插入排序
for (var i = gap; i < length; i++) {
var temp = this.array[i]
var j = i
while (this.array[j - gap] > temp && j > gap - 1) {
this.array[j] = this.array[j - gap]
j -= gap
}
//将j位置的元素赋值给temp
this.array[j] = temp
}
gap = Math.floor(gap/2)
}
}
}
var list = new ArrayList()
list.insert(1)
list.insert(5)
list.insert(2)
list.insert(3)
list.insert(4)
list.shell()
alert(list)