首页 > 其他分享 >数据结构232-快速排序的实现代码

数据结构232-快速排序的实现代码

时间:2023-02-10 11:35:18浏览次数:42  
标签:function right ArrayList 排序 array 数据结构 swap 232 left


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ArrayList</title>
</head>
<body>
<script>
function ArrayList() {
//属性
this.array = [];
//
ArrayList.prototype.insert = function (item) {
this.array.push(item);
};

ArrayList.prototype.toString = function (item) {
return this.array.join("-");
};
ArrayList.prototype.swap = function (m, n) {
var temp = this.array[m];
this.array[m] = this.array[n];
this.array[n] = temp;
};
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[right]){
this.swap(left,right)
}
//将center换到right-1的位置
this.swap(center,right-1)
return this.array[right-1]
}
ArrayList.prototype.quickSort = function (item) {
this.quickSort(0,this.array.length-1)
}

ArrayList.prototype.quickSort = function (left,right) {
if(left>=right) return

//
var prvot=this.median(left,right)
//
var i=left
var j=right-1
while(true){
while(this.array[++i]<prvot){

}
while(this.array[--i]>prvot){

}
if(i<j){
this.swap(i,j)
}else{
break
}
}
this.swap(i,right-1)
this.quicK(left,i-1)
this.quicK(i+1,right)
}
}
</script>
</body>
</html>

标签:function,right,ArrayList,排序,array,数据结构,swap,232,left
From: https://blog.51cto.com/u_15460007/6049015

相关文章