常用排序法

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

namespace ConAppSequenceLearn
{
    class Program
    {
        /// <summary>
        /// 冒泡排序法
        /// </summary>
        public class BubbleSorter
        {
            public void Sort(int[] list)
            {
                int i, j, temp;
                bool done = false;
                j = 1;
                while ((j < list.Length) && (!done))
                {
                    done = true;
                    for (i = 0; i < list.Length - j; i++)
                    {
                        if (list[i] > list[i + 1])
                        {
                            done = false;
                            temp = list[i];
                            list[i] = list[i + 1];
                            list[i + 1] = temp;
                        }
                    }
                    j++;
                }
            }
        }
        /// <summary>
        /// 选择排序法
        /// </summary>
        public class SelectionSorter
        {
            private int min;
            public void Sort(int[] list)
            {
                for (int i = 0; i < list.Length - 1; i++)
                {
                    min = i;
                    for (int j = i + 1; j < list.Length; j++)
                    {
                        //如果外层的值大于里层的值那么把标识位换一下
                        if (list[j] < list[min])
                            min = j;
                    }
                    int t = list[min];
                    list[min] = list[i];
                    list[i] = t;
                }
            }
        }

        /// <summary>
        /// 插入排序法
        /// </summary>
        public class InsertionSorter
        {
            public void Sort(int[] list)
            {
                for (int i = 1; i < list.Length; i++)
                {
                    int iCrrentValue = list[i];
                    int iCrrentIndex = i;
                    while (i > 0 && list[i - 1] > iCrrentValue)
                    {
                        list[i] = list[i - 1];
                        --i;
                    }
                    list[i] = iCrrentValue;
                }
            }
        }

        /// <summary>
        /// 快速排序法
        /// </summary>
        public class QuickSorter
        {
            public void Sort(int[] list, int min, int max)
            {
                int iMin, iMax, iTempVlue;
                int iAvgValue;
                iMin = min;
                iMax = max;
                iAvgValue = list[(iMax + iMin) / 2];
                do
                {
                    while (list[iMin] < iAvgValue && iMin < max) iMin++;
                    while (list[iMax] > iAvgValue && iMax > min) iMax--;
                    if (iMax >= iMin)
                    {
                        iTempVlue = list[iMin];
                        list[iMin] = list[iMax];
                        list[iMax] = iTempVlue;
                        iMin++;
                        iMax--;
                    }

                } while (iMax >= iMin);
                if (iMin < max) Sort(list, iMin, max);
                if (iMax > min) Sort(list, iMax, min);
            }
        }

        public static void Main()
        {
            int[] iArrary = { 1, 5, 3, 6, 2, 4 };
            SelectionSorter ss = new SelectionSorter();
            ss.Sort(iArrary);
            for (int m = 0; m < iArrary.Length; m++)
                Console.Write("{0} ", iArrary[m]);
            Console.WriteLine();
            Console.ReadLine();

        }


    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值