using System;
using System.Text; namespace HuaTong.General.Utility
{
/// <summary>
/// 自定义排序类
/// </summary>
public class Sorter
{
/// <summary>
/// 交换元素位置
/// </summary>
public static void Swap<T>(ref T[] arr, int i, int j)
{
T temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
} /// <summary>
/// 冒泡排序
/// </summary>
public static T[] BubbleSort<T>(T[] arr) where T : IComparable
{
for (int i = arr.Length - ; i > ; --i)
{
for (int j = ; j < i; ++j)
{
if (arr[j].CompareTo(arr[j + ]) > )
{
Swap(ref arr, j, j + );
}
}
}
return arr;
} /// <summary>
/// 选择排序
/// </summary>
public static T[] SelectionSort<T>(T[] arr) where T : IComparable
{
for (int i = ; i < arr.Length; ++i)
{
int index = i;
for (int j = i + ; j < arr.Length; ++j)
{
if (arr[j].CompareTo(arr[index]) < ) index = j;
}
Swap(ref arr, i, index);
}
return arr;
} /// <summary>
/// 插入排序
/// </summary>
public static T[] InsertionSort<T>(T[] arr) where T : IComparable
{
for (int i = ; i < arr.Length; ++i)
{
int j = i;
T value = arr[i];
while (j > && arr[j - ].CompareTo(value) > )
{
arr[j] = arr[j - ];
--j;
}
arr[j] = value;
}
return arr;
} /// <summary>
/// 希尔排序
/// </summary>
public static T[] ShellSort<T>(T[] arr) where T : IComparable
{
for (int step = arr.Length >> ; step > ; step >>= )
{
for (int i = ; i < step; ++i)
{
for (int j = i + step; j < arr.Length; j += step)
{
int k = j;
T value = arr[j];
while (k >= step && arr[k - step].CompareTo(value) > )
{
arr[k] = arr[k - step];
k -= step;
}
arr[k] = value;
}
}
}
return arr;
} /// <summary>
/// 快速排序
/// </summary>
public static T[] QuickSort<T>(T[] arr, int startIndex, int endIndex) where T : IComparable
{
int i, j;
T x, y; i = (startIndex < ? : startIndex);
j = (endIndex > arr.Length - ? arr.Length - : endIndex);
if (startIndex > endIndex) return null;
x = arr[(i + j) / ]; while (i <= j)
{
while (arr[i].CompareTo(x) < && i < endIndex)
{
i = i + ;
}
while (x.CompareTo(arr[j]) < && j > startIndex)
{
j = j - ;
}
if (i <= j)
{
y = arr[i];
arr[i] = arr[j];
arr[j] = y;
i = i + ;
j = j - ;
}
} if (startIndex < j) QuickSort(arr, startIndex, j);
if (i < endIndex) QuickSort(arr, i, endIndex); return arr;
} /// <summary>
/// 归并排序
/// </summary>
public static T[] MergeSort<T>(T[] arr, int startIndex, int endIndex, T[] arrF) where T : IComparable
{
startIndex = (startIndex < ? : startIndex);
endIndex = (endIndex > arr.Length - ? arr.Length - : endIndex);
arrF = new T[arr.Length];
if (startIndex >= endIndex) return null;
int m = (startIndex + endIndex) >> ;
MergeSort(arr, startIndex, m, arrF);
MergeSort(arr, m + , endIndex, arrF);
for (int i = startIndex, j = startIndex, k = m + ; i <= endIndex; ++i)
{
arrF[i] = arr[(k > endIndex || j <= m && arr[j].CompareTo(arr[k]) < ) ? j++ : k++];
}
for (int i = startIndex; i <= endIndex; ++i) arr[i] = arrF[i]; return arr;
} /// <summary>
/// 堆排序
/// </summary>
public static T[] HeapSort<T>(T[] arr) where T : IComparable
{
for (int i = ; i < arr.Length; ++i)
{
for (int j = i, k = (j - ) >> ; k >= ; j = k, k = (k - ) >> )
{
if (arr[k].CompareTo(arr[j]) >= ) break;
Swap(ref arr, j, k);
}
}
for (int i = arr.Length - ; i > ; --i)
{
Swap(ref arr, , i);
for (int j = , k = (j + ) << ; k <= i; j = k, k = (k + ) << )
{
if (k == i || arr[k].CompareTo(arr[k - ]) < ) --k;
if (arr[k].CompareTo(arr[j]) <= ) break;
Swap(ref arr, j, k);
}
}
return arr;
}
}
}

c# 自定义排序类(冒泡、选择、插入、希尔、快速、归并、堆排序等)的更多相关文章

  1. C++学习(三十八)(C语言部分)之 排序(冒泡 选择 插入 快排)

    算法是解决一类问题的方法排序算法 根据元素大小关系排序 从小到大 从大到小冒泡 选择 插入 快排希尔排序 归并排序 堆排序 冒泡排序 从头到尾比较 每一轮将最大的数沉底 或者最小数字上浮 选择排序 1 ...

  2. python 数据结构与算法之排序(冒泡,选择,插入)

    目录 数据结构与算法之排序(冒泡,选择,插入) 为什么学习数据结构与算法: 数据结构与算法: 算法: 数据结构 冒泡排序法 选择排序法 插入排序法 数据结构与算法之排序(冒泡,选择,插入) 为什么学习 ...

  3. java 基础排序(冒泡、插入、选择、快速)算法回顾

    java 基础排序(冒泡.插入.选择.快速)算法回顾 冒泡排序 private static void bubbleSort(int[] array) { int temp; for (int i = ...

  4. python 中的一些基础算法:递归/冒泡/选择/插入

    递归算法 如果一个函数包含了对自己的调用,那么这个函数就是递归的. 比如我们计算下1-7乘法的计算: def func(n): if n ==1 : return 1 return n*func(n- ...

  5. 【数据结构 C++】排序——冒泡、插入、选择、希尔、归并、快排、堆排序

    LeetCode 912. 排序数组 给你一个整数数组 nums,请你将该数组升序排列. 示例 1: 输入:nums = [5,2,3,1] 输出:[1,2,3,5] 示例 2: 输入:nums = ...

  6. 基本排序-冒泡/选择/插入(python)

    # -*- coding: utf-8 -*- import random def bubble_sort(seq): n = len(seq) for i in range(n-1): print( ...

  7. php中usort自定义排序如何使用

    php中usort自定义排序如何使用 一.总结 一句话总结:多写一个规则函数,而这个函数的写法和普通函数一样,调用的时候规则函数用函数名的字符串. 1.用户自定义规则函数有哪三个? usort — 使 ...

  8. php六种基础算法:冒泡,选择,插入,快速,归并和希尔排序法

    $arr(1,43,54,62,21,66,32,78,36,76,39); 1. 冒泡排序法  *     思路分析:法如其名,就是像冒泡一样,每次从数组当中 冒一个最大的数出来.  *     比 ...

  9. iOS 开发中常用的排序(冒泡、选择、快速、插入、希尔、归并、基数)算法

    1.冒泡排序: 冒泡算法是一种基础的排序算法,这种算法会重复的比较数组中相邻的两个元素.如果一个元素比另一个元素大(小),那么就交换这两个元素的位置.重复这一比较直至最后一个元素.这一比较会重复n-1 ...

随机推荐

  1. 【Python】高阶函数

    filter def is_palindrome(n): L = str(n) i = 0 j = len(L) - 1 while i != j: if L[i] != L[j]: return F ...

  2. 基于Node.js的爬虫工具 – Node Crawler

    Node Crawler的目标是成为最好的node.js爬虫工具,目前已经停止维护. 我们来抓取光合新知博客tech栏目中的文章信息.访问http://dev.guanghe.tv/category/ ...

  3. 20145329 《Java程序设计》第三周学习总结

    教材学习内容总结 java并非完整的面向对象程序语言 定义 1:class:定义类 2.char:类型声明变量 3.new:新建对象 4.名称 X:参考 5.=:可用于指定参考至新建变量 6.构造函数 ...

  4. Centos 7 关闭邮件服务及禁用IPv6

    关闭邮件服务(禁用25端口) sudo systemctl stop dovecot sudo systemctl stop postfix sudo systemctl disable doveco ...

  5. 【前端】jQuery选择器$()的实现原理

    今天三七互娱技术面试的时候面试官问了我这个问题,当时一脸懵逼,于是好好总结一下. 当我们使用jquery选择器的时候,$(s).回默认去执行jquery内部封装好的一个init的构造函数每次申明一个j ...

  6. 一文弄懂神经网络中的反向传播法——BackPropagation【转】

    本文转载自:https://www.cnblogs.com/charlotte77/p/5629865.html 一文弄懂神经网络中的反向传播法——BackPropagation   最近在看深度学习 ...

  7. ELK出现unassigned_shards查看及删除

    问题 用3台服务器搭建了ELK系统,有一天出现有几个索引一直无法同步,重启了elasticsearch也不行 如下图:elk-cluster一直处于red状态 解决方法 一,查看elasticsear ...

  8. AccessTokens

    https://www.oauth.com/oauth2-servers/access-tokens/ Access tokens are the thing that applications us ...

  9. Graph_Master(连通分量_D_Trajan缩点+dfs)

    hdu_2242 题目大意:求将一张无向图(n个点,m条边)移除一条边分为不连通两部分,使得两部分的点权和最接近,若无法分为两部分,则输出impossible. 题解:拿到题面还算清晰,就是先tarj ...

  10. Docker Mysql主从同步配置搭建Demo

    进行Docker操作前,先建立目录,我的路径是d:/docker/mysql,目录结构如下: --mysql --master --data --conf --my.cnf --slaver --da ...