冒泡,選擇排序法(C#)

iDotNetSpace發表於2009-10-26

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mppx
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[5];
            Random rnd = new Random();
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(200);
            }
            sc(arr);
            px(arr);
            xzpx(arr);
            Console.ReadLine();
        }
        ///


        /// 輸出陣列
        ///

        /// 陣列
        private static void sc(int[] a)
        {
            for (int i = 0; i < a.Length; i++)
            {
                Console.Write(a[i] + "  ");
            }
            Console.ReadLine();
        }
        ///
        /// 氣泡排序
        ///

        ///
        private static void px(int[] a)
        {
            int temp;
            for (int i = 0; i < a.Length - 1; i++)
            {
                for (int j = 0; j < a.Length - i - 1; j++)
                {
                    if (a[j] < a[j + 1])
                    {
                        temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;
                    }
                }
            }
            sc(a);
        }
        ///
        /// 選擇排序
        ///

        ///
        private static void xzpx(int[] a)
        {
            int temp;
            for (int i = 0; i < a.Length - 1; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    if(a[i]                    {
                        temp = a[i];
                        a[i] = a[j];
                        a[j] = temp;
                    }
                }
                sc(a);
            }
        }
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-617462/,如需轉載,請註明出處,否則將追究法律責任。

相關文章