#include <stdio.h> #include <string.h> #include <stdlib.h> //用冒泡排序法进行排序 void Maopao(char *arr[5]){ char *temp; for (int i=0; i<5-1; i++) { for (int j=0; j<5-i-1; j++) { temp=malloc(sizeof(arr[i])); //比较相邻元素的大小 if(strcmp(arr[j],arr[j+1])>0){ temp=arr[j+1]; arr[j+1]=arr[j]; arr[j]=temp; } } } } //输出排序后的结果 void print_arr(char *arr[5]){ for(int i=0;i<5;i++){ printf("%s\n",arr[i]); } } int main(int argc, const char * argv[]) { //定义数组 char *arr[5]={"heima","itcast","TingHua","Peking","shuangYuan"}; Maopao(arr); print_arr(arr); return 0; } |