数据结构排序大全(C#)

排序大全

1.稳定性比较

 插入排序、冒泡排序、二叉树排序、归并排序及其他线形排序是稳定的

 选择排序、希尔排序、快速排序、堆排序是不稳定的

2.时间复杂性比较

 插入排序、冒泡排序、选择排序的时间复杂性为O(n2)

 其它非线形排序的时间复杂性为O(nlog2n)

 线形排序的时间复杂性为O(n);

3.辅助空间的比较

归并排序的辅助空间为O(n),其它排序的辅助空间为O(1);

4. C# Codes

4.1 冒泡排序

    class Program

    {

        static void Main(string[] args)

        {

            int[] array = new int[] { 6, 6, 5, 4, 3, 2, 1, 0 };

            array = Sort(array);

            foreach (var item in array)

            {

                Console.WriteLine(item.ToString());

            }

            Console.ReadKey();

        }

 

        static int[] Sort(int[] array)

        {

            for (int i = 0; i < array.Length; i++)

            {

                for (int j = 0; j < array.Length - i -1; j++)

                {

                    if (array[j+1] < array[j])

                    {

                        Swap(array, j, j+1);

                    }

                }

            }

            return array;

        }

 

        static void Swap(int[] array, int firstIndex, int secondIndex)

        {

            int preserve = array[firstIndex];

            array[firstIndex] = array[secondIndex];

            array[secondIndex] = preserve;

        }

    }

4.2 简单选择排序

    class Program

    {

        static void Main(string[] args)

        {

            int[] array = new int[] { 6, 6, 5, 4, 3, 2, 1, 0 };

            array = SelectSort(array);

            foreach (var item in array)

            {

                Console.WriteLine(item.ToString());

            }

            Console.ReadKey();

        }

 

        static int[] SelectSort(int[] array)

        {

            for (int i = 0; i < array.Length; i++)

            {

                for (int j = i; j < array.Length; j++)

                {

                    if (array[j] < array[i])

                    {

                        Swap(array, j, i);

                    }

                }

            }

            return array;

        }

 

        static void Swap(int[] array, int firstIndex, int secondIndex)

        {

            int preserve = array[firstIndex];

            array[firstIndex] = array[secondIndex];

            array[secondIndex] = preserve;

        }

    }

4.3 快速排序

    class Program

    {

        static void Main(string[] args)

        {

            int[] array = new int[] { 6, 6, 5, 4, 3, 2, 1, 0 };

            array = QuickSort(array, 0, array.Length - 1);

            foreach (var item in array)

            {

                Console.WriteLine(item.ToString());

            }

            Console.ReadKey();

        }

 

        static int[] QuickSort(int[] array,int start, int end)

        {

            int low = start;

            int high = end;

            int mark = start;

            while (low < high)

            {

                while (low < mark)

                {

                    if (array[low] > array[mark])

                    {

                        Swap(array, low, mark);

                        mark = low;

                        continue;

                    }

                    else

                        low++;

                }

                while (mark < high)

                {

                    if (array[mark] > array[high])

                    {

                        Swap(array, high, mark);

                        mark = high;

                        continue;

                    }

                    else

                        high--;

                }

            }

            if (mark - start > 1)

            {

                QuickSort(array, start, mark-1);

            }

            if (end - mark > 1)

            {

                QuickSort(array, mark+1, end);

            }

            return array;

        }

 

        static void Swap(int[] array, int firstIndex, int secondIndex)

        {

            int preserve = array[firstIndex];

            array[firstIndex] = array[secondIndex];

            array[secondIndex] = preserve;

        }

    }

4.4 堆排序

class Program

    {

        static void Main(string[] args)

        {

            int[] array = new int[] {5,6,7,8,2,3,4,6,5,4,3,2 };

            array = HeapSort(array);

            foreach (var item in array)

            {

                Console.WriteLine(item.ToString());

            }

            Console.ReadKey();

        }

 

        public static void CreateHeap(int[] array, int lastIndex)

        {

            for (int i = lastIndex; i > 0; i--)

            {

                if (array[i] > array[i / 2])

                {

                    Swap(array, i, i / 2);

                }

            }

        }

 

        public static int[] HeapSort(int[] array)

        {

            for (int i = array.Length - 1; i > 0; i--)

            {

                CreateHeap(array, i);

                Swap(array, 0, i);

            }

            return array;

        }

 

        static void Swap(int[] array, int firstIndex, int secondIndex)

        {

            int preserve = array[firstIndex];

            array[firstIndex] = array[secondIndex];

            array[secondIndex] = preserve;

        }

    }

4.5 二分插入排序

class Program

    {

        static void Main(string[] args)

        {

            int[] array = new int[] { 3,2,1,4,5,67,678678,234234,2,3,4,5,6,7,8,4 };

            array = HalfInsertSort(array);

            foreach (var item in array)

            {

                Console.WriteLine(item.ToString());

            }

            Console.ReadKey();

        }

 

        public static int[] HalfInsertSort(int[] array)

        {

            for (int i = 0; i < array.Length; i++)

            {

                int start = 0;

                int end = i - 1;

 

                while (start <= end)

                {

                    int middle = (start + end) / 2;

                    if (array[middle] <= array[i])

                        start = middle + 1;

                    else

                        end = middle - 1;

                }

 

                int insertValue = array[i];

                for (int j = i; j > start; j--)

                {

                    array[j] = array[j - 1];

                }

                array[start] = insertValue;

            }

            return array;

        }

    }

4.6 直接插入排序

class Program

    {

        static void Main(string[] args)

        {

            int[] array = new int[] { 6, 6, 5, 4, 3, 2, 1, 0 };

            array = InsertSort(array);

            foreach (var item in array)

            {

                Console.WriteLine(item.ToString());

            }

            Console.ReadKey();

        }

 

        static int[] InsertSort(int[] array)

        {

            for (int i = 0; i < array.Length; i++)

            {

                for (int j = 0; j < i; j++)

                {

                    if (array[j] > array[i])

                    {

                        Swap(array, j, i);

                    }

                }

            }

            return array;

        }

 

        static void Swap(int[] array, int firstIndex, int secondIndex)

        {

            int preserve = array[firstIndex];

            array[firstIndex] = array[secondIndex];

            array[secondIndex] = preserve;

        }

    }

4.7 归并排序

class Program

    {

        static void Main(string[] args)

        {

            int[] array = new int[] {1,1,2,2,2,2,3,3,2,2,2,22222222,1,1,1,1,1,1,1,1,0,0,0,0,7};

            array = MergeSort(array, 0, array.Length - 1);

            foreach (var item in array)

            {

                Console.WriteLine(item.ToString());

            }

            Console.ReadKey();

        }

 

        public static int[] MergeSort(int[] array, int start, int end)

        {

            int middle = start;

            if (start < end)

            {

                middle = (start + end) / 2;

                MergeSort(array, start, middle);

                MergeSort(array, middle + 1, end);

                Merge(array, start, end);

            }

            return array;

        }

 

        public static void Merge(int[] array, int start, int end)

        {

            int middle = (start + end) / 2;

 

            Queue<int> container = new Queue<int>();

 

            int first = start;

            int second = middle + 1;

 

            while (first <= middle && second <= end)

            {

                if (array[first] < array[second])

                {

                    container.Enqueue(array[first++]);

                }

                else

                {

                    container.Enqueue(array[second++]);

                }

            }

 

            while (first <= middle)

            {

                container.Enqueue(array[first++]);

            }

 

            while (second <= end)

            {

                container.Enqueue(array[second++]);

            }

 

            for (int i = start; i <= end; i++)

            {

                array[i] = container.Dequeue();

            }

        }

    }

4.8 希尔排序

class Program

    {

        static void Main(string[] args)

        {

            int[] array = new int[] { 3,2,1,4,5,67,678678,234234,31231,1,3,3,3,3,3,3,3,3,2,2,2,1,1,1,1,1,1,12,3,3,3,3,32,3,4,5,6,7,8,4 };

 

            foreach (var item in ShellSort(array))

            {

                Console.WriteLine(item.ToString());

            }

            Console.ReadKey();

        }

 

        public static int[] ShellSort(int[] array)

        {

            int point = array.Length / 2;

 

            while (point > 0)

            {

                for (int i = 0; i < point; i ++)

                {

                    for (int j = i; j < array.Length; j += point)

                    {

                        for (int k = i; k < j; k += point)

                        {

                            if (array[k] > array[j])

                            {

                                int insertValue=array[j];

                                for (int t = j; t > k; t -= point)

                                {

                                    array[t] = array[t - point];

                                }

                                array[k] = insertValue;

                            }

                            continue;

                        }

                    }

                }

                point = point / 2;

            }

            return array;

        }

    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值