#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
char *arr[6] = {"cherry","apple","orange","pear","grape","watermelon"};
// 冒泡排序
for (int i=0; i<5; i++) {
for (int j=0; j<5-i; j++) {
// strcmp函数比较字符串,返回值>0,则前一个字符串大于后一个字符串
if (strcmp(arr[j], arr[j+1])>0) {
// 交换指针指向
char *temp;
temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
// 遍历输出字符串数组
for (int i=0; i<6; i++) {
printf("%s\n",arr[i]);
}
printf("\n");
}
return 0;
}
|
|