# Bubblesort an array?

Hello arduino folks!
I’m trying to do a bubblesort on an array with four numbers and I could really need some help right now, I can’t get my script to work somehow.
I got an array, the array is called unsortedData, with for numbers
unsortedData[1] = 4;
unsortedData[2] = 5;
unsortedData[3] = 3;
unsortedData[4] = 6;

In the final state i would like the sequence to be printed out like this:
3
4
5
6
If you don’t know how a bubblesort function work but still know the arduino programming language quite well, just google Bubblesort function.

Here’s my code so far:

`````` int unsortedData[4];
int sortedData[4];

void setup() {
Serial.begin(9600);

unsortedData[1] = 4;
unsortedData[2] = 5;
unsortedData[3] = 3;
unsortedData[4] = 6;

}

void loop() {

delay(1000*2);
for (int i = 1; i <= 3;) {
for (int c = 1; c + i <= 4;) { //Kan behöva en if loop istället för en for loop här
if(unsortedData[i+c] < unsortedData[i])
{
sortedData[i] = unsortedData[c+1];
sortedData[c+1] = unsortedData[i];
unsortedData[i] = sortedData[i];
unsortedData[c+1] = sortedData[c+i];
c = 1;
i = 1;
}
else {
}
c++;
}
i++;
c = 1;
for (int g = 1; g <= 4;) {
Serial.print("unsorted:");
Serial.println(unsortedData[g]);
g++;
}
for (int g = 1; g <= 4;) {
Serial.print("sorted:");
Serial.println(sortedData[g]);
g++;
}
delay(2000);
}
}
``````

I would really appreciate some help!
Thanks alot guys!
/Archelon

The index for C arrays starts at zero. If you declare an array with four elements then the indices for that array are 0, 1, 2 and 3. Rewrite your code to take this into account.

Pete

el_supremo:
The index for C arrays starts at zero. If you declare an array with four elements then the indices for that array are 0, 1, 2 and 3. Rewrite your code to take this into account.

Pete

Thanks Pete! Is there anything else you think looks wrong in my code? Thanks for enlighten me about the array thing! I will rewrite my code a litle bit, i'll inform you how it worked

It doesn't seem so hard, just have a buffer of two and working down the list see which of the numbers is smaller, switch them, and move on to the next pair, then repeat until no sorting is neccesary
id type it up its so simple but it sucks on my phone, ill do it later if you need an example

I just modified your code to fix the indenting problems and array indices. Also, I removed the lines that reset i and c to 1 every pass through the loop since those seemed like they’d make the loop never end.

``````int unsortedData[4];
int sortedData[4];

void setup() {
Serial.begin(9600);

unsortedData[0] = 4;
unsortedData[1] = 5;
unsortedData[2] = 3;
unsortedData[3] = 6;

}
void loop() {

delay(1000*2);
for (int i = 0; i < 3; i++) {
for (int c = 0; c + i < 4; c++) {
if(unsortedData[i+c] < unsortedData[i]) {
sortedData[i] = unsortedData[c+1];
sortedData[c+1] = unsortedData[i];
unsortedData[i] = sortedData[i];
unsortedData[c+1] = sortedData[c+i];
}
}
for (int g = 0; g < 4; g++) {
Serial.print("unsorted:");
Serial.println(unsortedData[g]);
}
for (int g = 0; g < 4; g++) {
Serial.print("sorted:");
Serial.println(sortedData[g]);
}
delay(2000);
}
}
``````