字符串的冒泡排序与整型冒泡排序与原理相同,在此就不对冒泡排序做详细介绍,直接给出源代码!
#include#include #define N 10int main(){ char *arr[N] = {"pas","pase","abced","asdf","acdef","ertugbs","jhgsds","ksjdjfh","skjsh","smjdjg"}; int i = 0; int j = 0; int size = sizeof(arr) / sizeof(arr[0]); for (i = 0; i < size- 1; i++) { for (j = 0; j < size-1-i; j++) { if (strcmp(arr[j],arr[j+1])>0) { char *tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } } for (i = 0; i < size; i++) { printf("%s ", arr[i]); } printf("\n"); system("pause"); return 0;}
在上边的程序中我定义了指针数组的大小,大家使用时可以不定义,直接初始化就好。本人由于学习经验等原因,程序肯定还有很多漏洞,欢迎各位大神对我写的的代码进行批评指正!