我遇到了一些麻煩。我們基本上被要求創建一個程序的兩個版本,該程序將對50個項目的數組進行排序,然後顯示LED中最大的數字。我已經使用基本的氣泡排序並顯示它。我的問題是當我必須使用遞歸來做到這一點。不幸的是,我對遞歸的瞭解非常有限,因爲我錯過了關於它的講座 - 他也沒有在線上放置筆記。我有一個很長的谷歌,但仍然無法讓我的頭靠近它。所以我問幾件事。首先,有人可以解釋使用sedo代碼進行氣泡排序的遞歸。其次,我的嘗試完全錯了嗎?遞歸氣泡排序
int numbers[49];
void setup()
{
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
Serial.begin(9600);
}
void loop()
{
resetLEDLow();
genNumbers();
sortNumbers();
displayNumber();
delay(5000);
}
void resetLEDLow()
{
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
}
void genNumbers()
{
for(int i = 0 ; i < 50 ; i++)
{
numbers[i] = random(256);
}
}
void sortNumbers()
{
int sizeOfArray = sizeof(numbers)/sizeof(int);
int check = 1;
if(check != 0)
{
check = 0;
for(int i = 0 ; i < sizeOfArray ; i++)
{
for (int j = 1 ; j < sizeOfArray ; j++)
{
if(numbers[j-1] > numbers[j])
{
int temp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = temp;
check++;
}
}
}
sortNumbers();
}
}
void displayNumber()
{
int i = 12;
int a = numbers[49];
Serial.println(numbers[49]);
while (a > 0)
{
int num = a % 2;
a = a/2;
if(num == 1)
{
digitalWrite(i, HIGH);
}else{
digitalWrite(i, LOW);
}
i--;
}
}
我知道,代碼不工作當它循環輪數被重置爲1這樣的條件永遠不會爲真因而這意味着將環圓,直到永遠。那麼,我需要改變什麼或者我所做的並不是真的遞歸呢?
真的需要一些幫助,讓我的大腦四處走動。
編輯:這是我的新遞歸嘗試。我刪除了與問題無關的代碼。
void loop()
{
int topLevel = 0;
int currentSort = 0;
int sizeOfArray = sizeof(numbers)/sizeof(int);
int numbers[49];
sortNumbers(topLevel,currentSort,sizeOfArray,numbers);
}
int sortNumbers(p,c,l,numbers)
{
// Swap if they are in the wrong order
if(numbers[c-1] > numbers[c])
{
int temp = numbers[c-1];
numbers[c-1] = numbers[c];
numbers[c] = temp;
}
// If we have finished this level and need to go to next level
if(c == l)
{
c = 0;
p++;
}
// Finished
if(p == l+1)
{
return numbers;
}
// Continue to the next place
return sortNumbers(p,c+1,l, numbers);
}
Sort50Rec:-1: error: 'p' was not declared in this scope Sort50Rec:-1: error: 'c' was not declared in this scope Sort50Rec:-1: error: 'l' was not declared in this scope Sort50Rec:-1: error: 'numbers' was not declared in this scope Sort50Rec:-1: error: initializer expression list treated as compound expression Sort50Rec.cpp: In function 'void loop()': Sort50Rec:19: error: 'numbers' was not declared in this scope Sort50Rec:21: error: 'sortNumbers' cannot be used as a function Sort50Rec.cpp: In function 'void genNumbers()': Sort50Rec:42: error: 'numbers' was not declared in this scope Sort50Rec.cpp: At global scope: Sort50Rec:46: error: redefinition of 'int sortNumbers' Sort50Rec:-1: error: 'int sortNumbers' previously defined here Sort50Rec:46: error: 'p' was not declared in this scope Sort50Rec:46: error: 'c' was not declared in this scope Sort50Rec:46: error: 'l' was not declared in this scope Sort50Rec:46: error: 'numbers' was not declared in this scope
我的可以肯定的一部分的方式,我的權利我命名功能,我在想你可以只返回一個值?或不?
編輯:修正錯誤指出新的錯誤。
void loop()
{
int topLevel = 0;
int currentSort = 0;
int numbers [49];
int sizeOfArray = sizeof(numbers)/sizeof(int);
numbers = sortNumbers(topLevel,currentSort,sizeOfArray,numbers);
}
int sortNumbers(int p,int c,int l,int numbers)
{
// Swap if they are in the wrong order
if(numbers[c-1] > numbers[c])
{
int temp = numbers[c-1];
numbers[c-1] = numbers[c];
numbers[c] = temp;
}
// If we have finished this level and need to go to next level
if(c == l)
{
c = 0;
p++;
}
// Finished
if(p == l+1)
{
return numbers;
}
// Continue to the next place
return sortNumbers(p,c+1,l, numbers);
}
Sort50Rec.cpp: In function 'void loop()': Sort50Rec:19: error: invalid conversion from 'int*' to 'int' Sort50Rec:19: error: initializing argument 4 of 'int sortNumbers(int, int, int, int)' Sort50Rec:19: error: incompatible types in assignment of 'int' to 'int [49]' Sort50Rec.cpp: In function 'void genNumbers()': Sort50Rec:40: error: 'numbers' was not declared in this scope Sort50Rec.cpp: In function 'int sortNumbers(int, int, int, int)': Sort50Rec:47: error: invalid types 'int[int]' for array subscript Sort50Rec:47: error: invalid types 'int[int]' for array subscript Sort50Rec:49: error: invalid types 'int[int]' for array subscript Sort50Rec:50: error: invalid types 'int[int]' for array subscript Sort50Rec:50: error: invalid types 'int[int]' for array subscript Sort50Rec:51: error: invalid types 'int[int]' for array subscript Sort50Rec.cpp: In function 'void displayNumber()': Sort50Rec:71: error: 'numbers' was not declared in this scope
感謝
[理解遞歸(應用於泡泡排序)](http://stackoverflow.com/questions/3486452/understanding-recursion-applying-it-on-bubble-sort) –
@OliCharlesworth我將採取現在看看它。謝謝 – Kyle93
@OliCharlesworth我看了一下。但是,我仍然不確定它是如何工作的。你介意擺脫一些光? – Kyle93