使用指针进行冒泡排序

时间:2012-11-09 14:44:04

标签: c++ sorting pointers

不确定我是否正确,或者即使我允许使用指针进行排序。我创建了一个具有指向其他元素的指针的结构。然后制作了这个结构的矢量,并尝试对它进行一些交换。

起初我在交换操作中使用了解引用运算符,但是当我这样做时,我的结果变得奇怪,我在最终结果中得到了一堆重复的#(它们应该都是唯一的),拉动dereference off解决了这个问题,但它没有排序(除非可能是通过内存地址)。

所以这是我的代码,关注的领域是sortSuperStruct函数

#include <algorithm> //used for swap, would like to use for sort, but oh well
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
//#include <math.h>
#include <string>
#include <sstream>
#include <vector>
//------------------------------------------------------------------------------
//namespaces?
//------------------------------------------------------------------------------
using namespace std;
//------------------------------------------------------------------------------
//prototypes
//------------------------------------------------------------------------------

struct superStruct
{
    //probably not needed
    float *percent;

    //copy of Dwarf's role pPercent;
    float *pPercent;
    int *roleName;
    int *name;

};

//parallel array to follow along with Dwarf role
struct roles
{
    int name;
    float priority;
    int numberToAssign;

    void setRolePriorities ()
    {
        priority = (float)rand()/(float)RAND_MAX;
    }

    void setNumberToAssign(int n)
    {
        numberToAssign = rand() % n + 1;

    }
};

//a fit for a role, a Dwarf has this
struct role
{
    float percent;
    //copy of roles name
    int *roleName;
    //copy of roles priority
    float *rolePriority;
    float pPercent;

    void set_pPercent()
    {
        pPercent = percent * (*rolePriority);

    };

};

struct Dwarf
{
    vector <role> roleValues;

    int *maxLabor;

    int name;

    void setRoleValues(int n, vector <roles> *roleS)
    {
        srand ( time(NULL) );
        //rolesValues

        roleValues.resize(n);

        //set role %'s
        for (int x = 0; x < n; x++)
        {
            roleValues[x].percent = (float)rand()/(float)RAND_MAX;
            //cout << roleValues[x].percent << endl;
            //I said I'm pointing to the address value of roleS...
            roleValues[x].roleName = &roleS->at(x).name;
            roleValues[x].rolePriority = &roleS->at(x).priority;
            roleValues[x].set_pPercent();
        }
    };
};

void initializeSuperStruct(vector <superStruct> *s, vector <Dwarf> *d, int n)
{
    //for each Dwarf
    int counter = 0;
    for (int x = 0; x < d->size(); x++)
    {
        //for each role
        for (int y = 0; y < n; y++)
        {
            s->at(counter).pPercent = &d->at(x).roleValues[y].pPercent;
            s->at(counter).name = &d->at(x).name;
            //both are pointers.
            s->at(counter).roleName = d->at(x).roleValues[y].roleName;
            //cout << d->at(x).roleValues[y].percent << endl;
            s->at(counter).percent = &d->at(x).roleValues[y].percent;

            counter++;
        }

    };


};

void printSuperStruct(vector <superStruct> *s)
{
    for (int x = 0; x < s->size(); x++)
    {
        cout << "Count: " << x << endl;
        cout << "Name: " << *s->at(x).name << endl;
        cout << "Percent: " << *s->at(x).percent << endl;
        cout << "pPercent: " << *s->at(x).pPercent << endl;
        cout << "Role Name: " << *s->at(x).roleName << endl;
    }
};

void sortSuperStruct(vector <superStruct> *s)
{
    //runs through list
    for (int i = 0; i < s->size(); i++)
    {
        //checks against lower element
        //cout << *s->at(i).pPercent << endl;
        for (int j = 1; j < (s->size()-i); j++)
        {
            //using pointer dereferences messed this up, but, not using them doesn't sort right
            if (*s->at(j-1).pPercent < *s->at(j).pPercent)
            {
                //cout << *s->at(j-1).pPercent << "vs. " << *s->at(j).pPercent << endl;

                //I don't think swap is working here
                //superStruct temp;
                //temp = s->at(j-1);
                //s->at(j-1) = s->at(j);
                //s->at(j) = temp;
                swap(s->at(j-1), s->at(j));
                //cout << *s->at(j-1).pPercent << "vs. " << *s->at(j).pPercent << endl;
            }
        }
    }

}

