冒泡排序算法会产生错误输出

时间:2016-08-17 03:10:13

标签: c++ sorting bubble-sort

首先,这是节点struct:

struct node{
    double data;
    node *next, *previous;
};
struct node *first,*last,*current;

现在这里有排序功能:

void bubblesort(){
    bool unfinished = true;
    node *temp= current;
    current =first;

    while(unfinished){
        unfinished = false;

        while (current!=last){
            if (current->data > current->next->data){
                temp= current -> next;
                current -> next =temp-> next;
                temp-> previous = current -> previous;
                current -> previous = n;
                temp-> next = current;
                unfinished = true;

                if(current->previous==last)
                    {last = current;
                    last->next = NULL;}
                if(current==first)
                    {first= current->previous;
                    first->previous=NULL;}
            }else{
                current = current->next;
            }
        }

};

使用此输入:

69.4637 -> 100.193 -> 136.683 -> 101.736 -> 115.222 -> 138.58 -> 90.3641 -> 101.509 -> 120.108 -> 129.731 -> 95.6236 -> 102.905 -> 89.0621 -> 94.345 -> 98.4529 -> 114.205 -> 129.379 -> 127.703 -> 92.5693 -> 88.4147 -> 125.005 -> 117.251 -> 104.636 -> 90.9676 -> 125.574 -> 134.147 -> 124.374 -> 116.539 -> 108.933 -> 82.3446 -> 124.502 -> 90.4727 -> 92.7035 -> 114.99 -> 114.87 -> 114.692

我只得到这个作为输出:

69.4637 -> 100.193 -> 136.683 -> 138.58

出了什么问题?

1 个答案:

答案 0 :(得分:0)

current -> previous = n;

应该是:

current -> previous = temp;