在双重链接列表中的QuickSort算法

Quicksort Algorithm in a doubly linked list

本文关键字:QuickSort 算法 列表 链接      更新时间:2023-10-16

我想用姓氏对QuickSort的算法进行对列表进行排序

在这一部分中,链的值交换

void swap(string* a, string* b){

cout<<"A and B are "<<*a<<" - "<<*b<<endl;
string t = *a;
*a = *b;
cout<<" A is ->"<<*a<<endl;
*b= t;
cout<<"B is ->"<<*b<<endl;
}

这是进行分区的地方。我注意到,当 * i和 * j取值时,它们是完全相同的名称,因此无法以后进行比较。在我看来,如果它是一个数字,那么此列表有效,但是当它是字符串时,此错误发生。

User *i = lower;

,但这在结尾不起作用,因为程序崩溃了,但是如果您更改字符串的值

User* partition(User *lower, User *high){
cout<<"Lower ->  "<<lower->lastname<<endl;
cout<<"High ->  "<<high->lastname<<endl;
string  pivot = high->lastname;
User *i = bajo->prev;
 for (User *j = lower; j != high; j = j->next)
{
    if (j->lastname.compare(pivot)< 0)
    {
        i = (i == NULL)? lower : i->next;
        cout<<"Atention J e I valen ->"<<i->lastname<<" - "<<j->lastname<<endl;
        swap(&(i->lastname), &(j->lastname));
    }
}
i = (i == NULL)? lower : i->lastname; // Similar to i++
swap(&(i->lastname), &(alto->lastname));
return i;
}

我失败了什么?我该如何真正采用所需的价值。

编辑:

这是源代码

#include <iostream>
#include<iomanip>
#include <string>
#include<cstdlib>
using namespace std;
class User
{
public :
    string lastname;
    User *next;
    User *prev;
    User()
    {
        lastname= "";
        next=NULL;
        prev=NULL;
    }
    int empty(User *listt)
    {
        if(listt ==  NULL)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    User *Insert(User *listt, string lastName)
    {
        User *temp = new User();
        if(empty(listt))
        {
            temp->lastname=lastName;
            listt = temp;
        }
        else
        {
            temp->lastname=lastName;
            listt->prev=temp;
            temp->next=listt;
            listt=temp;
        }
        return listt;
    }
    void swap(string* a, string* b)
    {
        string t = *a;
        *a = *b;
        *b= t;
    }
    User* partition(User* lower, User* high)
    {
        cout<<"Lower ->  "<<lower->lastname<<endl;
        cout<<"High ->  "<<high->lastname<<endl;
        string  pivot = high->lastname;
        User *i = lower->prev;
        for (User *j = lower; j != high; j = j->next)
        {
            if (j->lastname.compare(pivot)< 0)
            {
                i = (i == NULL)? lower : i->next;
                swap(&(i->lastname), &(j->lastname));
            }
        }
        i = (i == NULL)? lower : i->next; // Similar to i++
        swap(&(i->lastname), &(high->lastname));
        return i;
    }
    User *Last(User *listt)
    {
        User *temp = listt;
        while(temp && temp ->next)
            temp=temp->next;
        return temp;

    }
    void _quickSort( User* lower, User* high)
    {
        if(high != NULL && lower != high&&lower!= high->next)
        {
            User *p = partition(lower,high);
            _quickSort(lower,p->next); //I change this part
            _quickSort(p->next,high);
        }
    }
    void quickSort(User *listt)
    {
        User *h = Last(listt);
        _quickSort(listt, h);

    }
    User *Display(User *listt)
    {
        if(empty(listt))
        {
            cout<<"List empty"<<endl;
        }
        else
        {
            User *temp = new User();
            temp = listt;
            while(temp!= NULL)
            {
                cout<<"The last name is -> "<<temp->lastname<<endl;
                temp=temp->next;
            }
        }
        return listt;

    }
};
int main()
{
    User *listt = NULL;
    User y;
    bool exit = false;
    int opc;
    string lastName;
    while(!exit)
    {
        cout<<"1.-Insert an element"<<endl;
        cout<<"2.-Sort element-(Quicksort)"<<endl;
        cout<<"3.-Show elements"<<endl;
        cout<<"4.-Exitt"<<endl;
        cin>>opc;
        switch(opc)
        {
        case 1:
            cout<<"Inser your last name"<<endl;
            cin>>lastName;
            listt=y.Insert(listt,lastName);
            system("pause");
            system("cls");
            break;
        case 2:
            cout<<"Sorting...."<<endl;
            y.quickSort(listt);
            system("pause");
            system("cls");
            break;
        case 3:
            cout<<"Display..."<<endl;
            y.Display(listt);
            system("pause");
            system("cls");
            break;
        case 4:
            exit = true;
            break;

        }


    }


}

实际上您的交换函数似乎有效,但是string t = *a;的用法很奇怪,因为*a被视为int值,因此您不应将其分配给字符串,尽管编译器可以以任何方式处理它。另一方面,我想您提到的是将" A"的值复制到临时字符串中,应该以string* t = a;的形式完成,然后您可以进行b = t;,但是不再是通过引用传递的是更好的实践,例如

void swap(string &a, string &b){
    string t = a;
    a = b;
    b= t;
}

,您可能需要检查快速实现的实现,请参阅此页面上的参考

_quickSort(lower,p->next); //I change this part

这应该是p->prev。正确的功能:

void _quickSort(User* lower, User* high)
{
    if(high != NULL && lower != high&&lower != high->next)
    {
        User *p = partition(lower, high);
        _quickSort(lower, p->prev); //change it back!
        _quickSort(p->next, high);
    }
}

此外,Display中还有资源泄漏。不要在显示功能中分配新项目:

User *Display(User *listt)
{
    if(empty(listt))
    {
        cout<<"List empty"<<endl;
    }
    else
    {
        //User *temp = new User(); <== don't allocate new item here
        User *temp = listt;
        while(temp!= NULL)
        {
            cout<<"The last name is -> "<<temp->lastname<<endl;
            temp=temp->next;
        }
    }
    return listt;
}

用1000个项目进行测试:

class User
{
public:
    class Node
    {
    public:
        string lastname;
        Node *next;
        Node *prev;
        Node()
        {
            prev = next = nullptr;
        }
    };
    Node *head;
    User()
    {
        head = nullptr;
    }
    void Insert(string val)
    {
        Node *temp = new Node;
        temp->lastname = val;
        if (head)
        {
            head->prev = temp;
            temp->next = head;
        }
        head = temp;
    }
    void swap(string &a, string &b)
    {
        string t = a;
        a = b;
        b = t;
    }
    Node *Tail()
    {
        Node *temp = head;
        while(temp && temp->next)
            temp = temp->next;
        return temp;
    }
    Node* partition(Node* left, Node* right)
    {
        string pivot = right->lastname;
        Node *i = left->prev;
        for(Node *j = left; j != right; j = j->next)
        {
            if(j->lastname < pivot)
            {
                i = (i == nullptr) ? left : i->next;
                swap(i->lastname, j->lastname);
            }
        }
        i = (i == nullptr) ? left : i->next; // Similar to i++
        swap(i->lastname, right->lastname);
        return i;
    }
    void quickSort(Node* left, Node* right)
    {
        if(!left || !right || left == right || left == right->next) 
            return;
        Node *p = partition(left, right);
        quickSort(left, p->prev);
        quickSort(p->next, right);
    }
    void quickSort()
    {
        quickSort(head, Tail());
    }
    void Display()
    {
        string last;
        for (Node *n = head; n; n = n->next)
        {
            if(n->lastname < last)
            {
                cout << "error ***n";
                break;
            }
            last = n->lastname;
            cout << n->lastname << endl;
        }
    }
};
int main()
{
    User list;
    list.Insert("z");
    list.Insert("c");
    list.Insert("a");
    list.Insert("g");
    for(int i = 0; i < 1000; i++)
    {
        string str;
        for (int j = 0; j < 3; j++)
            str.push_back('a' + rand() % 26);
        list.Insert(str);
    }
    list.quickSort();
    list.Display();
    return 0;
}