在C#中常用的数组排序的方法有:选择排序法、冒泡排序法、插入排序法和希尔排序法等。

一、选择排序法
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            // C#选择排序法-www.baike369.com
            int[] array = new int[] { 2, 18, 9, 26, 3, 7, 5, 10 };
            Console.Write("数组排序前的结果为:");
            foreach (int n in array)
            {
                Console.Write("{0}", n + " ");
            }
            Console.WriteLine();
            int min;
            for (int i = 0; i < array.Length - 1; i++)
            {
                min = i;
                for (int j = i + 1; j < array.Length; j++)
                {
                    if (array[j] < array[min])
                        min = j;
                }
                int t = array[min];
                array[min] = array[i];
                array[i] = t;
            }
            Console.Write("数组排序后的结果为:");
            foreach (int n in array)
            {
                Console.Write("{0}", n + " ");
            }
            Console.ReadLine();
        }
    }
}

运行结果:
 
数组排序前的结果为:2 18 9 26 3 7 5 10
数组排序后的结果为:2 3 5 7 9 10 18 26

二、冒泡排序法
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[] { 2, 18, 9, 26, 3, 7, 5, 10 };
            Console.Write("数组排序前的结果为:");
            foreach (int n in array)
            {
                Console.Write("{0}", n + " ");
            }
            Console.WriteLine();
            for (int i = 0; i < array.Length; i++)
            {
                for (int j = i; j < array.Length; j++)
                {
                    if (array[i] < array[j])
                    {
                        int temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }
            Console.Write("数组排序后的结果为:");
            foreach (int n in array)
            {
                Console.Write("{0}", n + " ");
            }
            Console.ReadLine();
        }
    }
}

运行结果:
 
数组排序前的结果为:2 18 9 26 3 7 5 10
数组排序后的结果为:26 18 10 9 7 5 3 2

三、插入排序法
  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[] { 2, 18, 9, 26, 3, 7, 5, 10 };
            Console.Write("数组排序前的结果为:");
            foreach (int n in array)
            {
                Console.Write("{0}", n + " ");
            }
            Console.WriteLine();
            for (int i = 1; i < array.Length; i++)
            {
                int t = array[i];
                int j = i;
                while ((j > 0) && (array[j - 1] > t))
                {
                    array[j] = array[j - 1];
                    --j;
                }
                array[j] = t;
            }
            Console.Write("数组排序后的结果为:");
            foreach (int n in array)
            {
                Console.Write("{0}", n + " ");
            }
            Console.ReadLine();
        }
    }
}

运行结果:
 
数组排序前的结果为:2 18 9 26 3 7 5 10
数组排序后的结果为:2 3 5 7 9 10 18 26四、希尔排序法
希尔排序是将组分段,然后进行插入排序。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[] { 2, 18, 9, 26, 3, 7, 5, 10 };
            Console.Write("数组排序前的结果为:");
            foreach (int n in array)
            {
                Console.Write("{0}", n + " ");
            }
            Console.WriteLine();
            int inc;
            for (inc = 1; inc <= array.Length / 9; inc = 3 * inc + 1) ;
            for (; inc > 0; inc /= 3)
            {
                for (int i = inc + 1; i <= array.Length; i += inc)
                {
                    int t = array[i - 1];
                    int j = i;
                    while ((j > inc) && (array[j - inc - 1] > t))
                    {
                        array[j - 1] = array[j - inc - 1];
                        j -= inc;
                    }
                    array[j - 1] = t;
                }
            }
            Console.Write("数组排序后的结果为:");
            foreach (int n in array)
            {
                Console.Write("{0}", n + " ");
            }
            Console.ReadLine();
        }
    }
}

运行结果:
  
数组排序前的结果为:2 18 9 26 3 7 5 10
数组排序后的结果为:2 3 5 7 9 10 18 26

 
 

