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

泛型的冒泡,插入,选择,希尔算法

时间:2015-01-04 15:07:55      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

留着给自己用吧...

 public class FanXingHelper<T> where T:IComparable
    {
        /// <summary>
        /// 冒泡排序算法
        /// </summary>
        /// <param name="array"></param>
        public T[] MaoPao(T[] array) {
            int length = array.Length;
            for (int i = 0; i <= length-2; i++)
            {
                for (int j = length-1; j>=1 ; j--)
                {
                    if (array[j].CompareTo(array[j-1])<0)
                    {
                        T temp = array[j];
                        array[j] = array[j - 1];
                        array[j - 1] = temp;
                    }
                }
            }
            return  array;
        }
        /// <summary>
        /// 直接插入排序
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public T[] ZhiJieChaRu(T[] array) {
            int length = array.Length;
            for (int i = 1; i < length-1; i++)
            {
                int j = i;
                while (j>=1&&array[j].CompareTo(array[j-1])>0)
                {
                    T temp = array[j];
                    array[j] = array[j - 1];
                    array[j - 1] = temp;
                    j--;
                }
            }
            return array;
        }
        /// <summary>
        /// 选择排序
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public T[] XuanZhePaiXu(T[] array) {
            int length = array.Length;
            for (int i = 0; i <= length-2; i++)
            {
                int index = i;
                for (int j = length-1; j >i; j--)
                {
                    if (array[j].CompareTo(array[index])<0)
                    {
                        index = j;
                    }
                  
                     T temp = array[i];
                     array[i] = array[index];
                     array[index] = temp;
                }
            }
            return array;
        }
        /// <summary>
        /// 希尔排序算法
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public T[] XiErPaiXu(T[] array) {
            int length = array.Length;
            for (int i = length/2;i>0; i=i/2)
            {
                for (int j = i; j < i; j++)
                {
                    if (array[j].CompareTo(array[j-i])>0)
                    {
                        T temp = array[j];
                        int key = j - i;
                        while (key >= 0 && array[key].CompareTo(temp) > 0) {
                            array[key + i] = array[key];
                            key -= i;
                        }
                        array[key + i] = temp;
                    }
                }
            }
            return array;
        }
    }

如果有错希望能指出来...谢谢

这个主要是看了.net之美突发奇想所以写的这个东西

参考:

http://www.cnblogs.com/JimmyZhang/category/128644.html

泛型的冒泡,插入,选择,希尔算法

标签:

原文地址:http://www.cnblogs.com/fleas/p/4201088.html

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