常见的排序算法C#版

news/2024/7/3 8:54:10
 1  #region 冒泡排序算法
2 //首先将所有待排序的数字放入工作列表中
3 //从列表的第一个数字到倒数第二个数字,逐个检查:若某一位上的数字大于他的下一位,则将它与它的下一位交换
4 //重复2号步骤,直至再也不能交换
5 public class BubbleSort
6 {
7 public void Sort(int[] list)
8 {
9 int i, j, temp;
10 j = 1;
11 while (j < list.Length)
12 {
13 for (i = 0; i < list.Length - j; i++)
14 {
15 if (list[i] > list[i + 1])
16 {
17 temp = list[i];
18 list[i] = list[i + 1];
19 list[i + 1] = temp;
20 }
21 }
22 j++;
23 }
24 }
25 }
26 #endregion
 1  #region  选择排序算法
2 //1.设数组内存放了n个待排数字,数组下标从1开始,到n结束
3 //2.i=1从数组的第i个元素开始到第n个元素,寻找最小的元素
4 //3.将上一步找到的最小元素和第i位元素交换
5 //4.如果i=n-1算法结束,否则回到第3步
6 public class ChooseSort
7 {
8 public void Sort(int[] list)
9 {
10 int min;
11 for (int i = 0; i < list.Length - 1; i++)
12 {
13 min = i;
14 for (int j = i + 1; j < list.Length; j++)
15 {
16 if (list[j] < list[min])
17 min = j;
18 }
19 int t = list[min];
20 list[min] = list[i];
21 list[i] = t;
22 }
23 }
24 }
25 #endregion
 1 #region  插入排序算法
2 //首先新建一个空列表,用于保存已排序的有序数列(我们称之为"有序列表")
3 //从原数列中取出一个数,将其插入"有序列表"中,使其仍旧保持有序状态
4 //重复2号步骤,直至原数列为空
5 public class InsertSort
6 {
7 public void Sort(int[] list)
8 {
9 for (int i = 1; i < list.Length; ++i)
10 {
11 int t = list[i];
12 int j = i;
13 while ((j > 0) && (list[j - 1] > t))
14 {
15 list[j] = list[j - 1];
16 --j;
17 }
18 list[j] = t;
19 }
20 }
21 }
22 #endregion
 1   #region  希尔排序算法
2 //先取一个小于n的整数d1作为第一个增量,把文件的全部记录分成d1个组
3 //所有距离为dl的倍数的记录放在同一个组中。先在各组内进行直接插入排序
4 //然后,取第二个增量d2<d1重复上述的分组和排序,直至所取的增量dt=1(dt<dt-l<…<d2<d1),即所有记录放在同一组中进行直接插入排序为止
5 public class ShellSort
6 {
7 public void Sort(int[] list)
8 {
9 int inc;
10 for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
11 for (; inc > 0; inc /= 3)
12 {
13 for (int i = inc + 1; i <= list.Length; i += inc)
14 {
15 int t = list[i - 1];
16 int j = i;
17 while ((j > inc) && (list[j - inc - 1] > t))
18 {
19 list[j - 1] = list[j - inc - 1];
20 j -= inc;
21 }
22 list[j - 1] = t;
23 }
24 }
25 }
26 }
27 #endregion
 1    #region  快速排序算法
2 //实践证明,快速排序是所有排序算法中最高效的一种
3 //它采用了分治的思想:先保证列表的前半部分都小于后半部分,然后分别对前半部分和后半部分排序,这样整个列表就有序了
4 //这是一种先进的思想,也是它高效的原因。因为在排序算法中,算法的高效与否与列表中数字间的比较次数有直接的关系
5 //而"保证列表的前半部分都小于后半部分"就使得前半部分的任何一个数从此以后都不再跟后半部分的数进行比较了,大大减少了数字间不必要的比较。但查找数据得另当别论了
6 public class QuickSort
7 {
8 public void Sort(int[] intArray, int nLower, int nUpper)
9 {
10 if (nLower < nUpper)
11 {
12 int nSplit = Partition(intArray, nLower, nUpper);
13 ///递归排序
14 Sort(intArray, nLower, nSplit - 1);
15 Sort(intArray, nSplit + 1, nUpper);
16 }
17 }
18 /// <summary>
19 /// 方法参数:原始数组、第一个元素位置、最后元素位置
20 /// 方法功能:完成一趟快速排序
21 /// </summary>
22 /// <param name="intArray"></param>
23 /// <param name="nLower"></param>
24 /// <param name="nUpper"></param>
25 /// <returns></returns>
26 public int Partition(int[] intArray, int nLower, int nUpper)
27 {
28 int nLeft = nLower + 1;
29 ///以数组第一个元素值作为支点
30 int nPivot = intArray[nLower];
31 int nRight = nUpper;
32
33 int nSwap;
34 while (nLeft <= nRight)
35 {
36 ///从左向右寻找大于支点元素
37 while (nLeft <= nRight && intArray[nLeft] < nPivot)
38 nLeft++;
39 ///从右向左寻找小于支点元素
40 while (nLeft <= nRight && intArray[nRight] >= nPivot)
41 nRight--;
42 ///交换nLeft和nRight位置元素值
43 if (nLeft < nRight)
44 {
45 nSwap = intArray[nLeft];
46 intArray[nLeft] = intArray[nRight];
47 intArray[nRight] = nSwap;
48 nLeft++;
49 nRight--;
50 }
51 }
52 ///以intArray[nRight]为新支点
53 nSwap = intArray[nLower];
54 intArray[nLower] = intArray[nRight];
55 intArray[nRight] = nSwap;
56 return nRight;
57 }
58 }
59 #endregion
 1    class Program
