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 ...
随机推荐
- 中文翻译:pjsip教程(三)之ICE stream transport的使用
1:pjsip教程(一)之PJNATH简介 2:pjsip教程(二)之ICE穿越打洞:Interactive Connectivity Establishment简介 3:pjsip教程(三)之ICE ...
- 【HDU1402】【FFT】A * B Problem Plus
Problem Description Calculate A * B. Input Each line will contain two integers A and B. Process to e ...
- Java学习----类的组织(包)
1.包的概念 javac Engine.java -d . package:打包,把类文件编译成class文件会把Engine.class 放到com/cindy/pkg1/目录下 运行Engine ...
- HDU 5491 The Next
Problem Description Let L denote the number of 1s in integer D’s binary representation. Given two in ...
- JQuery.imgAreaSelect 参数说明
imgAreaSelect 参数说明: 参数 描述 aspectRatio 设定选取区域的显示比率,如:”4:3“ autoHide 如果设置为true,当选择区域选择结束时消失,默认值为:false ...
- WPF 进度条
//Create a Delegate that matches the Signature of the ProgressBar's SetValue method private delegate ...
- 子元素增加margin-top会增加给父元素的问题
假设我们有如下代码 <div id="father" style="height:400px;width:400px;background:#e4393c;&quo ...
- linux安装composer
1,确保php已成功安装,并且php可以被访问php -r "copy('https://getcomposer.org/installer', 'composer-setup.php'); ...
- 数据结构练习 00-自测5. Shuffling Machine (20)
Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...
- theano中的concolutional_mlp.py学习
(1) evaluate _lenet5中的导入数据部分 # 导入数据集,该函数定义在logistic_sgd中,返回的是一个list datasets = load_data(dataset) # ...