C# 数组排序方式总结

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


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


namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            // C#选择排序法
            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
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值