中国教程网论坛's Archiver

jiezi 发表于 2008-6-16 11:44

用冒泡法解题实例

[color=Red]问题:用冒泡法求数组int[] a = {100,40,60,87,34,11,56,0};[/color]



解答:

public class TestMao {

   
public static void main(String[] args) {
      int[] a = {100,40,60,87,34,11,56,0};
      int t;
      for(int i=0;i<a.length;i++)
      {
         for(int j=i;j<a.length;j++)
            {
          if(a[i]>a[j])
          {
           t=a[i];
           a[i]=a[j];
           a[j]=t;
             }
            }
      }
      for(int i=0;i<a.length;i++)
      {
      System.out.println(a[i]);
      }
}
}

jc:look jc:victory

28822882 发表于 2008-7-16 11:04

jc:sweat

kanbokb 发表于 2008-7-22 16:56

冒泡

public class numSort {

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                int[] arrays=new int[]{4,2,6,1,323,21,43,131};
                for(int i=0;i<8;i++)
                        for(int j=i;j<8;j++)
                                if(arrays[i]>arrays[j]){
                                        int temp;
                                        temp=arrays[i];
                                        arrays[i]=arrays[j];
                                        arrays[j]=temp;
                                }
                                else{
                                       
                                }
                       
                                for(int k=0;k<8;k++)
                                        System.out.println("排序为:"+arrays[k]+"\t");
                               
                        }

        }

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.