cannontim 于 2017.08.31 22:43 提问

#include
#include
int main(void)
{
int i, j;
int n = 3;
char abb[3][20] =
{
"333",
"22",
"1"
};
char temp[20];

``````for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (strlen(abb[i]) - strlen(abb[j])>0)
{
strcpy(temp, abb[i]);
strcpy(abb[i],abb[j]);
strcpy(abb[j], temp);
}
}
}
for (i = 0; i < n; i++)
{
puts(abb[i]);
printf("%d\n", strlen(abb[i]));

}

getchar();
return 0;
``````

}

6个回答

linqin107412   2017.09.01 09:16

C不怎么会，但是你这应该是冒泡排序吧，你的逻辑太混乱了，给你个java版本的，你自己参考下吧，第一层循环，表示有多少数要进行比较，

public static int[] bubbleSort(int[] before) {
int temp;
for(int i = 0;i < before.length;i++) {
for(int j = 0;j < before.length - i - 1;j++) {
if(before[j] > before[j+1]) {
temp = before[j];
before[j] = before[j+1];
before[j+1] = temp;
}
}
}
return before;
}
@org.junit.Test
public void testBubbleSort() {
int[] arr = {1,3,2,44,22,477,222,45};
System.out.println(Arrays.toString(bubbleSort(arr)));
}

songwz123   2017.09.01 09:18

qq_24029841   2017.09.01 09:20

m0_37894611   2017.09.01 11:58

SonOfWind0311   2017.09.01 13:49

Mr_cuiwei   2017.09.01 17:15