首页 > 代码库 > 数组 -自动遍历数组-冒泡排序

数组 -自动遍历数组-冒泡排序

数组:定义一组同类型的指定个数的变量索引从0开始

int[] name=new int[n];

------------------------------------------------------

自动遍历数组:

foreach (int a in shu)

{

   console.writeline(a);

}

上式相当于:

for( int i=0;i<shu.length;i++)

{

int a;

a=shu[i];

console.writeline(a);

}

----------------------------------------

等量代换:

int a=3,b=5;

int c;

c=a;

a=b;

b=c;

输入8个数并从大到小排序(冒泡排序)

 

 int[] a = new int[8];            

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

  {              

   a[i] = Convert.ToInt32(Console.ReadLine());

   }          

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

  {               for (int j=i  ;j  <8; j ++)              

              {        

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

                     {

                            int zhong;                    

                          zhong = a[i ];                     

                           a[i ] = a[j  ];                     

                           a[j  ] = zhong;                                       

                       }                                  

              }               

       Console.WriteLine(a[i]);          

  }                     

  Console.ReadLine();

 

数组 -自动遍历数组-冒泡排序