时间复杂度表示方式 大O表示法

冒泡排序
选择排序

插入排序

希尔排序

快速排序

代码实现
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(m,n){
let temp = this.array[m]
this.array[m] = this.array[n]
this.array[n] = temp
}
ArrayList.prototype.bubbleSort = function(){
let length = this.array.length
for(let j = length -1; j>=0;j--){
for(let i=0;i<j;i++){
if(this.array[i] > this.array[i+1]){
this.swap(i,i+1)
}
}
}
}
ArrayList.prototype.selectSort = function(){
let length = this.array.length
for(let j = length -1; j>=0;j--){
let min = j
for(let i=min+1;i<length -1;i++){
if(this.array[min] > this.array[i]){
min = i
}
}
this.swap(min,j)
}
}
ArrayList.prototype.insertSort = function(){
let length = this.array.length
for(let i=0;i<length -1;i++){
let temp = this.array[i]
let j = i
while(this.array[j-1] > temp && j>0){
this.array[j] = this.array[j-1]
j--
}
this.array[j] = temp
}
}
ArrayList.prototype.shellSort = function(){
let length = this.array.length
let gap = Math.floor(length/2)
while(gap >= 1){
for(let i=gap;i<length;i++){
let temp = this.array[i]
let 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)
}
}
ArrayList.prototype.median = function(left,right){
let 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)
}
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
}
let pivot = this.median(left,right)
let i = left
let j = right - 1
while(true){
while(this.array[i] < pivot){
i++
}
while(this.array[j] > pivot){
j--
}
if(i<j){
this.swap(i,j)
}else{
break
}
}
this.swap(i,right -1)
this.quick(left,i-1)
this.quick(i+1,right-1)
}
}