[英]Recursion in C - Bubble sort?
最后對代碼量表示歉意,因為有一點我將保留到最后。 我正在將一些變量讀取到結構中,並且我想對它們進行重新排序,以便最接近0的數字在頂部(它們都是負值,因為它們是分貝讀數)。 如果我沒記錯的話,我想我可能已經做了冒泡的排序(或進行了一些恐怖的嘗試),我決定應該遞歸地解決這個問題,我可以想到2或3種方法來解決這個問題也許更多的代碼,但不那么麻煩,但我努力了,而且行得通!
但是...一切都進行得很好,但是最后我得到了一個隨機重復。.我有10個節點,從nodes [0]到nodes [9]。 我特意聲明5之后(包括5之后)的所有內容都為null,只是為了測試如果沒有完整的10個條目,它將停止。 我的printf指示它停止了,但是最后我將5個值之一復制到節點中[9]?
我很累,所以它可能盯着我,但我真的不明白這是怎么回事。 任何提示/技巧將不勝感激。 同樣,任何關於不良做法或提高代碼效率的方法的觀察也不會被拒絕!
不必擔心硬編碼值或全局變量,它是用於嵌入式設備的,並且該結構將由另一個函數填充。 另外,我在下面的代碼中留了所有打印語句供調試,以供任何試圖對其進行快速編譯的人使用。
謝謝
#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)對於您正在使用的項目數,冒泡排序是可以的。 但是,如果您需要對很多東西進行排序(例如幾個數量級),則可能需要切換到其他排序算法。 平均情況下,冒泡排序為O(n ^ 2)。 (來源: https : //en.wikipedia.org/wiki/Bubble_sort#Performance )
2)遞歸通常用於“分而治之”式的排序算法,例如QuickSort。 氣泡排序可以輕松地迭代實現。 (例如: https : //en.wikipedia.org/wiki/Bubble_sort#Implementation )
3)如果您在具有嚴格內存限制的嵌入式系統上運行此程序,則遞歸可能是一個不好的選擇。 遞歸通常需要具有較大的堆棧空間來支持您正在進行的嵌套函數調用。 您可以使用尾部遞歸來解決此問題,但必須確保編寫了遞歸函數來支持該功能。 (尾遞歸的示例: https : //en.wikipedia.org/wiki/Tail_call#Example_programs )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.