2014-02-24 279 views
0

我的气泡排序代码出现了什么问题,以及如何在排序后(Lisesarch之前)将它写出来。冒泡排序。 C++

我已经使用了基于本书中唯一例子的代码。在网上搜索了一些关于如何按年龄对数组列表进行排序的指南,但是我找不到一个(至少,对我来说不是那么先进的)。所以我回来了另一段代码,可能会让你的眼睛流血^^对不起。

#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"); 
} 

我评论过我从代码中得到的错误//。

首先,我甚至不知道我在这里的气泡排序代码将针对年龄并按照我喜欢的方式进行排序。

我已经尝试了没有气泡排序代码的其余代码,它实际上工作得很好(:D)。
任何和所有的帮助泡沫排序代码或如何让它在排序后显示将是很好的。 请投我的问题或告诉我如何改革它,使其不那么烦恼,而不是抱怨。并且随时可以帮助我编辑我的问题(如果可以的话)。

+2

编译器告诉你主要问题是什么 - 你需要传递数组和列表大小作为参数/参数到汇编,否则它不能在它们上操作,因为它们被声明为main(locals) )或根本没有:( –

+0

所以在这种情况下,“n”需要是4?:(我是瑞典语,所以英语很难理解有时>如果我有这个权利,我可以将n重命名为max,设置max为4?为了让我更清楚 – Nyp0ns0pp0saurus

+0

首先 - 你为什么要使用变量,这些变量在任何地方都没有声明?(比如像'ortures'或'list'中的'n')尝试读一些关于C++ (最好是最基本的教程) – zoska

回答

1

你有第一件事在主函数中声明mylist并试图在bubblesort函数中访问该函数,该函数不属于人类的一部分。其他的事情是,你不能比较两个对象,因为你正在对列表进行排序。您可以基于年龄成员变量对mylist对象列表进行排序。亲自加班getAge()方法。

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"); 
} 

我认为这应该工作。 phewww ...

+0

谢谢队友!所以我应该在person类中的字符串名称和int年龄之后添加一个getage()?究竟会做什么以及如何制作该方法? – Nyp0ns0pp0saurus

+0

我会编辑我的答案,并尝试涵盖一切... – HadeS

+0

希望这会帮助你.. – HadeS

1

我只是简单地看了一下,但我认为你在泡泡分类中使用它之前并没有声明n。 也,我不认为你在那里做的是泡沫排序。试试这个:

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; 
       } 
      } 
     } 
} 

修改为你的榜样,我相信它会工作:)

+0

所以没有温度?:o所以,而替换我的空泡沫?并且aux的立场是什么?我对bubblesort和C++初学者都很陌生,所以我真的需要了解这个伴侣。对不起,有点愚蠢^^并感谢您的回答,我会尝试您提供的代码,尽我所能! – Nyp0ns0pp0saurus

+0

我使用了aux。它对temp做的是同样的事情。对不起,我没有正确解释,我很着急 – FilipMarusca

+0

不是很冒泡,但至少它打破了noswap的外层循环,这是非常重要的部分。你内在的循环应该是'i WhozCraig

1

变化

for (int j = 0; j<n - 1; j++) 

for (int j = 0; j<i - 1; j++) 

在冒泡功能