简体   繁体   English

C中的递归-冒泡排序?

[英]Recursion in C - Bubble sort?

Apologies in advance for the amount of code at the end, because there is a bit I Will leave it until last. 最后对代码量表示歉意,因为有一点我将保留到最后。 I am reading some variables into a struct, and I want to re-order these so the number closest to 0 is at the top (they are all minus values as they are decibel readings). 我正在将一些变量读取到结构中,并且我想对它们进行重新排序,以便最接近0的数字在顶部(它们都是负值,因为它们是分贝读数)。 If I remember my stuff correctly I think I may have done a bubble sort (or some horrible attempt of) Anyway, I decided that I should have a go at solving this with recursion, I can think of 2 or 3 ways to do this with maybe more code but less of a headache but I had a go, and it works! 如果我没记错的话,我想我可能已经做了冒泡的排序(或进行了一些恐怖的尝试),我决定应该递归地解决这个问题,我可以想到2或3种方法来解决这个问题也许更多的代码,但不那么麻烦,但我努力了,而且行得通!

However... everything sorts perfectly but I am left with a random duplicate at the end.. I have 10 nodes, nodes[0] to nodes[9]. 但是...一切都进行得很好,但是最后我得到了一个随机重复。.我有10个节点,从nodes [0]到nodes [9]。 I deliberately declare everything after and including 5 as null simply as a test that it will stop if it doesn't have a full 10 entries. 我特意声明5之后(包括5之后)的所有内容都为null,只是为了测试如果没有完整的10个条目,它将停止。 My printf's indicate that it stops, yet I end up with one of my 5 values being duplicated into nodes[9]? 我的printf指示它停止了,但是最后我将5个值之一复制到节点中[9]?

I'm quite tired so it may be staring me in the face but I really can't understand how this is happening. 我很累,所以它可能盯着我,但我真的不明白这是怎么回事。 Any hints/tips would be appreciated. 任何提示/技巧将不胜感激。 Also any observations about bad practice or ways to improve the efficiency of my code would not be frowned on! 同样,任何关于不良做法或提高代码效率的方法的观察也不会被拒绝!

Don't worry about the hard coded values or global variables, it is for an embedded device and the struct will be populated by another function. 不必担心硬编码值或全局变量,它是用于嵌入式设备的,并且该结构将由另一个函数填充。 Also, I have left all print statements for debugging in the below code for anyone that tries to give it a quick compile. 另外,我在下面的代码中留了所有打印语句供调试,以供任何试图对其进行快速编译的人使用。

Thanks 谢谢

#define MAX_NAME_SIZE 16
#define MAX_RSSI_SIZE 5

struct route_data
{
    char* ssid;

    int rssi;
};

struct route_data nodes[9];
struct route_data temp;


void main(){
nodes[0].ssid = "N1";
nodes[0].rssi = -20;
nodes[1].ssid = "N2";
nodes[1].rssi = -40;
nodes[2].ssid = "N3";
nodes[2].rssi = -34;
nodes[3].ssid = "N4";
nodes[3].rssi = -27;
nodes[4].ssid = "N5";
nodes[4].rssi = -80;

nodes[5].ssid = "NULL";
nodes[6].ssid = "NULL";
nodes[7].ssid = "NULL";
nodes[8].ssid = "NULL";
nodes[9].ssid = "NULL";

int y =0;

while(y!=10){
printf("Node[%d] -\n\t%s\t %d\n\n",y,nodes[y].ssid, nodes[y].rssi);
y++;
}

chooseBest(0,1);

y=0;

while(y!=10){
printf("Node[%d] -\n\t%s\t %d\n\n",y,nodes[y].ssid, nodes[y].rssi);
y++;
}

}

