function choiceSort(arr){
let len = arr.length
for(let i = 0, min; i < len - 1; i++){
min = arr[i]
for(let j = i; j < len; j++){
if(min > arr[j]){
let c = min
min = arr[j]
arr[j] = c
}
}
arr[i] = min
}
return arr
}
function ArrayList(){
let arr = []
this.insertItem = function(item){
arr.push(item)
}
this.toString = function(){
return arr.join()
}
this.bubbleSort = function(){
const len = arr.length
for(var i = 0; i < len; i++){
for(j = 0; j < len - 1 -i; j++){
if(arr[j] > arr[j+1]){
swap(j, j+1)
}
}
}
}
// 选择排序
this.choiceSort = function(){
const len = arr.length
let minIndex
for(let i = 0; i < len - 1; i++){
minIndex = i
for(let j = i; j < len; j++){
if(arr[minIndex] > arr[j]){
minIndex = j
}
}
if(i !== minIndex){
swap(i, minIndex)
}
}
}
}
function swap(index1, index2){
[index1, index2] = [index2, index1]
}
function createNewList(size){
const new_arr = new ArrayList()
for(let i = size; i > 0; i--){
new_arr.insertItem(i)
}
return new_arr
}
const size = createNewList(5)
console.log(size.toString())
size.choiceSort()
console.log(size.toString())
console