.NET算法集

 

  1. 选择排序

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Text; 

 

namespace sorter 

    public class SelectionSorter 

    { 

        private int min; 

        public void Sort(int[] arr) 

        { 

            for (int i = 0; i < arr.Length - 1; ++i) 

            { 

                min = i; 

                for (int j = i + 1; j < arr.Length; ++j) 

                { 

                    if (arr[j] < arr[min]) 

                    { 

                        min = j; 

                    } 

                } 

                int t = arr[min]; 

                arr[min] = arr[i]; 

                arr[i] = t; 

            } 

        } 

    } 

 

    class Program 

    { 

        static void Main(string[] args) 

        { 

            int[] arrInt = new int[] { 4, 2, 7, 1, 8, 3, 9, 0, 5, 6 }; 

            SelectionSorter selSor = new SelectionSorter(); 

            selSor.Sort(arrInt); 

            foreach (int i in arrInt) 

            { 

                Console.WriteLine(i); 

            } 

            Console.ReadKey(); 

        } 

    } 

 

  1. 冒泡排序

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Text; 

 

namespace sorter 

    public class EbullitionSorter 

    { 

        public void Sort(int[] arr) 

        { 

            int i, j, temp; 

            bool done = false; 

            j = 1; 

            while ((j < arr.Length) && (!done))  // 判断长度 

            { 

                done = true; 

                for (i = 0; i < arr.Length - j; i++) 

                { 

                    if (arr[i] > arr[i + 1]) 

                    { 

                        done = false; 

                        temp = arr[i]; 

                        arr[i] = arr[i + 1]; // 交换数据 

                        arr[i + 1] = temp; 

                    } 

                } 

                j++; 

            } 

        } 

    } 

 

    class Program 

    { 

        static void Main(string[] args) 

        { 

            int[] arrInt = new int[] { 4, 2, 7, 1, 8, 3, 9, 0, 5, 6 }; 

            EbullitionSorter selSor = new EbullitionSorter(); 

            selSor.Sort(arrInt); 

            foreach (int i in arrInt) 

            { 

                Console.WriteLine(i); 

            } 

            Console.ReadKey(); 

        } 

    } 

 

  1. 快速排序

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Text; 

 

namespace sorter 

    public class QuickSorter 

    { 

        private void swap(ref int l, ref int r) 

        { 

            int temp; 

            temp = l; 

            l = r; 

            r = temp; 

        } 

 

        public void Sort(int[] list, int low, int high) 

        { 

            int pivot; // 存储分支点 

            int l, r; 

            int mid; 

            if (high <= low) 

            { 

                return; 

            } 

            else if (high == low + 1) 

            { 

                if (list[low] > list[high]) 

                { 

                    swap(ref list[low], ref list[high]); 

                } 

                return; 

            } 

            mid = (low + high) >> 1; 

            pivot = list[mid]; 

            swap(ref list[low], ref list[mid]); 

            l = low + 1; 

            r = high; 

            do 

            { 

                while (l <= r && list[l] < pivot) 

                { 

                    l++; 

                } 

                while (list[r] >= pivot) 

                { 

                    r--; 

                } 

                if (l < r) 

                { 

                    swap(ref list[l], ref list[r]); 

                } 

            } while (l < r); 

            list[low] = list[r]; 

            list[r] = pivot; 

            if (low + 1 < r) 

            { 

                Sort(list, low, r - 1); 

            } 

            if (r + 1 < high) 

            { 

                Sort(list, r + 1, high); 

            } 

        } 

    } 

 

    class Program 

    { 

        static void Main(string[] args) 

        { 

            int[] arrInt = new int[] { 4, 2, 7, 1, 8, 3, 9, 0, 5, 6 }; 

            QuickSorter selSor = new QuickSorter(); 

            selSor.Sort(arrInt, 0, arrInt.Length - 1); 

            foreach (int i in arrInt) 

            { 

                Console.WriteLine(i); 

            } 

            Console.ReadKey(); 

        } 

    } 

}

  1.  插入排序

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Text; 

 

