C#中的各种排序算法
原文发布时间为:2008-11-26 —— 来源于本人的百度文章 [由搬家工具导入]
C#算法(一)选择排序
using System;
public class SelectionSorter
{
// public enum comp {COMP_LESS,COMP_EQUAL,COMP_GRTR};
private int min;
// private int m=0;
public void Sort(int[] list)
{
for (int i = 0; i < list.Length - 1; ++i)
{
min = i;
for (int j = i + 1; j < list.Length; ++j)
{
if (list[j] < list[min])
min = j;
}
int t = list[min];
list[min] = list[i];
list[i] = t;
// Console.WriteLine("{0}",list[i]);
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
SelectionSorter ss = new SelectionSorter();
ss.Sort(iArrary);
for (int m = 0; m <= 13; m++)
Console.WriteLine("{0}", iArrary[m]);
}
}
C#算法(二)插入排序
using System;
public class InsertionSorter
{
public void Sort(int[] list)
{
for (int i = 1; i < list.Length; ++i)
{
int t = list[i];
int j = i;
while ((j > 0) && (list[j - 1] > t))
{
list[j] = list[j - 1];
--j;
}
list[j] = t;
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
InsertionSorter ii = new InsertionSorter();
ii.Sort(iArrary);
for (int m = 0; m <= 13; m++)
Console.WriteLine("{0}", iArrary[m]);
}
}
C#算法(三)希尔排序
public class ShellSorter
{
public void Sort(int[] list)
{
int inc;
for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
for (; inc > 0; inc /= 3)
{
for (int i = inc + 1; i <= list.Length; i += inc)
{
int t = list[i - 1];
int j = i;
while ((j > inc) && (list[j - inc - 1] > t))
{
list[j - 1] = list[j - inc - 1];
j -= inc;
}
list[j - 1] = t;
}
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
ShellSorter sh = new ShellSorter();
sh.Sort(iArrary);
for (int m = 0; m <= 13; m++)
Console.WriteLine("{0}", iArrary[m]);
}
}
C#算法(四)快速排序
using System;
namespace QuickSorter
{
public class QuickSorter
{
private void Swap(ref int l, ref int r)
{
int s;
s = l;
l = r;
r = s;
}
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);
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
QuickSorter q = new QuickSorter();
q.Sort(iArrary, 0, 13);
for (int m = 0; m <= 13; m++)
Console.WriteLine("{0}", iArrary[m]);
}
}
}
C#中的各种排序算法的更多相关文章
- Java基础语法(8)-数组中的常见排序算法
title: Java基础语法(8)-数组中的常见排序算法 blog: CSDN data: Java学习路线及视频 1.基本概念 排序: 是计算机程序设计中的一项重要操作,其功能是指一个数据元素集合 ...
- CPrimerPlus第十一章中的“选择排序算法”学习
C Primer Plus第十一章字符串排序程序11.25中,涉及到“选择排序算法”,这也是找工作笔试或面试可能会遇到的题目,下面谈谈自己的理解. 举个例子:对数组num[5]={3,5,2,1,4} ...
- C#中常用的排序算法的时间复杂度和空间复杂度
常用的排序算法的时间复杂度和空间复杂度 常用的排序算法的时间复杂度和空间复杂度 排序法 最差时间分析 平均时间复杂度 稳定度 空间复杂度 冒泡排序 O(n2) O(n2) 稳定 O(1) 快速排序 ...
- java泛型中使用的排序算法——归并排序及分析
一.引言 我们知道,java中泛型排序使用归并排序或TimSort.归并排序以O(NlogN)最坏时间运行,下面我们分析归并排序过程及分析证明时间复杂度:也会简述为什么java选择归并排序作为泛型的排 ...
- Arrays.Sort()中的那些排序算法
本文基于JDK 1.8.0_211撰写,基于java.util.Arrays.sort()方法浅谈目前Java所用到的排序算法,仅个人见解和笔记,若有问题欢迎指证,着重介绍其中的TimSort排序,其 ...
- Java中常见的排序算法
这是我摘取的一段英文资料.我认为学习算法之前,对各种排序得有个大致的了解: Sorting algorithms are an important part of managing data. At ...
- Java中几种排序算法
1.冒泡排序算法 通过多次比较(相邻两个数)和交换来实现排序 public class bubble { public static void bubbleSort(int[] a) { int te ...
- PHP 中四大经典排序算法
1.冒泡排序 在要排序的一组数中,对当前还未排好的序列,从前往后对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒.即,每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换. ...
- stl中常用的排序算法
#include"iostream" #include"vector" using namespace std; #include"string&qu ...
随机推荐
- Git之远程项目克隆到本地配置
远程代码克隆到本地工作区,需要进行简单的配置,用于识别身份 1.git config --global user.name [设置用户名,你的github用户名] 2.git config -- ...
- 复位电路设计——利用PLL锁定信号(lock)产生复位信号
利用PLL锁定信号(lock)产生复位信号 在FPGA刚上电的时候,系统所需的时钟一般都要经过PLL倍频,在时钟锁定(即稳定输出)以前,整个系统应处于复位状态.因此,我们可以利用PLL的锁定信号来产生 ...
- Java长存!12个Java长久占居主要地位的原因
Java长存!12个Java长久占居主要地位的原因 我们很容易就会遗忘那些曾经在猿群中大热而又被各种新技术掩盖直至堙灭的技术的价值.就拿COBOL这个老猿们当年所用的神器来说,就跟条死鱼一样被现代猿基 ...
- 浏览器报错问题解决:Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight respons
FAQ: Failed to load http://www.erpshop.com/index.php: Request header field Content-Type is not allow ...
- COGS 1. 加法问题 (水体日常)
这是一个经典的入门问题,通过此问题,你可以学会如何使用该评测系统. [问题描述] 现在有两个实数,分别是A和B.请你从文件中读取A和B,计算它们的和A+B,并把它输出到文件中.(保留到整数) [输入格 ...
- Android(java)学习笔记161:开发一个多界面的应用程序之人品计算器的简单实现
1.开启新的Activity的方法: (1)Intent 意图 (2)intent.setAction("自定义") 记得在清单文件中声明 (3)intent.setData(前 ...
- 《少年先疯队》第九次团队作业:Beta冲刺第三天
3.1 今日完成任务情况 姚玉婷:酒店系统中剩余功能的完善 马丽莎:酒店系统中管理员功能的测试 张 琼:酒店系统中会员功能的测试 孙苗坤:酒店系统中其余管理功能的测试文档的编写 3.2 成员贡献时 ...
- Spring框架针对dao层的jdbcTemplate操作crud之add添加数据库操作
使用jdbcTemplate 原理是把加载驱动Class.forName("com.mysql.jdbc.Driver"); 和连接数据库Connection conn=Drive ...
- 169. Majority Element@python
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 乘法逆元-洛谷-P3811
题目背景 这是一道模板题 题目描述 给定n,p求1~n中所有整数在模p意义下的乘法逆元. 输入输出格式 输入格式: 一行n,p 输出格式: n行,第i行表示i在模p意义下的逆元. 输入输出样例 输入样 ...