陣列氣泡排序和字串氣泡排序
阿新 • • 發佈:2019-01-23
陣列氣泡排序
void BubbleSort(int *arr,int len)
{
int tmp;
for(int i=0;i<len-1;i++)
{
for(int j=0;j<len-1-i;j++)
{
if(arr[j] > arr[j+1])
{
tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}
字串氣泡排序
void StrSort(char **arr,int len) { char *tmp; for(int i=0;i<len-1;i++) { for(int j=0;j<len-1-i;j++) { if(strcmp(arr[j],arr[j+1]) > 0) { tmp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = tmp; } } } } int main() { int arr[] = {5,8,0,2,67,9,12,3}; BubbleSort(arr,sizeof(arr)/sizeof(arr[0])); char *str[10] = {"China","Japan","America", "Korea","England","Germany", "France","Russia","Singapore", "Malaysia"}; StrSort(str,sizeof(str)/sizeof(str[0])); return 0; }