node-maobao.js
‘use strict’
class BubbleSort{
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 = 0; i < self.a.length-1; i++) {
for (var j = self.a.length - 1; j >= i; j--) {
if (self.a[j-1]>self.a[j]){
self.swap(j-1,j,self.a)
}
}
console.log(self.a)
}
return self.a
}
}
module.exports = BubbleSort;
const BubbleSort = require(“./node-maobao”);
let a=[90,45,6,34,89,35]
const bubble = new BubbleSort(a);
const result = bubble.Sort()
console.log(result);
➜ maobao node test.js
[ 6, 90, 45, 34, 35, 89 ]
[ 6, 34, 90, 45, 35, 89 ]
[ 6, 34, 35, 90, 45, 89 ]
[ 6, 34, 35, 45, 90, 89 ]
[ 6, 34, 35, 45, 89, 90 ]
[ 6, 34, 35, 45, 89, 90 ]