function ArrayList(){
this.array = [];
ArrayList.prototype.insert = function (num){
this.array.push(num);
}
ArrayList.prototype.toString = function (num){
return this.array.join('->');
}
ArrayList.prototype.swap = function (m,n){
let temp = this.array[m];
this.array[m] = this.array[n];
this.array[n] = temp;
}
ArrayList.prototype.bubbleSort = function (){
console.time('bubbleSort');
for (let i = this.array.length - 1; i >=0 ; i--) {
for (let j = 0; j <this.array.length - 1; j++) {
if (this.array[j] > this.array[j+1]){
this.swap(j,j+1);
}
}
}
console.timeEnd('bubbleSort');
}
ArrayList.prototype.selectSort = function (){
console.time("selectSort")
for (let i = 0; i < this.array.length - 1; i++) {
let min = i;
for (let j = min + 1; j < this.array.length; j++) {
if (this.array[min] > this.array[j]) min = j;
}
this.swap(min,i);
}
console.timeEnd("selectSort")
}
ArrayList.prototype.insertSort = function(){
const length = this.array.length;
console.time('insertSort')
for (var i = 1; i <length; i++) {
const item = this.array[i];
let j = i;
while(item < this.array[j - 1] && j > 0 ){
this.array[j] = this.array[j - 1];
j--;
}
this.array[j] = item;
}
console.timeEnd('insertSort');
}
ArrayList.prototype.xiErSort = function(){
const length = this.array.length;
let gap = Math.floor(length / 2);
console.time('xiErSort')
while(gap >= 1){
for (var i = gap ; i < length; i++) {
const item = this.array[i];
let j = i;
while(item < this.array[j - gap] && j > gap - 1){
this.array[j] = this.array[j - gap];
j -= gap;
}
this.array[j] = item;
}
gap = Math.floor(gap / 2);
}
console.timeEnd('xiErSort');
}
ArrayList.prototype.getCenter = function(left,right){
let center = Math.floor((left + right) / 2);
if(this.array[left] > this.array[center]) this.swap(left,center);
if(this.array[left] > this.array[right]) this.swap(left,right);
if(this.array[center] > this.array[right]) this.swap(center,right);
this.swap(center,right - 1);
console.log(this.array[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;
let center = this.getCenter(left,right);
let i = left;
let j = right - 1;
while(true){
while(this.array[++i] < center){}
while(this.array[--j] > center){}
if(i < j){
this.swap(i,j);
}else{
break;
}
}
this.swap(i,right - 1);
this.quick(left,i - 1);
this.quick(i + 1,right);
}
}
let list = new ArrayList();
list.insert(52);
list.insert(12);
list.insert(6);
list.insert(100);
list.insert(65);
list.insert(34);
list.insert(15);
list.insert(8);
list.insert(1000);
list.insert(60);
list.insert(800);
list.insert(1);
list.insert(64);
list.insert(78);
list.insert(52);
list.insert(36);
list.insert(13);
list.insert(14);
console.log(list.toString());
console.time('quickSort')
list.quickSort();
console.timeEnd('quickSort')
console.log(list.toString());
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!