#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<string.h> void bubble_sort(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 = NULL; tmp = str[j]; str[j] = str[j + 1]; str[j + 1] = tmp; } } } } int main() { char*str[] = { "hello", "world", "to", "bit", "welcome" };//指针数组 int i = 0; bubble_sort(str, sizeof(str) / sizeof(str[0])); for (i = 0; i < sizeof(str) / sizeof(str[0]); i++) { printf("%s ", str[i]); } printf("\n"); system("pause"); return 0; }
结果:
bit hello to welcome world
请按任意键继续. . .
时间: 2024-11-02 06:28:17