i am a beginner and i need help.
my bubblesort doesnt work...
hope you can find the fault, becuase im a beginnner
//Deklerationen
int array1[8][8];
//Funktionen
void bubbleSort(int array1[8][8]) {
for (int x = 0; x < 8; x++) {
for (int z = 0; z < 8; z++) {
if (array1[x][z] > array1[x][z + 1]) {
int temp = array1[x][z];
array1[x][z] = array1[x][z + 1];
array1[x][z + 1] = temp;
}
}
}
}
void ausgabeArray(int array1[8][8]) {
for (int z = 0; z < 8; z++) {
Serial.println();
for (int s = 0; s < 8; s++) {
Serial.print(String(array1[z]) + "\t");
~~ }~~
~~ }~~
~~ Serial.println();~~ } //Setup wird 1x ausgeführt. void setup() {
~~ // put your setup code here, to run once:~~
~~ Serial.begin(9600);~~
~~ for (int z = 0; z < 8; z++) {~~
~~ for (int s = 0; s < 8; s++) {~~ array1[z] = random(1, 9999); ~~ } } Serial.println("Array mit Zufallszahlen zwischen 1-9999: "); ausgabeArray(array1); bubbleSort(array1); delay(5000); Serial.println("\nNach der Grösse sortiert: "); ausgabeArray(array1); Serial.println("\n\n\n"); } //loop wird immer wieder ausgeführt~~ void loop() { ~~ // put your main code here, to run repeatedly: }~~
Manuel__:
i am a beginner and i need help.
my bubblesort doesnt work...
hope you can find the fault, becuase im a beginnner
//Deklerationen
int array1[8][8];
//Funktionen
void bubbleSort(int array1[8][8]) {
for (int x = 0; x < 8; x++) {
for (int z = 0; z < 8; z++) {
if (array1[x][z] > array1[x][z + 1]) {
int temp = array1[x][z];
array1[x][z] = array1[x][z + 1];
array1[x][z + 1] = temp;
}
}
}
}
void ausgabeArray(int array1[8][8]) {
for (int z = 0; z < 8; z++) {
Serial.println();
for (int s = 0; s < 8; s++) {
Serial.print(String(array1[z][s]) + "\t");
}
}
Serial.println();
}
//Setup wird 1x ausgeführt.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for (int z = 0; z < 8; z++) {
for (int s = 0; s < 8; s++) {
array1[z][s] = random(1, 9999);
}
}
Serial.println("Array mit Zufallszahlen zwischen 1-9999: ");
ausgabeArray(array1);
bubbleSort(array1);
delay(5000);
Serial.println("\nNach der Grösse sortiert: ");
ausgabeArray(array1);
Serial.println("\n\n\n");
}
//loop wird immer wieder ausgeführt
void loop() {
// put your main code here, to run repeatedly:
}
You are only running one step of the bubble sort on each row of the array. You have to repeat that step until there are no more swaps on that row.
//Funktionen
void bubbleSort(int array1[8][8])
{
for (int x = 0; x < 8; x++)
{
boolean swapDone;
do
{
swapDone = false;
// for (int z = 0; z < 8; z++)
// EDIT As PieterP points out below, this should be:
for (int z = 0; z < (8-1); z++)
{
if (array1[x][z] > array1[x][z + 1])
{
int temp = array1[x][z];
array1[x][z] = array1[x][z + 1];
array1[x][z + 1] = temp;
swapDone = true;
}
}
} while (swapDone);
}
}
Unless you're doing it for learning purposes only, you probably don't want to be writing your own sorting functions. The C++ standard library has an implementation that has been thoroughly tested and that works on pretty much any container: [
[color=#5e6d03]#include[/color] [color=#434f54]<[/color][b][color=#d35400]Arduino_Helpers[/color][/b][color=#434f54].[/color][color=#000000]h[/color][color=#434f54]>[/color]
[color=#5e6d03]#include[/color] [color=#434f54]<[/color][b][color=#d35400]AH[/color][/b][color=#434f54]/[/color][color=#000000]STL[/color][color=#434f54]/[/color][color=#000000]algorithm[/color][color=#434f54]>[/color] [color=#434f54]// std::sort[/color]
[color=#5e6d03]#include[/color] [color=#434f54]<[/color][b][color=#d35400]AH[/color][/b][color=#434f54]/[/color][color=#000000]STL[/color][color=#434f54]/[/color][b][color=#d35400]iterator[/color][/b][color=#434f54]>[/color] [color=#434f54]// std::begin, std::end[/color]
[color=#434f54]// Pass the array by reference using ampersand (&) if you specify the size[/color]
[color=#00979c]void[/color] [color=#000000]sort[/color][color=#000000]([/color][color=#00979c]int[/color] [color=#000000]([/color][color=#434f54]&[/color][color=#000000]array1[/color][color=#000000])[/color][color=#000000][[/color][color=#000000]8[/color][color=#000000]][/color][color=#000000][[/color][color=#000000]8[/color][color=#000000]][/color][color=#000000])[/color] [color=#000000]{[/color]
[color=#434f54]// Use a range-based for-loop to prevend index errors[/color]
[color=#5e6d03]for[/color] [color=#000000]([/color][color=#00979c]auto[/color] [color=#434f54]&[/color][color=#000000]row[/color] [color=#434f54]:[/color] [color=#000000]array1[/color][color=#000000])[/color]
[color=#434f54]// Use the standard library sort function instead of reinventing the wheel [/color]
[color=#000000]std[/color][color=#434f54]:[/color][color=#434f54]:[/color][color=#000000]sort[/color][color=#000000]([/color][color=#000000]std[/color][color=#434f54]:[/color][color=#434f54]:[/color][color=#d35400]begin[/color][color=#000000]([/color][color=#000000]row[/color][color=#000000])[/color][color=#434f54],[/color] [color=#000000]std[/color][color=#434f54]:[/color][color=#434f54]:[/color][color=#d35400]end[/color][color=#000000]([/color][color=#000000]row[/color][color=#000000])[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]}[/color]
If you are using an AVR Arduino (Uno, Mega, Nano ...), you can use this Arduino Helpers library.
If you have a more powerful Arduino, you can just use system headers (#include <algorithm> and #include <iterator>).
std::sort and the other standard algorithms expect iterators as parameters. You can get the begin and end iterators of a container using the std::begin and std::end functions. You can read it as "sort from the elements from beginning of the row until the end of the row".
Range-based for-loops guarantee that you don't go out of bounds on the array. (For example if you later change the number of elements of the array, but forget to change the upper bounds in your for-loops.)