现在的位置: 首页 > 综合 > 正文

bubble sorting

2012年12月08日 ⁄ 综合 ⁄ 共 1358字 ⁄ 字号 评论关闭

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

namespace bubblesorting
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int nn = 1; nn > 0;nn++ )
            {
                Console.WriteLine("enter the d");
                int d = Convert.ToInt16(Console.ReadLine());
                int[] intarr = new int[d];
                Random rd = new Random();
                for (int n = 0; n < intarr.Length; n++)
                {
                    intarr[n] = rd.Next(0, d);
                }

                Console.WriteLine("print the array");

                foreach (int p in intarr)
                {
                    Console.WriteLine(p);
                }

                Console.ReadLine();
                for (int n = intarr.Length - 1; n >= 0; n--)
                {
                    for (int m = n - 1; m >= 0; m--)
                    {
                        Swap(ref intarr[n], ref intarr[m]);
                    }
                }
                Console.WriteLine("print the array after sorting");
                foreach (int p in intarr)
                {
                    Console.WriteLine(p);
                }
                Console.ReadLine();
            }
        }
        static void Swap(ref int m, ref int n)
        {
            int temp;
            if (m < n)
            {
                temp = m;
                m = n;
                n = temp;
            }
            else
            {
            }
        }

    }
}

抱歉!评论已关闭.