C#冒泡排序法學習

來源:互聯網
上載者:User

標籤: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#冒泡排序法學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.