int main()
{
    srand (time(NULL));

    int numberOfDwarves;
    int numberOfRoles;
    int maxLabors;

    vector <superStruct> allRoles;
    vector <Dwarf> myDwarves;
    vector <roles> myRoles;

    cout << "number of Dwarf's: " << endl;
    cin >> numberOfDwarves;

    cout << "max labor per dwarf" << endl;
    cin >> maxLabors;

    cout << "number of Roles: " << endl;
    cin >> numberOfRoles;

    //this will probably have to be a vector
    //Dwarf myDwarfs[numberOfDwarves];

    myDwarves.resize(numberOfDwarves);
    allRoles.resize(numberOfDwarves*numberOfRoles);
    myRoles.resize(numberOfRoles);

    //init roles first
    for (int x = 0; x < numberOfRoles; x++)
    {
        myRoles[x].name = x;
        myRoles[x].setRolePriorities();
        myRoles[x].setNumberToAssign(numberOfDwarves);
    }

    for (int x = 0; x < numberOfDwarves; x++)
    {
        myDwarves[x].setRoleValues(numberOfRoles, &myRoles);
        myDwarves[x].name = x;

        //messy having this here.
        myDwarves[x].maxLabor = &maxLabors;

    }

    initializeSuperStruct(&allRoles, &myDwarves, numberOfRoles);
    cout << "Before sort, weighted matrix" << endl;
    system("pause");
    printSuperStruct(&allRoles);
    sortSuperStruct(&allRoles);
    system("pause");
    cout << "After sort, weighted matrix" << endl;
    printSuperStruct(&allRoles);
}

输出:

    Role Name: 5
    Count: 96
    Name: 6
    Percent: 0.175787
    pPercent: 0.0178163
    Role Name: 5
    Count: 97
    Name: 7
    Percent: 0.175787
    pPercent: 0.0178163
    Role Name: 5
    Count: 98
    Name: 8
    Percent: 0.175787
    pPercent: 0.0178163
    Role Name: 5
    Count: 99
    Name: 9
    Percent: 0.175787
    pPercent: 0.0178163
    Role Name: 5

2 个答案:

答案 0 :(得分:2)

srand (time(NULL));

main开头调用一次。在各种方法中使用它意味着您将获得大量重复值。

if (s->at(j-1).pPercent < s->at(j).pPercent)

这确实比较了指针值,因为指针不一定指向同一个数组,具有未定义的行为(可能会比较虚拟地址)。如果要按值排序,则需要取消引用

if (*s->at(j-1).pPercent < *s->at(j).pPercent)

并在

    for (int j = 1; j < (s->size()-1); j++)
    {
        //using pointer dereferences messed this up, but, not using them doesn't sort right
        if (s->at(j-1).pPercent < s->at(j).pPercent)

你永远不会考虑最后一个元素,那应该是

for(unsigned int j = 1; j < s->size() - i; ++j)

进行适当的冒泡排序。

答案 1 :(得分:0)

你的泡泡排序对我来说似乎不对。

它应该类似于:

void sortSuperStruct(vector <superStruct>& s)
  for(int i = 0; i < s.size()-1; i++){
    for(int j = i; j < s.size(); j++){
      if(*(s[i].pPercent) < *(s[j].pPercent)){
        swap(s.at(i),s.at(j));
      }
    }
  }
}

主要差异:

  • 我从0变为size-1,而不是0变为size
  • j从i变为大小,而不是1变为size-1
  • if语句测试i和j,而不是j和j-1
  • 交换也交换i和j,而不是j和j-1

细微差别:

  • 向量通过引用而不是指针传递。这可能并不重要,但至少对我来说看起来更干净。

请注意,这将从最大到最小排序。如果你想要它从最小到最大,你的if语句应该有一个&gt;而是签字。