`use strict`
class SelectSort {
constructor (a){
this.a = a;
}
swap(i,j,a){
let tmp;
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
Sort(){
let self = this ;
for (var i = self.a.length - 1; i >= 0; i--) {
for (var j = i; j >= 0; j--) {
if (self.a[i]>self.a[j]){
self.swap(i,j,self.a)
}
}
console.log(self.a)
}
return self.a
}
}
module.exports = SelectSort;
const SelectSort = require('./selectSort');
let a = [30,56,12,98,34];
const selectSort = new SelectSort(a);
let result = selectSort.Sort()
console.log(result)
结果
➜ selectSort node test.js
[ 30, 56, 34, 98, 12 ]
[ 34, 56, 98, 30, 12 ]
[ 56, 98, 34, 30, 12 ]
[ 98, 56, 34, 30, 12 ]
[ 98, 56, 34, 30, 12 ]
[ 98, 56, 34, 30, 12 ]