c++ - 带链表的快速排序

标签 c++ linked-list quicksort

<分区>

我已经写了链表快速排序的代码,这里是代码

#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct _tagIntegerList
{
    int  nInteger;
    struct _tagIntegerList *prev;
    struct _tagIntegerList *next;


}IntegerList;
IntegerList *firstitem=NULL;
IntegerList *lastitem=NULL;
void addlist(int n)
{

IntegerList *pitem=new IntegerList;
pitem->nInteger=n;
if ( firstitem==NULL){
firstitem=lastitem=pitem;
pitem->next=pitem->prev=NULL;

}
else{
    lastitem->next=pitem;
    pitem->prev=lastitem;
    lastitem=pitem;
    pitem->next=NULL;


}




}
void removeall(){
    IntegerList *delitem,*pitem=firstitem;
    while(pitem!=NULL){
        delitem=pitem;
        pitem=pitem->next;
        delete delitem;



    }

    firstitem=lastitem=NULL;
}


void print(){

    IntegerList * pitem=firstitem;
    while(pitem!=NULL){

        cout<<pitem->nInteger<<" ";
        pitem=pitem->next;


    }

}
// Quick Sort List
void quicksort(IntegerList *left, IntegerList *right)
{
    IntegerList *start;
    IntegerList *current;
    int copyinteger;

    if (left==right) return ;
    start=left;
    current=start->next;
    while(1){
        if (start->nInteger<current->nInteger){
            copyinteger=current->nInteger;
            current->nInteger=start->nInteger;
        start->nInteger=copyinteger;


        }
// Check if we have reached the right end
        if (current=right) break;
        current=current->next;






    }



    //swap first and current items
    copyinteger=left->nInteger;
    left->nInteger=current->nInteger;
    current->nInteger=copyinteger;
        IntegerList *oldcurrent=current;
        // Check if we need to sort the left hand size of the Current point
        current=current->prev;
        if (current!=NULL){
            if ((left->prev!=current)&& (current->next!=left))
                quicksort(left,current);
                }
current=oldcurrent;
current=current->next;
if (current!=NULL){

    if ((current->prev!=right)&& (right->next!=current))
        quicksort(current,right);

}
}
int main(){
    addlist(100);
    addlist(12);
    addlist(56);
    addlist(67);
    addlist(4);
    addlist(91);
    addlist(34);
    addlist(59);
    addlist(42);
    addlist(20);
    addlist(83);
    addlist(74);
    addlist(33);
    addlist(79);
    addlist(49);
    addlist(51);

    quicksort(firstitem,lastitem);
    print();
    removeall();
 return 0;
}

但输出不是我所期待的,这是结果

4 56 67 12 91 34 59 42 20 83 74 33 79 49 51 100 

请帮我看看这段代码有什么问题吗?我也对算法的复杂度感兴趣,它是否与 O(nlogn) 相同?

最佳答案

我假设 current=right 应该是 current==rightif (current=right) break;

关于c++ - 带链表的快速排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9601018/

相关文章:

c++ - 链接现成的外部静态库

c++ - Visual Studio 10中使用/MT或/MD构建dll

algorithm - Θ(n lg n) 中的快速排序

c++ - 当我在快速排序算法的递归调用中包含主元时,为什么会出现堆栈溢出?

c++ - 什么是动态类型的对象

python - python递归中的链表

c - 通过循环遍历 vector 链接列表,使用 opengl1 绘制线条

c# - 为什么 Stack<T> 和 Queue<T> 用数组实现?

Kruskal 算法的 C++ 实现

c++ - 如何在cuda上创建全局可访问的变量?