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

基本排序算法(C#)

时间:2017-08-20 18:20:27      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:二分插入   排序算法   []   冒泡   lis   --   select   直接   end   

        #region Algorithm
        static void SelectionSorter()//选择排序
        {
            int[] list = new int[] { 9, 5, 6, 89, 2, 0, 12, 895, 1, 6 };
            for (int i = 0; i < list.Length - 1; i++)
            {
                int min = i;
                for (int j = i + 1; j < list.Length; j++)
                {
                    if (list[j] < list[min])
                    {
                        min = j;
                    }
                }
                var t = list[i];
                list[i] = list[min];
                list[min] = t;
            }
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
        }

        static void EbullitionSorter()//冒泡排序
        {
            int[] list = new int[] { 9, 5, 6, 89, 2, 0, 12, 895, 1, 6 };
            for (int i = 0; i < list.Length - 1; i++)
            {
                for (int j = 0; j < list.Length - 1 - i; j++)
                {
                    if (list[j + 1] < list[j])
                    {
                        int t = list[j + 1];
                        list[j + 1] = list[j];
                        list[j] = t;
                    }
                }
            }
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
        }

        static void QuickSort(int[] arry, int left, int right)//快速排序
        {
            if (left < right)
            {
                int i = left, j = right;
                int x = arry[i];
                while (i < j)
                {
                    while (i < j && arry[j] >= x)
                    {
                        j--;
                    }
                    arry[i] = arry[j];
                    while (i < j && arry[i] <= x)
                    {
                        i++;
                    }
                    arry[j] = arry[i];
                }
                arry[i] = x;
                QuickSort(arry, left, i - 1);
                QuickSort(arry, j + 1, right);
            }
        }
        static void QuickTest()
        {
            int[] arry = new int[] { 9, 2, 4, 56, 2, 87, 1, 0, 12, 2, 9 };
            QuickSort(arry, 0, arry.Length - 1);
            foreach (var i in arry)
            {
                Console.WriteLine(i);
            }
        }

        static void InsertSort()//直接插入排序
        {
            int[] arry = new int[] { 1, 3, 4, 2, 56, 2, 87, 1, 0, 12, 2, 9 };
            for (int i = 1; i < arry.Length; i++)
            {
                if (arry[i - 1] > arry[i])
                {
                    int j;
                    int temp = arry[i];
                    for (j = i - 1; j >= 0 && arry[j] > temp; j--)
                    {
                        arry[j + 1] = arry[j];
                    }
                    arry[j + 1] = temp;
                }
            }
            foreach (var i in arry)
            {
                Console.WriteLine(i);
            }
        }

        static void BinaryInsertSort()//二分插入排序
        {
            int[] arry = new int[] { 1, 3, 4, 2, 56, 2, 87, 1, 0, 12, 2, 9 };
            for (int i = 0; i < arry.Length; i++)
            {
                var temp = arry[i];
                int low = 0;
                int high = i - 1;
                int mid = 0;
                while (low <= high)
                {
                    mid = (low + high) / 2;
                    if (temp < arry[mid])
                    {
                        high = mid - 1;
                    }
                    else
                    {
                        low = mid + 1;
                    }
                }
                int j = 0;
                for (j = i - 1; j >= high + 1; j--)
                {
                    arry[j + 1] = arry[j];
                }
                arry[j + 1] = temp;
            }
            foreach (var i in arry)
            {
                Console.WriteLine(i);
            }
        }
        #endregion    

 

基本排序算法(C#)

标签:二分插入   排序算法   []   冒泡   lis   --   select   直接   end   

原文地址:http://www.cnblogs.com/zxxxx/p/7400507.html

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