C# 冒泡排序!

寫了一個關於冒泡的算法。

        static void Main(string[] args)
        
{
            Console.Write(
"please input the number of variable:");
            
int num =Int32.Parse( Console.ReadLine());
            
int[] intArray=new int[num];
            
for (int i = 0; i < intArray.Length; i++)
            
{
                Console.Write(
"please input the {0} variable:", i);
                intArray[i] 
= Int32.Parse( Console.ReadLine());
            }

            intArray 
= Sort(intArray);
            String output
="";
            
for (int j = 0; j < intArray.Length; j++)
            
{
                output 
+= output.Equals(""? intArray[j].ToString() : ","+intArray[j].ToString();
            }

            Console.WriteLine(output);
            Console.ReadLine();

        }


       
public static int[] Sort(int[] array)
        
{
            
for (int i = array.Length - 1; i > 0; i--)
            
{
                
for (int j = 0; j < i; j++)
                
{
                    
if (array[j] > array[i])
                    
{
                        
int temp = array[i];
                        array[i] 
= array[j];
                        array[j] 
= temp;
                    }

                }

            }

            
return array;
        }

    }

 

 add by inkstone @2008年01月22日

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章