问题
What is wrong with my bubble sort code and how do I get it to write out after sorting (before the Linesearch).
I've used code based on the only example I could find in the book. Searched around the net for some guide on how to sort an array list by age but I could not find one (at least, not one that wasn't too advanced for me). So I'm back with another piece of code that probably will make your eyes bleed ^^ Sorry.
#include <iostream>
#include <string>
using namespace std;
class person
{
public:
string name;
int age;
void SetInfo(const string _name, int _age)
{
name = _name;
age = _age;
}
int getAge(){ return age; }
};
int Linearsearch(person* personarray, int key)
{
for (int i = 0; i < 10; i++)
{
if (personarray[i].age == key)
return i;
}
return -1;
}
void bubblesort(person mylist[], int n)
{
for (int i = 1; i<n; i++)
{
for (int j = 0; j<n - 1; j++)
{
if (mylist[j].getAge() > mylist[j + 1].getAge())
{
person temp;
temp = mylist[j];
mylist[j] = mylist[j + 1];
mylist[j + 1] = temp;
}
}
}
}
int main()//start of program
{
person mylist[4];//List of people
mylist[0].SetInfo("Calle", 22);
mylist[1].SetInfo("Björn", 20);
mylist[2].SetInfo("Larry", 60);
mylist[3].SetInfo("Lena", 54);
//calling bubblesort()
bubblesort(mylist, 4);
int index = Linearsearch(mylist, 20);
if (index == -1)
cout << "person not found!";
else
cout << "the person you searched for " << mylist[index].name;
cin.get();
return 0;
system("pause");
}
I've commented the errors I get from the code I have with //.
First of all, I do not even know it the bubble sort code I have here will target the age and sort it like I would like it to.
I've tried the rest of the code without the bubble sort code and it actually works just fine (:D).
Any and all help with the bubble sort code or how to get it to show after sorting would be nice.
Please vote down my question or tell me how I could reform it to make it less annoying instead of just complaining. And feel free to help me edit anything in my question (if you can).
回答1:
First thing you have declared mylist
in main function and trying to access it in bubblesort
function which is not a part of person class. Other thing is that you cannot compare two object as you are doing to sort the list. You can sort mylist object list on the basis of age member variable. Add getAge()
method in person class.
class person
{
public:
string name;
int age;
void SetInfo(const string _name, int _age)
{
name = _name;
age = _age;
}
int getAge(){return age;}
};
void bubblesort(person mylist[],int n)
{
for (int i = 1; i<n; i++)
{
for (int j = 0; j<n - 1; j++) //n is "undefined" should n be the number of objects in the list maybe?
{
if (mylist[j].getAge() > mylist[j + 1].getAge()) //the first mylist is "undefined".
{
person temp;
temp = mylist[j];
mylist[j] = mylist[j + 1];
mylist[j + 1] = temp;
}
}
}
}
int main()//start of program
{
person mylist[4];//List of people
mylist[0].SetInfo("Calle", 22);
mylist[1].SetInfo("Björn", 20);
mylist[2].SetInfo("Larry", 60);
mylist[3].SetInfo("Lena", 54);
//calling bubblesort()
bubblesort(mylist,4);
//list.display(); //list is undefined.
int index = Linesearch(mylist, 20);
if (index == -1)
cout << "person not found!";
else
cout << "the person you searched for " << mylist[index].name;
cin.get();
return 0;
system("pause");
}
I think this should work. phewww...
回答2:
Change
for (int j = 0; j<n - 1; j++)
to
for (int j = 0; j<i - 1; j++)
in your bubblesort function
回答3:
i just took a quick look but i don't think you declared n before you used it in bubble sort. also, i don't think that what you do there is bubble sort. try this:
void bubblesort()
{
int k=1;
while (k==1)
{
k=0;
for (i=1;i<=n;i++)
{
if (mylist[i]>=mylist[i-1])
{
temp=mylist[i];
mylist[i]=mylist[i-1];
mylist[i-1]=temp;
k=1;
}
}
}
}
modify it for your example and i'm sure it will work :)
来源:https://stackoverflow.com/questions/21991369/bubble-sort-c