function ArrayList(){
this.array = []
ArrayList.prototype.insert = function(item){
this.array.push(item)
}
ArrayList.prototype.toString = function(){
return this.array.join('-')
}
}
module.exports = ArrayList
var ArrayList = require('./列表')
function Sorting(){
this.arr = new ArrayList()
Sorting.prototype.swap = function(m,n){
var temp = this.arr.array[m]
this.arr.array[m] = this.arr.array[n]
this.arr.array[n] = temp
}
Sorting.prototype.bubbleSort = function(){
var length = this.arr.array.length
for(var i = length-1;i>=0;i--){
for(var j = 0;j<i;j++){
if(this.arr.array[j] > this.arr.array[j+1]){
this.swap(j,j+1)
}
}
}
}
Sorting.prototype.slectionSort = function(){
var length = this.arr.array.length
for(var j = 0;j<length-1;j++){
var min = j
for(var i = min+1;i<length;i++){
if(this.arr.array[min] > this.arr.array[i]){
min = i
}
}
this.swap(min,j)
}
}
Sorting.prototype.insertSort = function(){
var length = this.arr.array.length
for(var i = 1;i<length;i++){
var temp = this.arr.array[i]
var j = i
while(this.arr.array[j-1] > temp && j>0){
this.arr.array[j] = this.arr.array[j-1]
j--
}
this.arr.array[j] = temp
}
}
Sorting.prototype.shellSort = function(){
var length = this.arr.array.length
var gap = Math.floor(length/2)
while(gap>=1){
for(var i = gap;i<length;i++){
var temp = this.arr.array[i]
var j = i
while(this.arr.array[j-gap] > temp && j>gap-1){
this.arr.array[j] = this.arr.array[j-gap]
j -= gap
}
this.arr.array[j] = temp
}
gap = Math.floor(gap/2)
}
}
Sorting.prototype.median = function(left,right){
var center = Math.floor((left+right)/2)
if(this.arr.array[left] > this.arr.array[center]){
this.swap(left,center)
}
if(this.arr.array[center] > this.arr.array[right]){
this.swap(center,right)
}
if(this.arr.array[left] > this.arr.array[center]){
this.swap(left,center)
}
this.swap(center,right-1)
return this.arr.array[right-1]
}
Sorting.prototype.qiuckSort = function (){
this.qiuck(0,this.arr.array.length-1)
}
Sorting.prototype.qiuck = function(left,right){
if(left>=right) return
var pivot = this.median(left,right)
var i = left
var j = right -1
while(i < j){
while(this.arr.array[++i] < pivot){}
while(this.arr.array[--j] > pivot){}
if(i < j){
this.swap(i,j)
}else{
break
}
}
console.log(left,right,i,j,this.arr.array.toString())
this.swap(i,right-1)
this.qiuck(left,i-1)
this.qiuck(i+1,right)
}
}
var list = new Sorting()
list.arr.insert(500)
list.arr.insert(600)
list.arr.insert(400)
list.arr.insert(300)
list.arr.insert(100)
list.arr.insert(100)
list.arr.insert(1100)
list.arr.insert(600)
list.arr.insert(100)
list.arr.insert(200)
list.arr.insert(300)
list.qiuckSort()
console.log(list.arr.toString())