C# 数组排序 基本算法 分类: C# 2014-09-25 15:43 129人阅读 评论(0) 收藏
说明:冒泡、直接插入、选择、自带方法四中基本排序算法。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace OrderBy {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
try {
//定义六个整形数组
int[] arr1 = new int[10] { 3, 2, 1, 10, 4, 5, 6, 9, 7, 8 };
int[] arr2 = new int[10] { 3, 2, 1, 10, 4, 5, 6, 9, 7, 8 };
int[] arr3 = new int[10] { 3, 2, 1, 10, 4, 5, 6, 9, 7, 8 };
int[] arr4 = new int[10] { 3, 2, 1, 10, 4, 5, 6, 9, 7, 8 };
int[] arr5 = new int[10] { 3, 2, 1, 10, 4, 5, 6, 9, 7, 8 };
int[] arr6 = new int[10] { 3, 2, 1, 10, 4, 5, 6, 9, 7, 8 };
string maopao = "";//冒泡排序 方法一
string maopao2 = "";//冒泡排序 方法二
string insert = "";//插入排序 方法一
string insert2 = "";//插入排序 方法二
string select = "";//选择排序
string arrayMethod = "";//自带函数排序
//变量数组
OrderByMaoPao(arr1);
foreach (int n in arr1) {
maopao += "," + n;
}
OrderByMaoPao2(arr2);
foreach (int n in arr2) {
maopao2 += "," + n;
}
OrderByInsert(arr3);
foreach (int n in arr3) {
insert += "," + n;
}
OrderByInsert2(arr4);
foreach (int n in arr4) {
insert2 += "," + n;
}
OrderBySelect(arr5);
foreach (int n in arr5) {
select += "," + n;
}
Array.Sort(arr6);
foreach (int n in arr6) {
arrayMethod += "," + n;
}
//去掉逗号
maopao = maopao.Substring(1, maopao.Length - 1);
maopao2 = maopao2.Substring(1, maopao2.Length - 1);
insert = insert.Substring(1, insert.Length - 1);
insert2 = insert2.Substring(1, insert2.Length - 1);
select = select.Substring(1, select.Length - 1);
arrayMethod = arrayMethod.Substring(1, arrayMethod.Length - 1);
string output = "冒泡1:"+maopao + "\r\n冒泡2:" + maopao2 + "\r\n插入1:" + insert + "\r\n插入2:" + insert2 + "\r\n选择 :" + select+"\r\n方法 :"+arrayMethod;
// txt.Text = output;
MessageBox.Show(output,System.Windows.Forms.Application.ProductName);
} catch (Exception ex) {
MessageBox.Show(ex.Message);
return;
}
}
/// <summary>
/// 冒泡排序 方法一
/// </summary>
/// <param name="array">要排序的数组</param>
private void OrderByMaoPao(int[] array) {
//arr.GetLength(0) 获取个数 也可以用arr.Length
for (int i = 0; i < array.Length; i++) {
for (int j = i; j < array.Length-1; j++) {
int temp;
if (array[i] >= array[j + 1]) {
temp = array[i];
array[i] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
/// <summary>
/// 冒泡排序 方法二
/// </summary>
/// <param name="array">要排序的数组</param>
private void OrderByMaoPao2(int[] array) {
int j, temp;
for (int i = 0; i < array.Length-1; i++) {
j = i + 1;
id: //定义一个标识
if (array[i] > array[j]) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
goto id;
} else {
if (j < array.Length - 1) {
j++;
goto id;
}
}
}
}
/// <summary>
/// 直接插入排序 个人理解(把一个值,插入到表中,位置是:比上一个数大、比下一个小)
/// </summary>
/// <param name="array">要排序的数组</param>
private void OrderByInsert(int[] array) {
for (int i = 0; i < array.Length - 1; i++) {
for (int j = i + 1; j <= array.Length - 1; j++) {
if (i > 0) {//前两个已存在
if (array[i] > array[j] && array[i - 1] < array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
} else { //第二个数
if (array[i] > array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
}
/// <summary>
/// 直接插入排序 简洁
/// </summary>
/// <param name="array">要排序的数组</param>
private void OrderByInsert2(int[] array) {
for (int i = 0; i <= array.Length - 1; i++) {
int temp = array[i];
int j = i;
while (j > 0 && array[j - 1] > temp) { //通过盘点,值一次次提前
array[j] = array[j - 1];
--j;
}
array[j] = temp;
}
}
/// <summary>
/// 选择排序 获取最小值
/// </summary>
/// <param name="array">要排序的数组</param>
private void OrderBySelect(int[] array) {
for (int i = 0; i <= array.Length - 1; i++) {
int min = array[i];
for (int j = i + 1; j <= array.Length - 1; j++) {
int temp;
if (min > array[j]) {
min = array[j];
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
C# 数组排序 基本算法 分类: C# 2014-09-25 15:43 129人阅读 评论(0) 收藏的更多相关文章
- Drainage Ditches 分类: POJ 图论 2015-07-29 15:01 7人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62016 Accepted: 23808 De ...
- 彩色模型 分类: 图像处理 Matlab 2015-01-08 20:43 364人阅读 评论(0) 收藏
彩色模型(又称彩色空间或彩色系统)是描述色彩的一种方法,本质上,彩色模型就是坐标系统和子空间的规范,系统中的每种颜色由单个点来表示.下面介绍两种最常用的彩色模型. 一.RGB彩色模型: RGB模型是最 ...
- C/C++中const的用法 分类: C/C++ 2015-07-05 00:43 85人阅读 评论(0) 收藏
const是C语言的关键字,经C++进行扩充,变得功能强大,用法复杂.const用于定义一个常变量(只读变量),当const与指针,引用,函数等结合起来使用时,情况会变得复杂的多.下面将从五个方面总结 ...
- Find The Multiple 分类: 搜索 POJ 2015-08-09 15:19 3人阅读 评论(0) 收藏
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21851 Accepted: 8984 Sp ...
- 周赛-DZY Loves Chessboard 分类: 比赛 搜索 2015-08-08 15:48 4人阅读 评论(0) 收藏
DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Ultra-QuickSort 分类: POJ 排序 2015-08-03 15:39 2人阅读 评论(0) 收藏
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 48111 Accepted: 17549 ...
- cf 61E. Enemy is weak 树状数组求逆序数(WA) 分类: Brush Mode 2014-10-19 15:16 104人阅读 评论(0) 收藏
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> ...
- max_flow(Dinic) 分类: ACM TYPE 2014-09-02 15:42 94人阅读 评论(0) 收藏
#include <cstdio> #include <iostream> #include <cstring> #include<queue> #in ...
- SQL 分组 加列 加自编号 自编号限定 分类: SQL Server 2014-11-25 15:41 283人阅读 评论(0) 收藏
说明: (1)日期以年月形式显示:convert(varchar(7),字段名,120) , (2)加一列 (3)自编号: row_number() over(order by 字段名 desc) a ...
随机推荐
- 从Bash漏洞学Shell脚本(冒号)
前天,爆发了Bash安全漏洞,非常恐怖.在网络上开始飞速传播,附带了非常友好的检测工具. $ env x='() { :;}; echo vulnerable' bash -c "echo ...
- windows phone 之ListBox模板选择
有时做项目时,会遇到一种情况:需要把获取到的数据集合显示到首页,比如新闻,但是: 新闻也分类别啊,比如:图片类新闻.文字类新闻.视频类新闻. 那我们可能采用的模板就不一样了,那么,如何根据类别来选择模 ...
- 问题:Maven: missing net.sf.json-lib
问题:Maven: missing net.sf.json-lib 解决: pom.xml里加入json-lib依赖,注意要添加classifier一行,如下: 这里maven地址:http://re ...
- 关于反射Assembly.Load("程序集").CreateInstance("命名空间.类")
关于反射Assembly.Load("程序集").CreateInstance("命名空间.类") 而不管在哪一层写这段代码其中的("程序集" ...
- 4d tensor
偶然在一个ppt中看到了如下关于tensor的解释,清晰明白,所以post在这里,以备后续查看 根据这个理解: theano中的input(4d tensor):[mini-batch size, n ...
- LightOj_1027 A Dangerous Maze
题目链接 题意: 你在一个迷宫里, 开始的时候你面前有n个门, 选择每个门的概率相等, 有两种结果: 1)回到|x|分钟之前(x为负时) 2)x分钟之后出迷宫(x为正时) 每次回到|x|分钟之前, 你 ...
- bzoj 2209: [Jsoi2011]括号序列 splay
2209: [Jsoi2011]括号序列 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 833 Solved: 392[Submit][Status ...
- hOAuth2.0认证和授权原理
原文地址: http://www.6zou.net/tech/what_is_oauth.html http://www.phpddt.com/%E4%BA%8C%E6%AC%A1%E5%BC%80% ...
- 使用php对多维维数组排序。
要多php的多维数组排序,可以使用php里的内置函数:array_multisort(); 语法:array_multisort(array1,sorting order,sorting type,a ...
- 孤陋寡闻又一遭:ReportEvent API函数(有微软Service官方例子为例)
API 详解: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363679(v=vs.85).aspx 使用例子: https: ...