在双重链接列表中的QuickSort算法
Quicksort Algorithm in a doubly linked list
我想用姓氏对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;
}
相关文章:
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 基于ELO的团队匹配算法
- C++选择排序算法中的逻辑错误
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- C++A*算法并不总是在路径中具有目标节点
- 排序算法c++
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- 算法问题:查找从堆栈中弹出的所有序列
- 下面是排序算法O(n)吗
- KMP算法和LPS表构造的运行时间
- 为什么我的排序算法会更改数组值
- 求最大元素位置的分治算法
- 具有非整数边容量的最大流量的Dinic算法
- 到连接组件算法的问题(递归)
- STL算法函数在多个一维容器上的使用
- 计算QuickSort算法中的基本操作
- 在双重链接列表中的QuickSort算法
- 使用QuickSort算法以降序排序向量
- 计算QuickSort算法中的组件明智比较数量
- 两种排序算法在同一个数组上给出了两种不同的输出(quickSort和heapSort)