冒泡排序就是從左至右比較相鄰的兩個數值大小,如果右側的數值較小,則交換兩個數值的位置,較大的數值就會像泡泡一樣一路向右漂浮。
#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");
}