简体   繁体   English

在冒泡排序中显示 2 个相同的值

[英]Display 2 of the same values in a bubble sort

I am sorting values to find the top 3 values.我正在对值进行排序以找到前 3 个值。 However my bubble sort only can display one value if there is 2 same highest value.但是,如果有 2 个相同的最高值,我的冒泡排序只能显示一个值。 for example: output:例如: 输出:

Please select broadband type (1 - DSL): 1
 3800 , month 1
 1700 , month 3
 1400 , month 4

expected output:预期输出:

Please select broadband type (1 - DSL): 1
 3800 , month 1
 3800 , month 2
 1400 , month 4

Array File:数组文件:

int file[3][6] =
{
    { 3800, 3800, 1700, 1400, 1300, 1285 },
    { 106900, 100400, 89600, 76900, 61500, 59200 },
    { 1260300, 1269900, 1285400, 1298800, 1316900, 1401280 }
};

Function:功能:

{

    int type3, c, d, highest = 0, prehighest = 99999999999, month = 0;

    printf("1. DSL \n");
    printf("2. Cable Modem \n");
    printf("3. Fibre Based \n");

    printf("Please select broadband type (1 - DSL): ");
    scanf(" %d", &type3);

    if (type3 <= 3)
    {
        for (d = 0; d < 3; d++)
        {
            for (c = 0; c < 6; c++)
            {
                if (file[type3 - 1][c] > highest && file[type3 - 1][c] < prehighest)
                {
                    highest = file[type3 - 1][c];
                    month = c + 1;
                }
            }

            if (file[type3 -1][3])
                printf(" %d , month %d \n", highest, month);

            prehighest = highest;
            highest = 0;
        }
    }
    else
    {
        printf("Error! Please enter a valid option. \n");
    }
    type3 =getchar();
}

OK, you want to find the 3 highest values...好的,您想找到 3 个最高值...

At first, as you allow negative values, you should check lower bound, too (actually, you'd even need to check for 0 for unsigned as well), so first:首先,当您允许负值时,您也应该检查下限(实际上,您甚至还需要检查 0 是否为无符号),因此首先:

if (1 <= type3 && type3 <= 3)

Then for not having to subtract each time in the loop, I'd do that once before:然后为了不必在循环中每次都减去,我之前会做一次:

{
    --type3;

Actually you don't do any bubble sort, as you don't modify the input array.实际上,您不进行任何冒泡排序,因为您不修改输入数组。 You would have to bubble up:你必须冒泡:

if(file[type3 - 1][c] > file[type3 - 1][c - 1]
{
    // swap positions c and c - 1!!!
}

For above, you'd have to start counting from 1, of course...对于上面,你必须从 1 开始计数,当然......

Then after sorting, you'd find the elements you look for at the very end.然后排序之后,您会在最后找到您要查找的元素。

Your code instead looks for the highest element not yet found, though (as is, complexity is just the same as with bubble sort: O(k*n) , if you want to find the k largest elements out of n ones – there are ways to do better, but these would be overkill for such little set of inputs...).您的代码反而会查找尚未找到的最高元素(按原样,复杂性与冒泡排序相同: O(k*n) ,如果您想从n个元素中找到k最大的元素 – 有可以做得更好的方法,但对于这么少的输入集来说,这些方法太过分了……)。

However, your algorithm cannot cope with duplicates;但是,您的算法无法处理重复项; as you modify prehighest to higest after having found first of the duplicates, the condition当您在找到第一个重复项后将prehighest修改为higest ,条件

file[type3 - 1][c] < prehighest

can't be true any more for any one of the further duplicates.对于任何进一步的重复项,都不能再真实了。

You could adjust your algorithm by counting the number of duplicates:您可以通过计算重复次数来调整算法:

if (file[type3 - 1][c] > highest && file[type3 - 1][c] < prehighest)
{
    /* ... */
    count = 1;
}
else
{
    count += file[type3 - 1][c] == highest;
}

Now you can output count times the value you found.现在您可以输出count乘以您找到的值。

So you'll modify the loop, eg:因此,您将修改循环,例如:

for(d = 0; d < 3; d += count)

You might find more values than required, though, so you might do something like:但是,您可能会发现比所需更多的值,因此您可能会执行以下操作:

int min = MIN(count, required);
required -= min;
for(int i = 0; i < min; ++i) { /* print */ };

In outer most loop, you then could then alternatively check for required being != 0:在最外层循环中,您可以选择检查required为 != 0:

for(int required = 3; required; )
{
    for (c = 0; c < 6; c++)
    {
    }
    // print and adjust required
}

With above loop there's a corner case left open, though: If you look for more elements that are contained in the array, you'll end up in an endless loop (would apply for the loop with d += count as well!).但是,在上面的循环中,有一个角落情况是开放的:如果您查找包含在数组中的更多元素,您将最终陷入无限循环(也适用于带有d += count的循环!)。 If that corner case might be realistic in any further scenario, you'd have to handle it explicitly.如果该极端情况在任何进一步的情况下可能是现实的,则您必须明确处理它。

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

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