码迷,mamicode.com
首页 > 编程语言 > 详细

C#冒泡排序法学习

时间:2017-02-05 16:49:47      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:each   位置   key   return   style   oid   names   eth   []   

一,冒泡排序法理解:就是将一个集合里的数据当前位置和后一位比较,然当前位置大于后一位,则两个位置替换,直到排序完成

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

namespace MaoPao
{
    class Program
    {
        static void Sort(int[] sArray)
        {
            bool sw = true;
            do
            {
                sw = false;
                for (int i = 0; i < sArray.Length - 1; i++)
                {
                    if (sArray[i] > sArray[i + 1])
                    {
                        int temp = sArray[i];
                        sArray[i] = sArray[i + 1];
                        sArray[i + 1] = temp;
                        sw = true;
                    }
                }
            } while (sw);
        }

        static void Main(string[] args)
        {
            int[] sArray = new int[] { 88, 73, 22, 1, 445, 53, 63, 5 };
            Sort(sArray);
            foreach (var temp in sArray)
            {
                Console.Write(temp + " ");
            }
            Console.ReadKey();
           
        }
    }
}

二,冒泡排序拓展

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

namespace MaoPao
{
    class Program
    {
        static void CommonSort<T>(T[] sArray, Func<T, T, bool> compareMethod)
        {
            bool sw = true;
            do
            {
                sw = false;
                for (int i = 0; i < sArray.Length - 1; i++)
                {
                    if (compareMethod(sArray[i], sArray[i + 1]))
                    {
                        T temp = sArray[i];
                        sArray[i] = sArray[i + 1];
                        sArray[i + 1] = temp;
                        sw = true;
                    }
                }
            } while (sw);
        }
        static void Main(string[] args)
        {

            Employee[] employees = new Employee[]
            {
                new Employee("张三",12), 
                new Employee("李四",234), 
                new Employee("陈五",14), 
                new Employee("李六",234), 
                new Employee("王七",90)
            };
            CommonSort<Employee>(employees, Employee.Compare);
            foreach (Employee em in employees)
            {
                Console.WriteLine(em);
            }
            Console.ReadKey();
        }
    }
}

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

namespace MaoPao
{
    class Employee
    {
        public string Name { get; private set; }
        public int Comp { get; private set; }

        public Employee(string name, int comp)
        {
            this.Name = name;
            this.Comp = comp;
        }
        //如果e1大于e2的话,返回true,否则返回false
        public static bool Compare(Employee e1, Employee e2)
        {
            if (e1.Comp > e2.Comp) return true;
            return false;
        }

        public override string ToString()
        {
            return Name + ":" + Comp;
        }
    }
}

 

C#冒泡排序法学习

标签:each   位置   key   return   style   oid   names   eth   []   

原文地址:http://www.cnblogs.com/May-day/p/6367868.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!