我在结构数组中使用冒泡排序时遇到错误

时间:2017-10-12 13:14:07

标签: c sorting structure

我有一个结构

typedef struct ratings {
    int userId;
    int movieId;
    int rating;
}Ratings;

我正在使用冒泡排序按照我的选择进行排序

Ratings rect[64], temp;
      n = 64;

   for (i = 0; i < n-1; i++)
{

   for (j = 0; j < (n - i -1); j++)
    {
      if (REC1[j].movieId >= REC1[j+1].movieId)
       {
         temp = REC1[j];
         REC1[j] = REC1[j + 1];
         REC1[j + 1] = temp;
      }
   }
}

 for (i = 0; i < n-1; i++)
{

   for (j = 0; j < (n - i -1); j++)
    {
      if(REC1[j].movieId == REC1[j+1].movieId ) 
      {
        if (REC1[j].userId >= REC1[j+1].userId)
        {
          temp = REC1[j];
          REC1[j] = REC1[j + 1];
          REC1[j + 1] = temp;
        }
     }

   }
}

我使用上面的逻辑来获得输出并成功。它可以降低到更低吗?请试一试 我的输入是

    1,1,9
    1,2,0
    1,3,2
    2,1,2
    2,2,10
    2,3,10
    3,1,7
    3,2,1
    3,3,9

我希望输出为

1,1,9
2,1,2
3,1,7
1,2,0
2,2,10
3,2,1
1,3,2
2,3,10
3,3,9

为此我使用上面的排序逻辑仍然没有得到结果 This is the output i am getting but i need a the above output

2 个答案:

答案 0 :(得分:0)

您已将Ratings声明为struct ratings的类型。

因此你应该声明一个像下面这样的结构数组

Ratings myratings[100], temp;

if (myratings[i].movieId > myratings[j].movieId)
       {
         temp = myratings[j];/* it might not work if you are not compiling with a C++ compiler better use memcpy function if you are using a C compiler */
         myratings[j] = myratings[j + 1];
         myratings[j + 1] = temp;
      }

请更正您的泡泡短算法,如下所示: -

for (i = 0; i < n-1; i++)
{

   for (j = 0; j < (n - i -1); j++)
    {
      if (rect[j].movieId > rect[j+1].movieId)
       {
         temp = rect[j];
         rect[j] = rect[j + 1];
         rect[j + 1] = temp;
      }
   }
}

如果你想只交换值而不是它应该是的对象      int t;

//This is bubble sort for middle row
for (i = 0; i < n-1; i++)
{

   for (j = 0; j < (n - i -1); j++)
    {
      if (rect[j].movieId > rect[j+1].movieId)
      {
         t = rect[j].movieId;
         rect[j].movieId = rect[j+1].movieId;
         rect[j+1].movieId = t;
      }

   }
}

 //This is bubble sort for 1st row

for (i = 0; i < n-1; i++)
{

   for (j = 0; j < (n - i -1); j++)
    {
      if (rect[j].userId > rect[j+1].userId)
      {
         t = rect[j].userId;
         rect[j].userId = rect[j+1].userId;
         rect[j+1].userId = t;
      }

   }
}

//This is bubble sort for 3rd row


for (i = 0; i < n-1; i++)
{

   for (j = 0; j < (n - i -1); j++)
    {
      if (rect[j].rating > rect[j+1].rating)
      {
         t = rect[j].rating;
         rect[j].rating = rect[j+1].rating;
         rect[j+1].rating = t;
      }

   }
}

你必须在你的代码中添加以上三个while循环

答案 1 :(得分:0)

中的typedef关键字
typedef struct ratings {
    int userId;
    int movieId;
    int rating;
}Ratings, temp;

Ratingstemp定义为类型struct ratings别名,而不是struct ratings类型的对象 }。你想做的是像

struct ratings {
  int userId;
  int movieId;
  int rating;
};

struct ratings Ratings[N], temp; // where N is the size of the array you want.  

typedef struct ratings {
    int userId;
    int movieId;
    int rating;
}Ratings;

Ratings myRatings[N], temp;

我更喜欢第一种形式,我自己。如果该类型的用户必须知道其struct - 即(即必须访问个别成员),我宁愿不在typedef后面隐藏struct类型。