int chooseBest(int x, int i){

    //No point comparing the same values, increment up
    if(x==i){
    printf("X and I match - x %d\t i %d\n\n",x,i);
    i++;
    chooseBest(x,i);
    }
    //if X is less than I, and I is not null, check if i is greater than x and then swap
    else if((nodes[x].rssi<nodes[i].rssi)&&(nodes[i].rssi!=0)){
    printf("\nNode X Rssi %d\t Node I Rssi %d\n",nodes[x].rssi,nodes[i].rssi);
    printf("Node[X] is smaller than I - i %d\n",i);
    printf("X - %d\tI - %d\n\n",x,i);
        if(i>x){
            if(i!=0){
                temp.ssid = nodes[x].ssid;
                temp.rssi = nodes[x].rssi;

                nodes[x].ssid = nodes[i].ssid;
                nodes[x].rssi = nodes[i].rssi;

                nodes[i].ssid = temp.ssid;
                nodes[i].rssi = temp.rssi;
            }
        }
    i++;
    chooseBest(x,i);
    }
    //Is X greater than I and X is not null? If so, is X greater than I. Swap values
    else if((nodes[x].rssi>nodes[i].rssi)&&(nodes[x].rssi!=0)){
    printf("Node[X] is bigger\n");
    printf("X - %d\tI - %d\n\n",x,i);
        if(x>i)
        {
            temp.ssid = nodes[x].ssid;
            temp.rssi = nodes[x].rssi;

            nodes[x].ssid = nodes[i].ssid;
            nodes[x].rssi = nodes[i].rssi;

            nodes[i].ssid = temp.ssid;
            nodes[i].rssi = temp.rssi;

        }
    i++;
    chooseBest(x,i);
    }
    else{
        //If X is null we have traversed all values
        if(nodes[x].rssi==0){
            printf("Nodes[x] equals null\n");
            printf("X - %d\tI - %d\n\n",x,i);
        return 0;
        }
        //Otherwise, we have reached the end of I and need to increment X and reset I
        else{
        printf("About to increment X\n");
        printf("X - %d\tI - %d\n\n",x,i);
        x++;
        i=0;
        chooseBest(x,i);
        //printf("End of the line\n");
        }

    }

您仅分配了9个节点,而您正在使用10个。这很可能会导致问题。

1) For the number of items you're using, bubble sort would be fine. 1)对于您正在使用的项目数,冒泡排序是可以的。 However, if you need to sort a lot of things (like a few more orders of magnitude), you may want to switch to a different sorting algorithm. 但是,如果您需要对很多东西进行排序(例如几个数量级),则可能需要切换到其他排序算法。 Bubble sort is O(n^2) on the average case. 平均情况下,冒泡排序为O(n ^ 2)。 (source: https://en.wikipedia.org/wiki/Bubble_sort#Performance ) (来源: https : //en.wikipedia.org/wiki/Bubble_sort#Performance

2) Recursion is usually used for "divide and conquer"-style sorting algorithms like QuickSort. 2)递归通常用于“分而治之”式的排序算法,例如QuickSort。 Bubble Sort can easily be implemented iteratively. 气泡排序可以轻松地迭代实现。 (example: https://en.wikipedia.org/wiki/Bubble_sort#Implementation ) (例如: https : //en.wikipedia.org/wiki/Bubble_sort#Implementation

3) If you are running this on an embedded system with strict memory limitations, recursion may be a bad choice. 3)如果您在具有严格内存限制的嵌入式系统上运行此程序,则递归可能是一个不好的选择。 Recursion typically requires having a large stack space to support the nested function calls you're making. 递归通常需要具有较大的堆栈空间来支持您正在进行的嵌套函数调用。 You can remedy this with tail recursion, but you'd have to make sure your recursive functions are written to support that. 您可以使用尾部递归来解决此问题,但必须确保编写了递归函数来支持该功能。 (example of tail recursion: https://en.wikipedia.org/wiki/Tail_call#Example_programs ) (尾递归的示例: https : //en.wikipedia.org/wiki/Tail_call#Example_programs

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

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