简体   繁体   English

排序算法未在for循环中递增

[英]Sorting algorithm not incrementing in for loop

void sortRooms()

I came up with this algorithm to sort my room struct. 我想出了这种算法来排序我的房间结构。 Inside room struct there are only 3 values (50, 120, 30) which is why I have to decrease the index by at the start and increase dec by 1 everytime. 在房间结构中,只有3个值(50、120、30),这就是为什么我必须在开始时将索引减小,每次将dec增大1的原因。 However, when i compile and it prints out 但是,当我编译并打印出来时

OHE 100 120
SAL 210 30
OHE 120 50

I traced out the steps on paper but I cant seem the follow why the index is not incrementing correctly so that it would go on to compare 120 with 50 and then proceed to switch those values. 我找出了纸上的步骤,但似乎无法理解为什么索引未正确递增,因此它会继续将120与50进行比较,然后继续切换这些值。 Any help would be appreciated. 任何帮助,将不胜感激。

Why don't you change the while loop into a for loop? 为什么不将while循环更改为for循环? For example: 例如:

for (int dec=2; (jk[0]-dec+1) > 1; dec++)
{
    if (room[i].max_students > room[jk[0] - dec].max_students)
    {            
        TEMPbuilding_code = room[i].building_code;
        room[i].building_code = room[jk[0] - dec].building_code;
        room[jk[0] - dec].building_code = TEMPbuilding_code;

        TEMProom_number = room[i].room_number;
        room[i].room_number = room[jk[0] - dec].room_number;
        room[jk[0] - dec].room_number = TEMProom_number;

        TEMPmax_students = room[i].max_students;
        room[i].max_students = room[jk[0] - dec].max_students;
        room[jk[0] - dec].max_students = TEMPmax_students;
    }
}

Also, did your order of operations in the while loop mess up? 另外,您在while循环中的操作顺序是否混乱? I suppose it should be jk[0] + 1 - dec or jk[0] - dec + 1 ? 我想应该是jk[0] + 1 - decjk[0] - dec + 1

For i=0 , you find the largest element (of all elements, except the last one, since dec starts off at 2 , not 1 ) and put it in the 0th position. 对于i=0 ,您找到(除最后一个元素外的所有元素中的最大元素,因为dec2开始,而不是1 ),并将其放在第0个位置。

Then, for i=1 , you find the largest element (again of all elements, except the last one, which includes the first) and put it in the 1st position. 然后,对于i=1 ,您找到最大的元素(同样,除最后一个元素(包括第一个元素)之外的所有元素)并将其放在第一个位置。

And so on. 等等。

You can't sort like that - sure, the largest element will end up at the end (which, based on your comment to the other answer, isn't what you want), but there's nothing ensuring that any of the other elements are actually sorted. 您无法像这样进行排序-当然,最大的元素会最后出现(根据您对其他答案的评论,这不是您想要的),但是无法确保其他任何元素都是实际排序。

If you want to have it sorted from largest to smallest, in your inner loop, you need to stop when you get to i , so you're only picking the largest element of the remaining elements. 如果要在内部循环中按从大到小的顺序对其进行排序,则需要在到达i时停止,因此您只选择其余元素中的最大元素。

This is very similar to selection sort , so you could look at that for further information, or optimizations (like keeping track of the maximum and just swapping at the end, rather than swapping at each bigger element we find). 这与选择排序非常相似,因此您可以查看它以获取更多信息或进行优化(例如跟踪最大值并在最后交换,而不是在找到的每个较大元素处交换)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM