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 ...
随机推荐
- Java学习----接口
1. interface关键字 2. 接口中的方法全部是抽象方法,不能被实例 3. 接口中的成员变量: public static final 4. 当子类实现接口的时候,必须覆盖接口中所有的方法 / ...
- O(1)检测2的幂次
class Solution { public: /* * @param n: An integer * @return: True or false */ bool checkPowerOf2(in ...
- JavaScript学习总结【1】、初识JS
1.什么是 JavaScript? JavaScript 是一门跨平台.面向对象的动态的弱类型的轻量级解释型语言,是一种基于对象和事件驱动并具有相对安全性的客户端脚本语言.应用于 HTML 文档能够在 ...
- 为何要fork()两次来避免产生僵尸进程??
最近安装书上说的,开始搞多进程了..看到了一个好帖子,学习学习 http://blog.sina.com.cn/s/blog_9f1496990100y420.html 首先我们要明白,为什么要避免僵 ...
- Objective-C property属性解析
@interface … @property (原子性,可写性,内存管理) id name; @end 原子性: nonatomic, atomic 默认atomic 可写性: rea ...
- bzoj 3669: [Noi2014]魔法森林 动态树
3669: [Noi2014]魔法森林 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 363 Solved: 202[Submit][Status] ...
- linux下常用网络操作汇总
首先说明下RHEL6下设置IP地址的确和RHEL5下有几点是不同的. 我装完RHEL6中默认选择的是DHCP自动获取方式: [root@localhost ~]# vi /etc/sysconfig/ ...
- 几种交换两个数函数(swap函数)的写法和解析
#include <iostream> using namespace std; /*值传递,局部变量a和b的值确实在调用swap0时变化了,当结束时,他们绳命周期结束*/ void sw ...
- 【HDOJ】1814 Peaceful Commission
2-SAT基础题目. /* 1814 */ #include <iostream> #include <vector> #include <algorithm> # ...
- 【HDOJ】1421 搬寝室
DP.这题都能TLE,发现memset有时居然比for还要慢. #include <stdio.h> #include <stdlib.h> #include <stri ...