最简单的排序方法。
如果从大到小排序,那么从[0]元素开始,和后面的元素进行对比,如果后面元素大,则和[0]元素交换。
核心思想:选定基准元素,和其他元素对比。
#include <stdio.h>unsigned char buff1[] = { 15, 32, 35, 45, 58, 96, 7, 8, 12 };unsigned char buff2[] = { 23, 57, 31, 98, 77, 66, 95, 85, 17 };void Direct_Switch_Big_To_Small(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++) //这里-1可以节约一次循环时间{for (j = i + 1; j < input_length; j++){if (input_data[i] < input_data[j]){temp = input_data[i];input_data[i] = input_data[j];input_data[j] = temp;}}}
}void Direct_Switch_Small_To_Big(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++) //这里-1可以节约一次循环时间{for (j = i + 1; j < input_length; j++){if (input_data[i] > input_data[j]){temp = input_data[i];input_data[i] = input_data[j];input_data[j] = temp;}}}
}int main()
{int i = 0;Direct_Switch_Big_To_Small(buff1, sizeof(buff1));for (i = 0; i < sizeof(buff1); i++){printf("%d ", buff1[i]);}printf("\r\n");Direct_Switch_Small_To_Big(buff2, sizeof(buff2));for (i = 0; i < sizeof(buff2); i++){printf("%d ", buff2[i]);}printf("\r\n");
}
不过从函数实现的过程可以看出,直接排序并不是一个高效的排序方法,但优点是便于理解,几乎所有人都可以实现。