在无法找到它的结构数组中使用冒泡排序时出现错误
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
为此,我使用上面的排序逻辑仍然没有得到结果
This is the output i am getting but i need a the above output
您已经将 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;
将 Ratings
和 temp
定义为类型 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
-ness(即必须访问单个成员),我宁愿不将 struct
类型隐藏在 typedef
后面。
我有一个结构
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
您已经将 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;
将 Ratings
和 temp
定义为类型 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
-ness(即必须访问单个成员),我宁愿不将 struct
类型隐藏在 typedef
后面。