选择排序
//创建列表类 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.select = function () { //获取数组长度 var length = this.array.length for (var j = 0; j < length - 1; j++) { var min = j for (var i = min + 1; i < length; i++) { if (this.array[min] > this.array[i]) { min = i } } this.swap(min, j) } } } var list = new ArrayList() list.insert(1) list.insert(5) list.insert(2) list.insert(3) list.insert(4) list.select() alert(list)