我的快速排序代码有问题。我不知道为什么,但它没有排序。
我的计划
#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;
}
我的问题是在CASE 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;
}
答案 0 :(得分:1)
你的代码很严重, 你的快速搜索似乎没问题,但你的程序不是。
我稍微修改了你的代码,你仍然可以做更多。
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
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(Student students[],int left, int right)
{
Student val = students[right];
Student temp;
int j = right;
int i = left - 1;
while (true)
{
while (students[++i].getID() < val.getID());
while (students[--j].getID() > val.getID()) {
if(j == left)
break;
}
if(i >= j)
break;
temp=students[i];
students[i]=students[j];
students[j]=temp;
}
temp=students[i];
students[i]=students[right];
students[right]=temp;
return i;
}
void quicksort(Student students[],int left, int right)
{
if (left < right)
{
int pivot = quick_sort_help(students, left, right);
quicksort(students, left, pivot - 1);
quicksort(students, pivot + 1, right);
}
}
void quick_sort(Student students[],int size){
quicksort(students,0,size-1);
}
int main()
{
Student myStudent[100];
string name;
string surname;
string id;
int choice;
int temp=0;
char ans1;
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[temp].setName(name);
myStudent[temp].setSurname(surname);
myStudent[temp].setID(id);
cout<<"Want to add new Student? (y/n)";
cin>>ans1;
temp++;
}while(ans1=='y'||ans1=='Y');
quick_sort(myStudent, temp);
do{
cout<<"What do you want to search with?"<<endl;
cout<<"1-)Search with name:"<<endl;
cout<<"2-)Search with surname:"<<endl;
cout<<"3-)Search with ID (binary!):"<<endl;
cout<<"4-)Show List!"<<endl;
cin>>choice;
switch (choice)
{
case 1:
{
cout<<"Enter the student name:";
cin>>name;
for(int i=0;i<temp;i++)
{
if(myStudent[i].getName()==name)
{
cout<<myStudent[i].getID() + " " + myStudent[i].getName() + " " + myStudent[i].getSurname()<<endl;
}
}
break;
}
case 2:
{
cout<<"Enter the student surname:";
cin>>surname;
for(int i=0;i<temp;i++)
{
if(myStudent[i].getSurname()==surname)
{
cout<<myStudent[i].getID() + " " + myStudent[i].getName() + " " + myStudent[i].getSurname()<<endl;
}
}
break;
}
case 3:
{
cout<<"Enter the student ID:";
cin>>id;
int left=0;
int right = temp;
int mid = (right + left)/2;
while(left <= right){
if(myStudent[mid].getID()>id)
{
right = mid - 1;
mid = (right+left)/2;
}
if(myStudent[mid].getID()<id)
{
left = mid + 1;
mid = (right+left)/2;
}
if(myStudent[mid].getID()==id)
{
cout<<myStudent[mid].getID()+ " " + myStudent[mid].getName()+ " " + myStudent[mid].getSurname()<<endl;
break;
}
}
break;
}
case 4:
{
cout<<"id/name/surname"<<endl;
for(int i=0;i<temp;i++)
{
cout<<myStudent[i].getID()+ " " + myStudent[i].getName() + " " + myStudent[i].getSurname()<<endl;
}
break;
}
}
cout<<"Do you want to continue?(y/n)"<<endl;
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;
}