【问题标题】:Bubble sort algorithm produces incorrect output冒泡排序算法产生不正确的输出
【发布时间】:2016-08-17 03:10:13
【问题描述】:

首先,这是节点结构:

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

出了什么问题?

【问题讨论】:

  • n 在current -> previous = n; 中使用的是什么?
  • 调试程序时发现了什么?
  • 对我来说好像是作业。

标签: c++ sorting bubble-sort


【解决方案1】:
current -> previous = n;

应该是:

current -> previous = temp;

【讨论】:

    猜你喜欢
    • 2018-12-06
    • 1970-01-01
    • 2022-06-10
    • 2016-02-18
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多