说明:冒泡、直接插入、选择、自带方法四中基本排序算法。
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;
}
}
}
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。