惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
宝玉的分享
宝玉的分享
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
J
Java Code Geeks
腾讯CDC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Engineering at Meta
Engineering at Meta
L
LangChain Blog
Jina AI
Jina AI
博客园 - 叶小钗
B
Blog RSS Feed
Recent Announcements
Recent Announcements
H
Help Net Security
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
B
Blog
云风的 BLOG
云风的 BLOG
Blog — PlanetScale
Blog — PlanetScale
D
DataBreaches.Net
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
罗磊的独立博客

博客园 - 蓝之风

解决:gradle 前言中不允许有内容 【LINQ标准查询操作符总结】之聚合操符 RxJS 中的创建操作符 项目管理(1) 什么是项目? 数据库表数据统计及数据表的数据大小统计SQL C# 中一些类关系的判定方法 Windows Azure NotificationHub+Firebase Cloud Message 实现消息推动(付源码) Windows 下安装RabbitMQ服务器及基本配置 【设计模式】组合模式实现二叉树先序遍历,中序遍历和后序遍历 【设计模式】组合模式 Composite Pattern 【设计模式】桥接模式 Bridge Pattern 在VS 2017 下创建 Xamarin NuGet Package 【设计模式】适配器模式 Adapter Pattern 基础查找算法(顺序无序查找算法和递归二叉查找算法) 【设计模式】原型模式 Pototype Pattern 【设计模式】单例模式 Singleton Pattern 【设计模式】建造者模式 Builder Pattern 【设计模式】抽象工厂模式 Abstract Factory Pattern 【设计模式】工厂方法模式 Factory Method Pattern
三个基本排序算法执行效率比较(冒泡排序,选择排序和插入排序)
蓝之风 · 2018-08-05 · via 博客园 - 蓝之风

三个基本排序算法执行效率比较(冒泡排序,选择排序和插入排序)

2018-08-05 19:50  蓝之风  阅读(19588)  评论()    收藏  举报

1、冒泡算法。

冒泡算法是最基础的一个排序算法,每次使用第一个值和身后相邻的值进行比较,如果是升序将大数向左边交换,降序则向右边交换。最终将大数移动到一边,最终排成一个序列:

public class Sorting
{
    public void BubbleSorting()
    {
        int[] arr = new int[10];
        Random rd = new Random(100);
        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] = rd.Next(1, 100);
        }
        Console.WriteLine("Init array:");
        Print(arr);
        Console.WriteLine("Sorting:");
        for (int i = arr.Length - 1; i >= 1; i--)
        {
            for (int j = 0; j <= i - 1; j++)
            {
                if (arr[j] > arr[j + 1])
                {
                    Swap(ref arr[j], ref arr[j + 1]);
                }
            }

            Print(arr);
        }
        Console.WriteLine("Sort Result:");
        Print(arr);
    }

    void Swap(ref int a, ref int b)
    {
        var temp = a;
        a = b;
        b = temp;
    }
    void Print(int[] list)
    {
        for (int i = 0; i < list.Length; i++)
        {
            Console.Write("" + list[i]);
        } 
        Console.WriteLine();
    }
}

结果:

image

2、选择排序

选择排序需要两层循环来实现,外层循环控制次数,内层循环控制找到最小的值。然后将内层循环找到的最小值与外层循环本次索引对应元素进行交换,直至遍历完整个数组。

public void SelectionSort()
{
    int[] arr = InitArray(10);
    int length = 10;
    Console.WriteLine("Sorting:");
    for (int i = 0; i < length; i++)
    {
        int min = i;
        for (int j = i + 1; j < length; j++)
        {
            if (arr[min] > arr[j]) min = j;
        }
        Swap(ref arr[i], ref arr[min]);
        Print(arr);
    }
    Console.WriteLine("Sort Result:");
    Print(arr);
}

结果:

image

3、插入排序

插入排序有两层循环,外层循环逐个遍历数组元素,内层循环把外层循环的元素与该元素在内层循环的下一个元素进行比较,如果外层循环选择的元素小于内层循环选择的元素,那么数组元素都行右移动为内层循环元素留出位置。

public void InsertionSort()
{
    int[] arr = InitArray(10);
    int length = 10;
    Console.WriteLine("Sorting:");
    for (int i = 1; i < length; i++)
    {
        int temp = arr[i];
        int inner=i;
        while (inner > 0 && arr[inner - 1] >= temp)
        {
            arr[inner] = arr[inner - 1];
            inner--;
        }
        arr[inner] = temp;
        Print(arr);
    }
    Console.WriteLine("Sort Result:");
    Print(arr);
}

结果:

image

4、三种算法的效率

做一个简单的比较,这里的初始化数据在每种算法之中, 因为每个算法都包含初始化,因此不会影响到比较.

测试代码:

static void TestSorting(int size)
{    
    Sorting sort = new Sorting();
    Stopwatch watch = new Stopwatch();
    watch.Start();
    sort.BubbleAscSorting(size);
    watch.Stop();
    Console.WriteLine("BubbleAscSorting Time Milliseconds:" + watch.ElapsedMilliseconds);
    watch.Restart();
    sort.SelectionSort(size);
    watch.Stop();
    Console.WriteLine("SelectionSort Time Milliseconds:" + watch.ElapsedMilliseconds);
    watch.Restart();
    sort.InsertionSort(size);
    Console.WriteLine("InsertionSort Time Milliseconds:" + watch.ElapsedMilliseconds);
    Console.ReadKey();
}

1000个整数结果:

 image

10000个整数结果:

image

100000个整数结果:

image

从测试结果得到下面的表格:

  Bubble Selection Insertion Bubble/Selection Bubble/Insertion Selection/Insertion
1000 15 4 3 3.75 5 1.333333333
10000 1342 412 283 3.257281553 4.74204947 1.455830389
100000 125212 40794 27570 3.069372947 4.541603192 1.479651795
             
Avg       3.358884833 4.761217554 1.422938506

忽略测试环境,因为三个算法都是在同一个环境中跑的, 可以得出如下结论:

1.冒泡算法效率最低。

2.插入算法效率最高。

3.选择算法是冒泡算法的3.3倍。

4.插入算法是冒泡算法的4.7倍。

5.插入算法是选择算法的1.4陪。