日期:2014-05-17  浏览次数:21039 次

C#第1次试验(4)设计一个程序,输入10个数存入数组中,然后实现冒泡排序

精简算法:

/*static void Main(string[] args)       
 {
       int[] a = {1,2,3,4,5,6,7,8,9,10}; 
       Array.Sort(a);          
  	for (int i = 0; i < b.Length; i++)        
    {             
  	  Console.Write(b[i].ToString() + " ");    
    }       
     	Console.ReadLine();   
  } 
*/


常规算法:

 using System; 
 using System.Collections.Generic; 
 using System.Linq; 
 using System.Text; 
 
namespace ConsoleApplication14
 { 
     class Program 
     { 
         static void Main(string[] args) 
         { 
 
             int[] a = {1,2,3,4,5,6,7,8,9,10}; 
             int[] b = BubbleSort(a); 
             for (int i = 0; i < b.Length; i++) 
             {
                 
                 Console.WriteLine(b[i].ToString() + " "); 
 
            } 
             Console.ReadLine(); 
         } 
 
        public static int[] BubbleSort(int[] list) 
         { 
             int i,j,temp; 
             for ( j = 0; j < list.Length; j++) 
             { 
                 for (i = list.Length - 1; i > j; i--) 
                 { 
                     if (list[j] < list[i]) 
                     { 
                         temp = list[j]; 
                         list[j] = list[i]; 
                         list[i] = temp; 
                     } 
                 } 
             } 
 
            return list; 
         } 
     } 
 }


 

输出结果:

经验总结:

冒泡排序要逐一进行!