快速排序不起作用,不明白为什么



function quicksort(items){
if((items.length)<=1){
return(items);
}
point1 = 0;
point2 = ((items.length)-1);
while(point1 != point2){
if(((items[point1] < items[point2]) &&(point1>point2)) || ((items[point1] > items[point2]) && (point1 < point2))){
var temp = items[point1];
items[point1] = items[point2];
items[point2] = temp;
var temp2 = point1;
point1 = point2;
point2 = temp2;
}
if(point1<point2){
point1 = point1 + 1;
}else{
point1 = point1 - 1;
}
}
var left = quicksort(items.slice(0,point1));
var right = quicksort(items.slice((point1+1),items.length));
var x  = [];
x.concat(left,[items[point1]],right);
return(x);
}
var x = (quicksort([5,4,3,2,1,8,19,7,100,6]));
console.log(x);

我不明白为什么我的快速排序实现不起作用,我希望能有新的眼光来发现的问题

我重写了它,它现在可以工作了,对未来的任何人来说:

function quicksort(items){
if((items.length)<=1){
return(items);
}
var pointer1 = 0;
var pointer2 = (items.length -1);
while(pointer1 != pointer2){
if(((items[pointer1]>items[pointer2])&&(pointer1<pointer2)) || (((items[pointer2])>items[pointer1]) && (pointer2<pointer1))){
var temp = items[pointer1];
items[pointer1] = items[pointer2];
items[pointer2] = temp;
var temp2 = pointer1;
pointer1 = pointer2;
pointer2 = temp2;
}
if (pointer1<pointer2){
pointer1+=1;
}else{
pointer1-=1;
}
}
var left = quicksort((items.slice(0,pointer1)));
var right = quicksort((items.slice((pointer1+1),(items.length))));
return(left.concat([items[pointer1]],right));
}
console.log(quicksort([5,4,3,2,1]));

相关内容

  • 没有找到相关文章

最新更新