我想用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;
}
}
}
答案 0 :(得分:1)
实际上你的交换函数似乎有效,但string t = *a;
用法有点奇怪,因为*a
被认为是一个int值所以你不应该将它分配给一个字符串,尽管编译器可以处理它。另一方面,我猜你提到的是将“a”的值复制到一个临时字符串中,它应该以{{1}}完成,然后你可以string* t = a;
而不是通过引用传递是一种更好的做法,例如
b = t;
您可能需要查看快速排序实现,请参阅this page
上的参考答案 1 :(得分:0)
_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;
}