冒泡排序练习

来源:互联网 发布:mysql建立表 编辑:程序博客网 时间:2024/04/28 10:39
 

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

namespace ConsoleApplication7
{
    class sort
    {
        public double[] paixu(double[] list)
        {
            int i, j;
            double t;
            for (j = 0; j < list.Length - 1; j++)
            {
                for (i = 0; i < (list.Length - 1) - j; i++)
                {
                    if (list[i] > list[i + 1])
                    {
                        t = list[i];
                        list[i] = list[i + 1];
                        list[i + 1] = t;
                    }
                }
            }

            return list;

        }
    }
    class Program
    {
        static void Main(string[] args)
        {
        
           double[] a={2,1,4,3,8,4,10,22};
           double[] s;
           sort p=new sort();
           s = p.paixu(a);
           for (int i = 0; i < s.Length; i++)
           {
               Console.Write(s[i]+",");
           }

           Console.ReadKey();
        }
    }
}
*/

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

namespace ConsoleApplication7
{
    class sort
    {
        public double[] paixu(double[] list,out int count,out int z)
        {
            int i, j;
            double t;
            count = 0;
            z = 0;
            for (j = 0; j < list.Length - 1; j++)
            {
                for (i = 0; i < (list.Length - 1) - j; i++)
                {
                    if (list[i] > list[i + 1])
                    {
                        t = list[i];
                        list[i] = list[i + 1];
                        list[i + 1] = t;
                        count++;
                        z++;
                    }
                    count++;
                }
                count++;
            }

            return list;

        }
        public double[] shuru(int num)
        {
            double[] a = new double[num];

            for (int x = 0; x < num; x++)
            {

                a[x] = Convert.ToDouble(Console.ReadLine());
            }
            return a;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            sort p = new sort();
            int b;
            Console.Write("请输入要排序的个数\t");
            b=Convert.ToInt32(Console.ReadLine());
            double[] s;
            int count, z;
            s = p.paixu(p.shuru(b),out count,out z);
            Console.WriteLine("总循环次数:" + count + "\t排序循环次数:" + z);
            for (int i = 0; i < s.Length; i++)
            {
                Console.Write(s[i] + " ");
            }
            double r = (double)z / (double)count;
            //double r = (double)z / count;
           // double r = z / count;
            Console.WriteLine("\t排序效率:" + Math.Round(r, 2));
            Console.WriteLine("\t排序效率:" + string.Format("{0:N2}",r) );
           
            Console.ReadKey();
        }
    }
}

 

原创粉丝点击