插入排序,希尔排序,归并排序

public class MainClass
{
    public static void Main()
    {
        int[] arr = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
        ShellSort1(arr);
        foreach (var item in arr)
        {
            Console.Write(item + " ");
        }
        Console.ReadKey();
    }

    static void ShellSort1(int[] arr)
    {
        //取增量
        int step = arr.Length / 2;

        while (step >= 1)
        {
            //无须序列
            for (int i = step; i < arr.Length; i++)
            {
                var temp = arr[i];

                int j;

                //有序序列
                for (j = i - step; j >= 0 && temp < arr[j]; j = j - step)
                {
                    arr[j + step] = arr[j];
                }
                arr[j + step] = temp;
            }
            step = step / 2;
        }
    }  

    static void InsertSort(int[] arr)
    {
        //无须序列
        for (int i = 1; i < arr.Length; i++)
        {
            var temp = arr[i];

            int j;

            //有序序列
            for (j = i - 1; j >= 0 && temp < arr[j]; j--)
            {
                arr[j + 1] = arr[j];
            }
            arr[j + 1] = temp;
        }
    }
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MergeSort
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = { 3, 2, 1, 8, 9, 0 };

            MergeSort(array, new int[array.Length], 0, array.Length - 1);

            Console.WriteLine(string.Join(",", array));
        }

        ///<summary>
/// 数组的划分
///</summary>
///<param name="array">待排序数组</param>
///<param name="temparray">临时存放数组</param>
///<param name="left">序列段的开始位置,</param>
///<param name="right">序列段的结束位置</param>
        static void MergeSort(int[] array, int[] temparray, int left, int right)
        {
            if (left < right)
            {
                //取分割位置
                int middle = (left + right) / 2;

                //递归划分数组左序列
                MergeSort(array, temparray, left, middle);

                //递归划分数组右序列
                MergeSort(array, temparray, middle + 1, right);

                //数组合并操作
                Merge(array, temparray, left, middle + 1, right);
            }
        }

        ///<summary>
/// 数组的两两合并操作
///</summary>
///<param name="array">待排序数组</param>
///<param name="temparray">临时数组</param>
///<param name="left">第一个区间段开始位置</param>
///<param name="middle">第二个区间的开始位置</param>
///<param name="right">第二个区间段结束位置</param>
        static void Merge(int[] array, int[] temparray, int left, int middle, int right)
        {
            //左指针尾
            int leftEnd = middle - 1;

            //右指针头
            int rightStart = middle;

            //临时数组的下标
            int tempIndex = left;

            //数组合并后的length长度
            int tempLength = right - left + 1;

            //先循环两个区间段都没有结束的情况
            while ((left <= leftEnd) && (rightStart <= right))
            {
                //如果发现有序列大,则将此数放入临时数组
                if (array[left] < array[rightStart])
                    temparray[tempIndex++] = array[left++];
                else
                    temparray[tempIndex++] = array[rightStart++];
            }

            //判断左序列是否结束
            while (left <= leftEnd)
                temparray[tempIndex++] = array[left++];

            //判断右序列是否结束
            while (rightStart <= right)
                temparray[tempIndex++] = array[rightStart++];

            //交换数据
            for (int i = 0; i < tempLength; i++)
            {
                array[right] = temparray[right];
                right--;
            }
        }
    }
}
View Code

插入排序的时间复杂度为:O(N^2)

     希尔排序的时间复杂度为:平均为:O(N^3/2)

                                       最坏: O(N^2)

     归并排序时间复杂度为: O(NlogN)

                空间复杂度为:  O(N) 

原文地址:https://www.cnblogs.com/futengsheng/p/8041278.html