簡體   English   中英

在無法找到它的結構數組中使用冒泡排序時出現錯誤

[英]I am getting an error while using bubble sort in array of structures not able to find it

我有一個結構

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

為此,我正在使用我上面的排序邏輯仍然沒有得到結果這是我得到的輸出,但我需要上面的輸出

您已經將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 循環

中的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 (即必須訪問單個成員),我寧願不將struct類型隱藏在typedef后面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM