C#数组排序方法
在C#中常用的数组排序的方法有:选择排序法、冒泡排序法、插入排序法和希尔排序法等。
一、选择排序法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
// C#选择排序法-www.baike369.com
int[] array = new int[] { 2, 18, 9, 26, 3, 7, 5, 10 };
Console.Write("数组排序前的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.WriteLine();
int min;
for (int i = 0; i < array.Length - 1; i++)
{
min = i;
for (int j = i + 1; j < array.Length; j++)
{
if (array[j] < array[min])
min = j;
}
int t = array[min];
array[min] = array[i];
array[i] = t;
}
Console.Write("数组排序后的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.ReadLine();
}
}
}
运行结果:
数组排序前的结果为:2 18 9 26 3 7 5 10
数组排序后的结果为:2 3 5 7 9 10 18 26
二、冒泡排序法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[] { 2, 18, 9, 26, 3, 7, 5, 10 };
Console.Write("数组排序前的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.WriteLine();
for (int i = 0; i < array.Length; i++)
{
for (int j = i; j < array.Length; j++)
{
if (array[i] < array[j])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
Console.Write("数组排序后的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.ReadLine();
}
}
}
运行结果:
数组排序前的结果为:2 18 9 26 3 7 5 10
数组排序后的结果为:26 18 10 9 7 5 3 2
三、插入排序法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[] { 2, 18, 9, 26, 3, 7, 5, 10 };
Console.Write("数组排序前的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.WriteLine();
for (int i = 1; i < array.Length; i++)
{
int t = array[i];
int j = i;
while ((j > 0) && (array[j - 1] > t))
{
array[j] = array[j - 1];
--j;
}
array[j] = t;
}
Console.Write("数组排序后的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.ReadLine();
}
}
}
运行结果:
数组排序前的结果为:2 18 9 26 3 7 5 10
数组排序后的结果为:2 3 5 7 9 10 18 26四、希尔排序法
希尔排序是将组分段,然后进行插入排序。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[] { 2, 18, 9, 26, 3, 7, 5, 10 };
Console.Write("数组排序前的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.WriteLine();
int inc;
for (inc = 1; inc <= array.Length / 9; inc = 3 * inc + 1) ;
for (; inc > 0; inc /= 3)
{
for (int i = inc + 1; i <= array.Length; i += inc)
{
int t = array[i - 1];
int j = i;
while ((j > inc) && (array[j - inc - 1] > t))
{
array[j - 1] = array[j - inc - 1];
j -= inc;
}
array[j - 1] = t;
}
}
Console.Write("数组排序后的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.ReadLine();
}
}
}
运行结果:
数组排序前的结果为:2 18 9 26 3 7 5 10
数组排序后的结果为:2 3 5 7 9 10 18 26
C#数组排序方法的更多相关文章
- CSharpGL(36)通用的非托管数组排序方法
CSharpGL(36)通用的非托管数组排序方法 如果OpenGL要渲染半透明物体,一个方法是根据顶点到窗口的距离排序,按照从远到近的顺序依次渲染.所以本篇介绍对 UnmanagedArray< ...
- 数组排序方法(join()、reverse()、sort())
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ***PHP 数组排序 +php二维数组排序方法(PHP比较器)
PHP - 一维数组的排序函数 在本节中,我们将学习如下 PHP 数组排序函数: sort() - 以升序对数组排序 rsort() - 以降序对数组排序 asort() - 根据值,以升序对关联数组 ...
- php二维数组排序方法(转自http://www.3lian.com/edu/2013/12-26/118882.html)
一维数组排序可以使用asort.ksort等一些方法进程排序,相对来说比较简单.二维数组的排序怎么实现呢?使用array_multisort和usort可以实现 例如像下面的数组: 代码如下: ...
- PHP指定字段的多维数组排序方法
PHP数组排序可以用array_multisort方法实现,但是如果是多维数组,并且我们要指定数组中的某个字段进行排序,那么这就需要我们自己写方法实现了. function sortArrByFiel ...
- javascript数组(1) ——sort的工作原理及其他数组排序方法
一说到数组排序,最直观的想法就是用sort啊! 请问不用使用sort方法还可以使用什么方法进行数组排序? 比如 : 快速排序法.合并排序法.冒泡排序法.选择排序法.插入排序法.布尔排序法.交互排序. ...
- php二维数组排序方法(array_multisort usort)
一维数组排序可以使用asort.ksort等一些方法进程排序,相对来说比较简单.二维数组的排序怎么实现呢?使用array_multisort和usort可以实现 例如像下面的数组: $users = ...
- php二维数组排序方法(array_multisort,usort)
一维数组排序可以使用asort.ksort等一些方法进程排序,相对来说比较简单.二维数组的排序怎么实现呢?使用array_multisort和usort可以实现 例如像下面的数组: $users = ...
- PHP 数组排序方法总结
sort:本函数为 array 中的单元赋予新的键名.这将删除原有的键名而不仅是重新排序. rsort:本函数对数组进行逆向排序(最高到最低). 删除原有的键名而不仅是重新排序. asort:对数组进 ...
随机推荐
- CF1088F Ehab and a weird weight formula【倍增】
首先把点权归到边上,设点权较小的一个点是v,也就是(u,v)的边权是log2(dis(u,v))*a[v]+a[v]+a[u] 然后还有一个性质就是这棵树按点权最小点提起来就是一个堆 暴力是n^2的M ...
- 黑马学习MyBatis 用MyBatis对表进行条件查询 模糊查询 动态sql
package cn.itcast.domain; /* CREATE TABLE `message` ( `id` int(11) NOT NULL, `command` varchar(16) D ...
- Linux对外提供服务 网络操作 端口操作 1.开启服务监听端口 2.设置防火墙,放行访问端口的包 iptables&netfilter 四表五链和通堵策略
主题: Linux服务器上软件提供服务 1.网络操作 2.端口操作 1.网络操作 本机必须能够ping通目标主机(本地虚拟机或者远程主机) 2.端口操作 1.开启服务监听端口 2.设置防火墙,放行访问 ...
- 14.插入数据、复制数据--SQL
一.插入完整的行 要求指定表名和插入到新行中的值 INSERT INTO Customers ', 'Toy Land', '123 Any Street', 'New York', 'NY', ', ...
- redis之进阶
redis之进阶 redis redis介绍 redis的功能特性 1,高速读写 2,数据类型丰富 3,支持持久化 4,多种内存分配及回收策略 5,支持事务 6,消息队列.redis用的多的还是发 ...
- CF914D Bash and a Tough Math Puzzle 线段树+gcd??奇怪而精妙
嗯~~,好题... 用线段树维护区间gcd,按如下法则递归:(记题目中猜测的那个数为x,改动次数为tot) 1.若子区间的gcd是x的倍数,不递归: 2.若子区间的gcd是x的倍数,且没有递归到叶子结 ...
- mongodb 分片技术
MongoDB Sharding Cluster 分片集群 规划:10个实例:38017-38026 (1)configserver:3台构成的复制集(1主两从,不支持arbiter)38018-38 ...
- 对于es线程池使用的思考
es有内置的线程池 在实际项目中,发现 使用client框架关闭连接太慢(其实是把连接归还到池子里),采用异步关闭. 随着连接的关闭,计算机内存在不断下降 ------------------- ...
- 采用React+Ant Design组件化开发前端界面(一)
react-start 基础知识 1.使用脚手架创建项目并启动 1.1 安装脚手架: npm install -g create-react-app 1.2 使用脚手架创建项目: create ...
- node-amqp 使用fanout发布订阅rabbitmq消息
publisher代码 const amqp = require('amqp'); let option = { host: 'server-ip', port: 5672, login: 'gues ...