冒泡排序就是从左至右比较相邻的两个数值大小,如果右侧的数值较小,则交换两个数值的位置,较大的数值就会像泡泡一样一路向右漂浮。
#include <stdio.h>//small to big
void Bubble_Sort(unsigned char *input_data, unsigned int input_length)
{unsigned int i = 0, j = 0;unsigned char temp = 0;for (i = 0; i < input_length - 1; i++){for (j = 0; j < input_length - 1 - i; j++){if (input_data[j] > input_data[j + 1]){temp = input_data[j];input_data[j] = input_data[j + 1];input_data[j + 1] = temp;}}}
}int main()
{int i = 0;unsigned char buff[] = { 54, 78, 61, 46, 18, 56, 14, 51, 65, 97, 79, 13 };Bubble_Sort(buff, sizeof(buff));for (i = 0; i < sizeof(buff); i++){printf("%d ", buff[i]);}printf("\r\n");
}