C#数组排序方法

时间:2023-03-09 19:20:54
C#数组排序方法

在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