C#数组排序方法的更多相关文章

  1. CSharpGL(36)通用的非托管数组排序方法

    CSharpGL(36)通用的非托管数组排序方法 如果OpenGL要渲染半透明物体,一个方法是根据顶点到窗口的距离排序,按照从远到近的顺序依次渲染.所以本篇介绍对 UnmanagedArray< ...

  2. 数组排序方法(join()、reverse()、sort())

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. ***PHP 数组排序 +php二维数组排序方法(PHP比较器)

    PHP - 一维数组的排序函数 在本节中,我们将学习如下 PHP 数组排序函数: sort() - 以升序对数组排序 rsort() - 以降序对数组排序 asort() - 根据值,以升序对关联数组 ...

  4. php二维数组排序方法(转自http://www.3lian.com/edu/2013/12-26/118882.html)

    一维数组排序可以使用asort.ksort等一些方法进程排序,相对来说比较简单.二维数组的排序怎么实现呢?使用array_multisort和usort可以实现 例如像下面的数组:    代码如下: ...

  5. PHP指定字段的多维数组排序方法

    PHP数组排序可以用array_multisort方法实现,但是如果是多维数组,并且我们要指定数组中的某个字段进行排序,那么这就需要我们自己写方法实现了. function sortArrByFiel ...

  6. javascript数组(1) ——sort的工作原理及其他数组排序方法

    一说到数组排序,最直观的想法就是用sort啊! 请问不用使用sort方法还可以使用什么方法进行数组排序? 比如 :  快速排序法.合并排序法.冒泡排序法.选择排序法.插入排序法.布尔排序法.交互排序. ...

  7. php二维数组排序方法(array_multisort usort)

    一维数组排序可以使用asort.ksort等一些方法进程排序,相对来说比较简单.二维数组的排序怎么实现呢?使用array_multisort和usort可以实现 例如像下面的数组: $users = ...

  8. php二维数组排序方法(array_multisort,usort)

    一维数组排序可以使用asort.ksort等一些方法进程排序,相对来说比较简单.二维数组的排序怎么实现呢?使用array_multisort和usort可以实现 例如像下面的数组: $users = ...

  9. PHP 数组排序方法总结

    sort:本函数为 array 中的单元赋予新的键名.这将删除原有的键名而不仅是重新排序. rsort:本函数对数组进行逆向排序(最高到最低). 删除原有的键名而不仅是重新排序. asort:对数组进 ...

随机推荐

  1. NetCore + Mysql + EF:No coercion operator is defined between types 'System.Int16' and 'System.Boolean',

    总结三种解决办法: 1.Mysql升级到7 2.Nuget安装Pomelo.EntityFrameworkCore.MySql 2.2.0替代MySql.Data.EntityFrameworkCor ...

  2. Nacos深入浅出(八)

    Nacos-spring-context.java 感觉这个后台要比之前的Nacos复杂多了,涉及到很多基础的概念,慢慢看,这个后面慢慢更新解析过程 看到他的目录结构一个是基于注解,一个是XML的解析 ...

  3. Python-15-收集参数

    允许用户提供任意数量的参数: def print_params(*params): print(params)   >>> print_params('Testing') ('Tes ...

  4. Educational Codeforces Round 65 (Rated for Div. 2) B. Lost Numbers

    链接:https://codeforces.com/contest/1167/problem/B 题意: This is an interactive problem. Remember to flu ...

  5. php:php相关的函数或用法记录

    //1:判断字符串是否全是字母组成的 $str = 'AAKAaa_aLJIGF'; var_dump(ctype_alpha($str)); //boolean false,全部是英文时才是返回tr ...

  6. Ubuntu 下修改Tomcat和Jetty默认的JDK和初始内存

    修改/etc/default/tomcat  或者  /etc/default/jetty   文件 中的 JAVA_HOME 和 JAVA_OPTS

  7. DevExpress PivotGrid 使用记录

    1.自定total值: 调试的时候,如果要定位,给一个index,然后,把e.CustomVale=index++;定位后,监视ds的值,每个ds的值不一样!

  8. 牛客网Java刷题知识点之正则表达式(Matcher、Pattern)

    不多说,直接上干货! 正则表达式是什么? 正则表达式是一种可以用于模式匹配和替换的规范, 一个正则表达式就是由普通的字符(例如字符a到z) 以及特殊字符(元字符) 组成的文字模式, 它用以描述在查找文 ...

  9. Vsftp设置为PASV mode(被动模式传送)

    首先配置vsftpd.conf文件: #vi /etc/vsftpd/vsftpd.conf 在文件的末尾加上: pasv_enable=YES pasv_max_port=30010 pasv_mi ...

  10. nuxt实践

    利用手脚架搭起来的服务端渲染实例目录结构.nuxtassets 未编译的静态资源如 LESS.SASS 或 JavaScriptcomponents 用于组织应用的 Vue.js 组件middlewa ...