ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 冒泡,选择排序法(C#)

冒泡,选择排序法(C#)

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-10-26 18:22:50 0 删除 编辑

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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5297791