插入排序
//创建列表类 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.insertion = function () { var length = this.array.length for (var i = 1; i < length; i++) { var temp = this.array[i] var j = i while (this.array[j - 1] > temp && j > 0) { this.array[j] = this.array[j - 1] j-- } this.array[j]=temp } } } var list = new ArrayList() list.insert(1) list.insert(5) list.insert(2) list.insert(3) list.insert(4) list.insertion() alert(list)