zoukankan      html  css  js  c++  java
  • 冒泡排序与快速排序的性能比较

    冒泡排序和快速排序是典型的两种常用排序方式。

    冒泡排序:特点是用时间换空间,只考虑比较的次数的话,它是时间复杂度是O(n*n);空间复杂度S(1);

    快速排序:特点是用空间换时间,快速排序是一种不稳定的排序,理想情况每一次都将待排序数组划分成等长两个部分,则需要logn次划分,快速排序时间复杂度下界为O(nlogn),最坏情况是当把已经有序的序列在进行快速排序,快速排序时间复杂度上界为O(n*n)和冒泡排序一样(可以采用随机快速排序来改善),快速排序的平均时间复杂度为O(nlogn);空间复杂度S(logn),由于快速排序采用是递归,所以需要注意的是递归栈上需要花费最小logn和最多n的空间(最坏情况下);

    下面例子是关于冒泡排序和快速的排序时耗时性能比较,如果把冒泡排序排好的数组再用快速排序来排序,这就会形成快速排序最差情况,递归层次也是最多的,当数组元素达到一定数量,会造成堆栈溢出。

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace QuickSortConsole
    {
        class Program
        {
            static void Main(string[] args)
            {
                var list=new List<int>();
                var r=new Random();
    
                for (int i = 0; i < 100000; i++)
                {
                    var n = r.Next(0, 10001);
                    list.Add(n);
                }
    
                var arr1 = list.ToArray();
                var arr2 = list.ToArray();
    
                Console.WriteLine("冒泡:");
                Stopwatch watch = new Stopwatch();
                watch.Start();
                Sort(arr1);
                watch.Stop();
                Console.WriteLine(watch.Elapsed.TotalMilliseconds);
    
                Console.WriteLine("快速:");
                watch.Reset();
                watch.Start();
                QuickSort(arr2);
                watch.Stop();
                Console.WriteLine(watch.Elapsed.TotalMilliseconds);
    
                Console.Read();
            }
    
    
            //冒泡排序
            static void Sort(int[] arr)
            {
                for (int i = 0; i < arr.Length - 1; i++)
                {
                    for (int j = arr.Length-1; j >i ; j--)
                    {
                        if (arr[j] < arr[j - 1])
                        {
                            var temp = arr[j];
                            arr[j] = arr[j - 1];
                            arr[j - 1] = temp;
                        }
                    }
                }
            }
    
    
            //快速排序
            static void QuickSort(int[] arr)
            {
                QuickParttionSort(0, arr.Length - 1, arr);
            }
    
            static void QuickParttionSort(int low, int high, int[] arr)
            {
                if (low < high)
                {
                    int l = low;
                    int h = high;
    
                    int temp = arr[l];
    
                    do
                    {
                        while (arr[h]>=temp&&l<h)
                        {
                            h--;
                        }
    
                        if (l < h)
                        {
                            arr[l] = arr[h];
                            l++;
                        }
    
                        while (arr[l]<=temp&&l<h)
                        {
                            l++;
                        }
    
                        if (l < h)
                        {
                            arr[h] = arr[l];
                            h--;
                        }
                    } while (l < h);
    
    
                    arr[l] = temp;
    
                    QuickParttionSort(low,l-1,arr);
                    QuickParttionSort(h+1,high,arr);
                }
            }
        }
    }

  • 相关阅读:
    程其襄实变函数与泛函分析课件
    谢惠民答案
    谢惠民 数学分析习题课讲义 答案
    谢惠民数学分析习题课讲义下册参考解答
    重磅! 谢惠民下册参考解答已经全部完成, 共 473 页!
    各大高校考研试题参考解答目录2020/06/21版
    Jenkins Pipeline审批
    Zabbix监控DHCP作用域(json格式数据)
    MDT通过UserExit.vbs调用PowerShell脚本获取变量
    MDT通过PowerShell脚本自定义变量(自定义计算机名)
  • 原文地址:https://www.cnblogs.com/bravo2012/p/3208681.html
Copyright © 2011-2022 走看看