在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. Solr highlight

    hl.preserveMulti 默认是false.Set to true to perform highlighting on all values of a multivalued field a ...

  2. 下拉刷新,上拉加载功能--dropload.js的使用

    这段时间工作太忙了,没时间更新博客内容,在这段时间,也学习到了不少新的知识.今天先整理一下dropload.js的使用方法吧,这个是在为项目中使用过的插件,很好用,但是真正用到项目中还是会有一些小小的 ...

  3. 微服务监控神器Prometheus的安装部署

    本文涉及:如何在k8s下搭建Prometheus+grafana的监控环境 基本概念 Prometheus提供了容器和云原生领域数据搜集.存储.处理.可视化和告警一套完整的解决方案,最初时是由Soun ...

  4. OSU!

    题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll read(){ ,f= ...

  5. POJ 3299

    #include <iostream> #include "math.h" double e2h(double e) { return 0.5555*(e-10.0); ...

  6. 送气球.jpg(模拟)

    链接:https://ac.nowcoder.com/acm/contest/318/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...

  7. CodeForces - 287B-Pipeline(二分)

    Vova, the Ultimate Thule new shaman, wants to build a pipeline. As there are exactly n houses in Ult ...

  8. 寒假作业第二组E题题解

    注意看题,注意看题,注意看题.重要的事情三遍感觉都不够.不怕大家笑话,这道题RuntimeError 9次,我一直以为是哪里越界了,结果最后发现的时候,真是无语了,题目里说了,所有的integer都不 ...

  9. python大战机器学习——人工神经网络

    人工神经网络是有一系列简单的单元相互紧密联系构成的,每个单元有一定数量的实数输入和唯一的实数输出.神经网络的一个重要的用途就是接受和处理传感器产生的复杂的输入并进行自适应性的学习,是一种模式匹配算法, ...

  10. 我的grunt配置

    module.exports = function(grunt) { // 配置. grunt.initConfig({ pkg: grunt.file.readJSON('package.json' ...