我的快速排序代码有问题。我不知道为什么,但它没有排序。
我的程序
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
string studentArray[100][3];
string getName();
string getSurname();
string getID();
void setName(string stdName);
void setSurname(string stdSurname);
void setID(string stdIDs);
private:
string name;
string surname;
string ID;
};
int quick_sort_help(string &text,int left, int right)
{
char val = text[right];
char temp;
int j = right;
int i = left - 1;
while (true)
{
while (text[++i] < val);
while (text[--j] > val) {
if(j == left)
break;
}
if(i >= j)
break;
temp=text[i];
text[i]=text[j];
text[j]=temp;
}
temp=text[i];
text[i]=text[right];
text[right]=temp;
return i;
}
void quicksort(string &text,int left, int right)
{
if (left < right)
{
int pivot = quick_sort_help(text, left, right);
quicksort(text, left, pivot - 1);
quicksort(text, pivot + 1, right);
}
}
void quick_sort(string &text,int size){
quicksort(text,0,size-1);
}
int main()
{
Student myStudent;
string name;
string surname;
string id;
int choice;
int temp=0;
char ans1;
do{
cout<<"What do you want to search with"<<endl;
cout<<"1-)For adding a Student:"<<endl;
cout<<"2-)Search with name:"<<endl;
cout<<"3-)Search with surname:"<<endl;
cout<<"4-)Search with ID (binary!):"<<endl;
cout<<"5-)Show List!"<<endl;
cin>>choice;
switch (choice)
{
case 1:
{
char ans;
do
{
cout<<"Please enter Student's name: ";
cin>>name;
cout<<"Please enter Student's surname: ";
cin>>surname;
cout<<"Please enter Student's ID: ";
cin>>id;
myStudent.setName(name);
myStudent.setSurname(surname);
myStudent.setID(id);
myStudent.studentArray[temp][0]=myStudent.getID();
myStudent.studentArray[temp][1]=myStudent.getName();
myStudent.studentArray[temp][2]=myStudent.getSurname();
cout<<"Want to add new Student? (y/Y)";
cin>>ans;
temp++;
}while(ans=='y'||ans=='Y');
break;
}
case 2:
{
cout<<"Enter the student name:";
cin>>name;
for(int i=0;i<temp;i++)
{
if(myStudent.studentArray[i][1]==name)
{
cout<<myStudent.studentArray[i][0] + " " + myStudent.studentArray[i][1] + " " + myStudent.studentArray[i][2]<<endl;
}
}
break;
}
case 3:
{
cout<<"Enter the student surname:";
cin>>surname;
for(int i=0;i<temp;i++)
{
if(myStudent.studentArray[i][2]==surname)
{
cout<<myStudent.studentArray[i][0] + " " + myStudent.studentArray[i][1] + " " + myStudent.studentArray[i][2]<<endl;
}
}
break;
}
case 4:
{
cout<<"Enter the student ID:";
cin>>id;
for(int i=0;i<temp;i++){
for(int j=i+1;j<temp;j++){
quick_sort(myStudent.studentArray[temp][0],temp);
}
}
int binary=temp/2;
for(int i=0;i<temp;i++)
{
if(myStudent.studentArray[binary][0]>id)
{
binary = binary - binary/2;
}
if(myStudent.studentArray[binary][0]<id)
{
binary = binary + binary/2;
}
if(myStudent.studentArray[binary][0]==id)
{
cout<<myStudent.studentArray[binary][0]+ " " + myStudent.studentArray[binary][1]+ " " + myStudent.studentArray[binary][2]<<endl;
break;
}
}
break;
}
case 5:
{
cout<<"id/name/surname"<<endl;
for(int i=0;i<temp;i++)
{
cout<<myStudent.studentArray[i][0]+ " " + myStudent.studentArray[i][1] + " " + myStudent.studentArray[i][2]<<endl;
}
break;
}
}
cout<<"Want to select action again?(y/Y)";
cin>>ans1;
}while(ans1=='y'||ans1=='Y');
return 0;
}
string Student::getName()
{
return name;
}
string Student::getSurname()
{
return surname;
}
string Student::getID()
{
return ID;
}
void Student::setName(string stdName)
{
name=stdName;
}
void Student::setSurname(string stdSurname)
{
surname=stdSurname;
}
void Student::setID(string stdID)
{
ID=stdID;
}
我的问题是在案例 4 中,我将在仅使用二进制搜索进行搜索时进行快速排序,其他搜索功能运行良好,我找不到解决方法
我的排序功能
int quick_sort_help(string &text,int left, int right)
{
char val = text[right];
char temp;
int j = right;
int i = left - 1;
while (true)
{
while (text[++i] < val);
while (text[--j] > val) {
if(j == left)
break;
}
if(i >= j)
break;
temp=text[i];
text[i]=text[j];
text[j]=temp;
}
temp=text[i];
text[i]=text[right];
text[right]=temp;
return i;
}
void quicksort(string &text,int left, int right)
{
if (left < right)
{
int pivot = quick_sort_help(text, left, right);
quicksort(text, left, pivot - 1);
quicksort(text, pivot + 1, right);
}
}
void quick_sort(string &text,int size){
quicksort(text,0,size-1);
}
我将这个快速排序函数与二维数组一起使用:
案例4
case 4:
{
cout<<"Enter the student ID:";
cin>>id;
for(int i=0;i<temp;i++){
for(int j=i+1;j<temp;j++){
quick_sort(myStudent.studentArray[temp][0],temp);
}
}
int binary=temp/2;
for(int i=0;i<temp;i++)
{
if(myStudent.studentArray[binary][0]>id)
{
binary = binary - binary/2;
}
if(myStudent.studentArray[binary][0]<id)
{
binary = binary + binary/2;
}
if(myStudent.studentArray[binary][0]==id)
{
cout<<myStudent.studentArray[binary][0]+ " " + myStudent.studentArray[binary][1]+ " " + myStudent.studentArray[binary][2]<<endl;
break;
}
}
break;
}
“我的新分类”
void quickSort(Student arr[], int left, int right)
{
int i = left, j = right;
Student tmp;
int pivot = arr[(left + right) / 2].getID();
/* partition */
while (i <= j) {
while (arr[i].getID() < pivot)
i++;
while (arr[j].getID() > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
/* recursion */
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
}
“我的二分搜索”
int binary=temp/2;
for(int i=0;i<temp;i++)
{
if(myStudent[binary].getID()>srcid)
{
binary = binary - binary/2;
}
if(myStudent[binary].getID()<srcid)
{
binary = binary + binary/2;
}
if(myStudent[binary].getID()==srcid)
{
cout<<myStudent[binary].getID()+ " " + myStudent[binary].getName()+ " " + myStudent[binary].getSurname()<<endl;
break;
}