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 (a, b) {
// temp=this.array[a]
// this.array[a]=this.array[b]
// this.array[b]=temp
// 交换数组中的两个元素
[this.array[a], this.array[b]] = [this.array[b], this.array[a]];
};
// 冒泡排序
ArrayList.prototype.bubbleSort = function () {
for (let i = 0; i < this.array.length - 1; i++) {
for (let j = 0; j < this.array.length - 1 - i; j++) {
if (this.array[j] > this.array[j + 1]) {
this.swap(j, j + 1);
}
}
}
return this.array;
};
// 选择排序
ArrayList.prototype.selectionSort = function () {
for (let j = 0; j < this.array.length - 1; j++) {
var min = j;
for (let i = min + 1; i < this.array.length; i++) {
if (this.array[min] > this.array[i]) {
min = i;
}
}
this.swap(j, min);
}
return this.array;
};
// 插入排序
ArrayList.prototype.insertSort = function () {
for (var i = 1; i < this.array.length; i++) {
// for(var j=i-1;j>=0;j--){
// if(this.array[i]<this.array[j]){
// this.array.splice(j,0,this.array[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;
}
return this.array;
};
// 希尔排序
ArrayList.prototype.shellSort = function () {
// 初始化增量gap
var gap = Math.floor(this.array.length / 2);
while (gap >= 1) {
for (var i = gap; i < this.array.length; i++) {
var temp = this.array[i];
var j = i;
while (this.array[j - gap] > temp && j > gap - 1) {
this.array[j] = this.array[j - gap];
j -= gap;
}
this.array[j] = temp;
}
gap = Math.floor(gap / 2);
}
return this.array;
};
// 快速排序
// 选择枢纽
ArrayList.prototype.median = function (left, right) {
var center = Math.floor((left + right) / 2);
if (this.array[left] > this.array[center]) {
this.swap(left, center);
}
if (this.array[center] > this.array[right]) {
this.swap(center, right);
}
if (this.array[left] > this.array[center]) {
this.swap(left, center);
}
// 将center换到right-1
this.swap(center, right - 1);
return this.array[right - 1];
};
ArrayList.prototype.quickSort = function () {
this.quick(0, this.array.length - 1);
};
ArrayList.prototype.quick = function (left, right) {
if (left >= right) return;
// 获取枢纽
var pivot = this.median(left, right);
var i = left;
var j = right - 1;
while (true) {
while (this.array[++i] < pivot) {}
while (this.array[--j] > pivot) {}
if (i < j) {
this.swap(i, j);
} else {
break;
}
}
this.swap(i, right - 1);
this.quick(left, i - 1);
this.quick(i + 1, right);
};
ArrayList.prototype.findMax = function () {
var max = 0;
for (var i = 0; i < this.array.length; i++) {
if (this.array[i] > max) {
max = this.array[i];
}
return max;
}
};
ArrayList.prototype.countingSort = function () {
if (this.array.length < 2) {
return array;
}
const maxValue = this.findMax();
let sortedIndex = 0;
const counts = new Array(maxValue + 1);
this.array.forEach((element) => {
if (!counts[element]) {
counts[element] = 0;
}
counts[element]++;
});
counts.forEach((element, i) => {
console.log(element);
while (element > 0) {
this.array[sortedIndex++] = i;
element--;
}
});
return this.array;
};
}
// test
var list = new ArrayList();
list.insert(66);
list.insert(88);
list.insert(12);
list.insert(87);
list.insert(100);
list.insert(5);
list.insert(566);
list.insert(23);
console.log(list.toString());
// console.log(list.bubbleSort())
// console.log('selection',list.selectionSort());
// console.log('insert',list.insertSort());
// console.log('shell',list.shellSort());
// list.quickSort();
// console.log("quick", list);
list.countingSort()
console.log('countingSort',list);
常见排序方法_js
最新推荐文章于 2024-05-19 02:32:57 发布