气泡排序。 C ++ [英] bubble sort. C++

查看:63
本文介绍了气泡排序。 C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

气泡排序代码有什么问题,以及如何在排序后(在Linesearch之前)将其写出来。

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.

我已经尝试了其余的代码,但没有冒泡排序代码,并且实际上工作得很好(:D)。

关于此问题的所有帮助冒泡排序代码或如何使其在排序后显示。
请投票否决我的问题,或者告诉我如何进行改革以减少烦恼,而不仅仅是抱怨。并且随时可以帮助我编辑问题中的所有内容(如果可以)。

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).

推荐答案

您声明的第一件事 mylist 在主函数中,并尝试在不属于person类的 bubblesort 函数中访问它。另一件事是,您无法像对列表进行排序那样比较两个对象。您可以根据年龄成员变量对mylist对象列表进行排序。在人员类中添加 getAge()方法。

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

我认为这应该可行。 phewww ...

I think this should work. phewww...

这篇关于气泡排序。 C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