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


    留着给自己用吧...

     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

  • 相关阅读:
    面试准备
    论文投稿Cover letter
    Pycharm 快捷键
    linux下常用命令:
    Qt中数据模块学习
    Qt 多线程和网络编程学习
    VS高效开发快捷键
    良好编码风格习惯整理
    Qt QAxObject操作excel文件过程总结(转):
    Qt开发中的实用笔记三--关于各种类的零碎知识点:
  • 原文地址:https://www.cnblogs.com/fleas/p/4201088.html
Copyright © 2020-2023  润新知