当前位置: 首页 > 工具软件 > Coda Bubble > 使用案例 >

bubble sorting

束志业
2023-12-01
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
            {
            }
        }

    }
}


 类似资料:

相关阅读

相关文章

相关问答