写冒泡排序可以排序多个字符串
#define _CRT_SECURE_NO_WARNINGS 0
#include <stdio.h>
#include <windows.h>
#include <assert.h>
#include <string.h>
void string_sort(char *arr[][7], int num)
{
assert(arr);
assert(num > 0);
int i = 0;
int j = 0;
int flag = 0;
for (i = 0; i < num; i++)
{
flag = 0;
for (j = 0; j < num - 1 - i; j++)
{
if (strcmp(arr[j], arr[j + 1]) > 0)
{
flag = 1;
//相当于两个数交换(同样的思想)
char buff[64]; //创建缓冲区
strcpy(buff, arr[j]); //arr[j]放入缓冲区
strcpy(arr[j], arr[j + 1]); //arr[j+1]覆盖arr[j]
strcpy(arr[j + 1], buff); //缓冲区覆盖arr[j+1]
}
}
}
if (flag == 0)
{
return;
}
}
int main()
{
char arr[][7] =
{
"ffffff",
"aaaaaa",
"bbbbbb",
"gggggg",
"kkkkkk",
};
int len = sizeof(arr) / sizeof(arr[0]);
string_sort(arr, len);
system("pause");
return 0;
}