冒泡法对字符串排序

来源:百度知道 编辑:UC知道 时间:2024/06/23 08:12:47
编写一个程序,用冒泡法对数组greeting从小到大进行排序,排序时修改greeting的元素的值。
char * greeting[ ]={ “Hello”, ”Good morning “, ”How are you”, “How do you do “, “Good afternoon”, “Good evening”};

#include <stdio.h>
#include <string.h>

char * greeting[]={ "Hello", "Good morning", "How are you", "How do you do", "Good afternoon", "Good evening"};

int main(){
int fl=1,i;

while(fl){
fl=0;
for(i=0;i<5;i++)
if(strcmp(greeting[i],greeting[i+1])>0){
char * temp;
temp=greeting[i];
greeting[i]=greeting[i+1];
greeting[i+1]=temp;
fl=1;
}
}

for(i=0;i<=5;i++)
printf("%s\n",greeting[i]);

return 0;
}

#include <stdio.h>
#include <string.h>

void swap(char* a, char* b, size_t width)
{
char tmp;
while(width--)
{
tmp = *a;
*a++ = *b;
*b++ = tmp;
}
}

void bsort(void* base, size_t num, size_t width,