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

Bubble sort algorithm produces incorrect output

本文关键字:输出 错误 算法 冒泡排序      更新时间:2023-10-16

首先,这里是节点结构:

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

出了什么问题?

current -> previous = n;

应该是:

current -> previous = temp;