namespace sorter 

    public class InsertionSorter 

    { 

        public void Sort(int[] arr) 

        { 

            for (int i = 1; i < arr.Length; i++) 

            { 

                int t = arr[i]; 

                int j = i; 

                while ((j > 0) && (arr[j - 1] > t)) 

                { 

                    arr[j] = arr[j - 1]; // 交换顺序 

                    --j; 

                } 

                arr[j] = t; 

            } 

        } 

    } 

 

    class Program 

    { 

        static void Main(string[] args) 

        { 

            int[] arrInt = new int[] { 4, 2, 7, 1, 8, 3, 9, 0, 5, 6 }; 

            InsertionSorter selSor = new InsertionSorter(); 

            selSor.Sort(arrInt); 

            foreach (int i in arrInt) 

            { 

                Console.WriteLine(i); 

            } 

            Console.ReadKey(); 

        } 

    } 

 

  1. 希尔排序

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Text; 

 

namespace sorter 

    public class ShellSorter 

    { 

        public void Sort(int[] arr) 

        { 

            int inc; 

            for (inc = 1; inc <= arr.Length / 9; inc = 3 * inc + 1) ; 

            for (; inc > 0; inc /= 3) 

            { 

                for (int i = inc + 1; i <= arr.Length; i += inc) 

                { 

                    int t = arr[i - 1]; 

                    int j = i; 

                    while ((j > inc) && (arr[j - inc - 1] > t)) 

                    { 

                        arr[j - 1] = arr[j - inc - 1]; // 交换数据 

                        j -= inc; 

                    } 

                    arr[j - 1] = t; 

                } 

            } 

        } 

    } 

 

    class Program 

    { 

        static void Main(string[] args) 

        { 

            int[] arrInt = new int[] { 4, 2, 7, 1, 8, 3, 9, 0, 5, 6 }; 

            ShellSorter selSor = new ShellSorter(); 

            selSor.Sort(arrInt); 

            foreach (int i in arrInt) 

            { 

                Console.WriteLine(i);  

            } 

            Console.ReadKey(); 

        } 

    } 

 

  1. 归并排序

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Text; 

 

namespace Merge 

    public class Function 

    { 

        private int Groups; 

        private int CopyGroups; 

        private int mergerows; 

        private int[] Array27; 

        private static Random ran = new Random(); 

        public Function(int length) 

        { 

            Array27 = new int[length]; 

            for (int i = 0; i < length; i++) 

                Array27[i] = ran.Next(1, 100); 

        } 

        //选择 

        public void ToMergeSort() 

        { 

            MergeSort(Array27); 

        } 

        public void ToRecursiveMergeSort() 

        { 

            RecursiveMergeSort(Array27, 0, Array27.Length - 1); 

        } 

        public void ToNaturalMergeSort() 

        { 

            NaturalMergeSort(Array27); 

        } 

 

        /// <summary> 

        /// 归并排序(递归) 

        ///    核心思想:(分治) 

        ///           将待排序元素(递归直至元素个数为1)分成左右两个大小大致相同的2个子集合,然后, 

        ///           分别对2个子集合进行排序,最终将排好序的子集合合并成为所要求的排好序的集合.   

        /// 核心算法时间复杂度:    

        ///           T(n)=O(nlogn) 

        /// </summary> 

        /// <param name="Array"></param> 

        /// <param name="left"></param> 

        /// <param name="right"></param> 

        public void RecursiveMergeSort(int[] Array, int left, int right) 

        { 

            int middle = (left + right) / 2; 

 

            if (left < right) 

            { 

                //对前半部分递归拆分 

                RecursiveMergeSort(Array, left, middle); 

                //对后半部分递归拆分 

                RecursiveMergeSort(Array, middle + 1, right); 

                MergeOne(Array, left, middle, right); 

            } 

        } 

        public void MergeOne(int[] Array, int left, int middle, int right) 

        { 

            int leftindex = left; 

            int rightindex = middle + 1; 

            //动态临时二维数组存放分割为两个小Array的数组排列顺序后的数据 

            int[] merge = new int[right + 1]; 

            int index = 0; 

            //对两个小数组合并排序 

            while (leftindex <= middle && rightindex <= right) 

                merge[index++] = (Array[leftindex] - Array[rightindex]) >= 0 ? Array[rightindex++] : Array[leftindex++]; 

            //有一侧子数列遍历完后,将另外一侧子数列剩下的数依次放入暂存数组中(有序) 

            if (leftindex <= middle) 

            { 

                for (int i = leftindex; i <= middle; i++) 

                    merge[index++] = Array[i]; 

            } 

            if (rightindex <= right) 

            { 

                for (int i = rightindex; i <= right; i++) 

             

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值