写冒泡排序可以排序多个字符串。

//写冒泡排序可以排序多个字符串
#include <stdio.h>  
#include <string.h>  

void bubble_sort_str(char *str[], int sz)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < sz - 1; i++)
	{
		for (j = 0; j < sz - 1 - i; j++)
		{
			if (strcmp(*(str + j), *(str + j + 1))>0)
			{
				char *tmp = *(str + j);
				*(str + j) = *(str + j + 1);
				*(str + j + 1) = tmp;
			}
		}
	}
}
int main()
{
	int i = 0;
	char *str[] = { "cccc", "bbbb", "dddd", "aaaa" };
	bubble_sort_str(str, sizeof(str) / sizeof(*str));
	for (i = 0; i < sizeof(str) / sizeof(*str); i++)
	{
		printf("%s ", *(str + i));
	}
	printf("\n");
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Damn_Yang/article/details/82713977
今日推荐