Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Arduino bubble sorting method
Arduino bubble sorting method
Like the standard C++, the basic idea of the algorithm is to judge the adjacent data and change the position if it is bigger than it. The code is roughly as follows

String? InString// stores the string obtained from the serial port.

int? No. [100]; //Store the maximum value of 100 bits of the data array to be arranged, and int means that only integer variables are accepted.

int? p=0,a = 0;

Invalid? Bobup()// This function is a bubble sort loop.

{

int? I,j,temp?

for(j = 0; j & ltp; j++)?

{

for(I = 0; I<P-j; i++)?

{

if(num[i]? & gt? num[i+ 1])

{

Temporary workers? =? num[I]; ?

num[i]? =? num[I+ 1]; ?

num[i+ 1]? =? Temperature; ?

}

}

}

Serial.println ("Update? :"); //Arduino sends the sorted array to the serial port.

for(int? k = 0; k & ltp+ 1; k++)

{

serial . print(num[k]);

Serial.println(",");

}

}

Invalid? Settings ()

{

serial . begin(9600);

}

Invalid? loop()? //Read serial data and convert int (I won't tell you that it is separated from the example)

{

What time? (Serial.available()? & gt? 0)

{

inString? +=? char(serial . read()());

Delay (2);

a = 1;

}

if(a== 1)?

{

num[p]= instring . toint();

boboUp();

inString =

p++;

a = 0;

}

I tested it with my own board, and the results are as follows.

Usage is to enter a number in the edit box at the top of the serial display, and click Send to save a number in the array (it is not supported to enter a string of numbers, so try to change the code yourself). When the array is updated, arduino will return an ordered array to the computer through the serial port. At most, the integer 100 is supported. If it needs to be larger or floating-point data needs to be arranged, it can be modified according to code comments.