说明:冒泡、直接插入、选择、自带方法四中基本排序算法。

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) 收藏的更多相关文章

  1. Drainage Ditches 分类: POJ 图论 2015-07-29 15:01 7人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62016 Accepted: 23808 De ...

  2. 彩色模型 分类: 图像处理 Matlab 2015-01-08 20:43 364人阅读 评论(0) 收藏

    彩色模型(又称彩色空间或彩色系统)是描述色彩的一种方法,本质上,彩色模型就是坐标系统和子空间的规范,系统中的每种颜色由单个点来表示.下面介绍两种最常用的彩色模型. 一.RGB彩色模型: RGB模型是最 ...

  3. C/C++中const的用法 分类: C/C++ 2015-07-05 00:43 85人阅读 评论(0) 收藏

    const是C语言的关键字,经C++进行扩充,变得功能强大,用法复杂.const用于定义一个常变量(只读变量),当const与指针,引用,函数等结合起来使用时,情况会变得复杂的多.下面将从五个方面总结 ...

  4. 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 ...

  5. 周赛-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 ...

  6. Ultra-QuickSort 分类: POJ 排序 2015-08-03 15:39 2人阅读 评论(0) 收藏

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 48111   Accepted: 17549 ...

  7. cf 61E. Enemy is weak 树状数组求逆序数(WA) 分类: Brush Mode 2014-10-19 15:16 104人阅读 评论(0) 收藏

    #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> ...

  8. max_flow(Dinic) 分类: ACM TYPE 2014-09-02 15:42 94人阅读 评论(0) 收藏

    #include <cstdio> #include <iostream> #include <cstring> #include<queue> #in ...

  9. SQL 分组 加列 加自编号 自编号限定 分类: SQL Server 2014-11-25 15:41 283人阅读 评论(0) 收藏

    说明: (1)日期以年月形式显示:convert(varchar(7),字段名,120) , (2)加一列 (3)自编号: row_number() over(order by 字段名 desc) a ...

随机推荐

  1. 各大浏览器内核(Rendering Engine)

    记得刚开始写网页的时候,听童鞋们说各大浏览器的内核,也是懵懵懂懂的,知一不知其二,今天特地查一下: 内核只是一个通俗的说法,其英文名称为“Layout engine”,翻译过来就是“排版引擎”,也被称 ...

  2. ajax只是一个称呼

    记得刚入行的时候,看到ajax,即异步的javascript和xml这样一个概念,一点感觉都没有.参加工作前的第一轮面试,被问到有没有自己实现过ajax,我觉得自己实现肯定很复杂吧. 从名字理解 从名 ...

  3. [转] js call

    call 方法  转自: http://www.cnblogs.com/sweting/archive/2009/12/21/1629204.html调用一个对象的一个方法,以另一个对象替换当前对象. ...

  4. SVN遇到Can't convert string from 'UTF-8' to native encoding

    刚配好mysql,svn co代码的时候遇到问题 svn: Can't convert string from 'UTF-8' to native encoding: svn: platform/co ...

  5. 简单学c——前言

      1.学C语言需要什么基础吗? 零基础. 2.什么是C语言? C语言是一种编程语言. 3.什么是编程语言? 编程语言是用来定义计算机程序的形式语言,是一种被标准化的交流技巧,用来向计算机发出指令. ...

  6. matlab中元胞数组(cell)转换为矩阵

    matlab中元胞数组(cell)转换为矩阵. cell转换为矩阵函数为:cell2mat(c),其中c为待转换的元胞数组: 转化之后的矩阵可能不满足我们对矩阵维数的要求,那么也许还需要下面两个函数: ...

  7. 【Java】WEB-INF目录与META-INF目录的作用

    /WEB-INF/web.xml Web应用程序配置文件,描述了 servlet 和其他的应用组件配置及命名规则. /WEB-INF/classes/包含了站点所有用的 class 文件,包括 ser ...

  8. Android应用程序的生命周期

    转自Android应用程序的生命周期 在对一个简单的Hello World工程进行项目结构剖析后,我们接着来学习下一个Android应用程序的生命周期是怎么样的,以便为后面的开发有个垫下良好的基石~ ...

  9. 使用Python实现Hadoop MapReduce程序

    转自:使用Python实现Hadoop MapReduce程序 英文原文:Writing an Hadoop MapReduce Program in Python 根据上面两篇文章,下面是我在自己的 ...

  10. 用正则式判断URL是否合法-python

    import sys import re #Make sure we have a single URL argument. if len(sys.argv) != 2: print (sys.std ...