2 {
3 static void Main(string[] args)
4 {
5 int[] iArrary = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
6 BubbleSort bs = new BubbleSort(); //实例化类
7 bs.Sort(iArrary);//排序
8 for (int i = 0; i < iArrary.Length; i++)
9 {
10 Console.WriteLine(iArrary[i]);
11 }
12 }

  输出结果:


http://www.niftyadmin.cn/n/4255179.html

相关文章

php 接口服务器设计与实现,php服务器接口框架

【实例简介】之前项目前端用android&#xff0c;服务器端用php&#xff0c;所以自己用php封装了一套服务器接口框架【实例截图】【核心代码】友伴框架php└── php├── core│ ├── base│ │ ├── Facade.php│ │ ├── Factory.php│ │ ├── Modul…

英语练习 -《翻译学习法》

People read on the web用户在网站上阅读 People only read word-by-word on the web when they are really interested in the content. 用户只有对他们感兴趣的内容&#xff0c;才会一个字一个字的去阅读 They usually skim the pages looking for highlighted keywords&…

oracle数据库支持的触发器,oracle数据库PL/SQL之触发器-Oracle

oracle数据库PL/SQL之触发器七)触发器 www.2cto.com1.基本概念两种功能&#xff1a;完成由数据库的完整性约束难以完成的复杂业务规则的约束&#xff1b;监视数据库的各种操作&#xff0c;实现审计功能。触发器分为&#xff1a;DML触发器(对表或视图执行DML操作时触发)&…

前端资源(13)

Markdown 地址 Markdown 语法说明 (简体中文版 http://wowubuntu.com/markdownmarkdown入门参考 https://github.com/LearnShare/Learning-Markdown/blob/master/README.mdgitbook https://www.gitbook.com 国外的在线markdown可编辑成书mdeditor https://www.zybuluo.com/mde…

oracle nodemanage,Oracle WebLogic Server 12c: Node Manager配置与使用

在生产环境中&#xff0c;WebLogic服务器实例经常会跨多个管理域(domains)、物理主机(machines)或地域(geographic locations)分开部署。Node Manager是远程启动、停止或重启管理服务器和受管服务器的工具。尽管它不是必须的&#xff0c;但如果你的WebLogic服务器中的应用有高可…

FND_CONCURRENT.SET_COMPLETION_STATUS(服务器端函数)

FND_CONCURRENT.SET_COMPLETION_STATUS (服务器端函数) 语法&#xff1a; FUNCTION fnd_concurrent.set_completion_status(status IN VARCHAR2, message IN VARCHAR2) RETURN BOOLEAN; 说明&#xff1a;在并发程序内调用用来设…

linux监控脚本的作用,Linux几个常用的监控脚本

下面是我常用的几个主机监控的脚本&#xff0c;大家可以根据自己的情况再进行修改&#xff0c;希望能给大家一点帮助。1、查看主机网卡流量#!/bin/bash#network#Mike.Xuwhile : ; dotimedate %m"-"%d" "%k":"%Mdaydate %m"-"%drx_befo…

时尚工具箱

◢工具列表: ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ 一、安全辅助 01 360顽固***专杀大全 02 IE安全助手 03 金山毒霸U盘病毒专杀 04 暴力文件删除器 05 文件夹看门狗 白金版 06 翼缘在线杀毒 二、实用工具 01 PDF阅读器绿色纯…