Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 8 years ago

用java寫氣泡排序

我的程式碼 可以幫我看哪裡錯嗎??

public static void main (String[] args)

{

int a[]={4,55,33,1,43,22};

int i, j, b,c;

for(i=4;i<=0;i--)

{

for(j=0;j==i;j++)

{

if(a[j]>a[j+1])

{

b=a[j];

a[j]=a[j+1];

a[j+1]=a[j];

}

}

}

for(c=0;c<=5;c++)

{

System.out.println("a["+c+"]="+a[c]);

}

}

}

1 Answer

Rating
  • 8 years ago
    Favorite Answer

    Your logic is absolutely wrong. Please study hard in the future by yourself.

    public class Help0702 {

    public static void main (String[] args)

    {

    int a[]={4,55,33,1,43,22};

    int b;

    for(int i=0;i<=5;i++)

    {

    for(int j=i;j<=5;j++)

    {

    if(a[i]>a[j])

    {

    b=a[i];

    a[i]=a[j];

    a[j]=b;

    }

    }

    }

    for(int c=0;c<=5;c++)

    {

    System.out.println("a["+c+"]="+a[c]);

    }

    }

    }

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.