Bubble sort (Bubble sort) is a simpler sort algorithm in the field of computer science. It repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if their order is wrong. The task of visiting the series is repeated until no further exchange is needed, which means that the sequence has been sorted. The name of this algorithm is because the smaller elements will slowly "float" through the exchange to the top of the series, hence the name. Because of the simplicity of bubble sorting, it is often used to introduce the concept of algorithms to students who are getting started with computer programming.
Copy Code code as follows:
#include <stdio.h>
int swap (int *a, int *b)
{
*a = *a + *b;
*b = *a-*b;
*a = *a-*b;
return 0;
}
int Mao_pao (int *a, int n)
{
int I, j, Flag;
for (i = n-1, flag = 1; i > 0 && flag; i.)
{
Flag = 0;
for (j = 0; J < i; ++j)
{
if (A[j] > a[j + 1])
{
flag = 1;
Swap (A + j, a + j + 1);
}
}
}
return 0;
}
int main (int argc, const char *argv[])
{
int i;
int a[] = {7,8,6,2,9,4,3,1,5,10};
Mao_pao (A, 10);
for (i = 0; i < ++i)
{
printf ("%4d", A[i]);
}
printf ("\ n");
return 0;
}