选择排序算法的标准是什么?(What are the criteria for choosing a sorting algorithm?)

系统教程 行业动态 更新时间:2024-06-14 16:59:46
选择排序算法的标准是什么?(What are the criteria for choosing a sorting algorithm?)

我正在阅读包括冒泡排序,选择排序,合并排序,堆排序,存储桶排序等的排序方法。它们还包含时间复杂度,这有助于我们了解哪种排序有效。 所以我有一个基本的问题。 如果我们包含数据而不是如何选择排序。 时间复杂度是帮助我们决定排序方法的参数之一。 但是我们有另外一个参数来选择排序方法吗?

试图找出更好的理解排序。

有一些关于堆排序的查询:

我们在哪里使用堆排序?

什么是堆排序的更大优势(除时间复杂度O(n log n))?

堆排序的缺点是什么?

什么是构建堆的时间? (我听说O(n)但我不确定。)

任何情况下,我们必须使用堆排序或堆排序更好的选择(除优先级队列)?

在对数据应用堆排序之前,我们将查看数据的参数是什么?

I was reading sorting method which include bubble sort, selection sort, merge sort, heap sort, bucket sort etc.. They also contain time complexity which help us to know which sorting is efficient. So I had a basic question. If we contain data than how will we be choose sorting. Time complexity is one of parameter which help us to decide sorting method. But do we have another parameter to choose sorting method?.

Just trying to figure out sorting for better understanding.

Having some query about heap sort:

Where do we use heap sort?

What is bigger advantage of heap sort (except time complexity O(n log n))?

What is disadvantage of heap sort?

What is build time for heap? (I heard O(n) but I'm not sure.)

Any scenario where we have to use heap sort or heap sort is better option (except priority queue)?

Before applying the heap sort on data, what are the parameter will we look into data?

最满意答案

排序算法的两个主要理论特征是时间复杂度和空间复杂度。

一般来说, 时间复杂度让我们知道算法的性能随着数据集大小的增加而变化。 需要考虑的事项:

您期望排序多少数据? 这将帮助您了解是否需要查找时间复杂度非常低的算法。 你的数据如何排序? 它会被部分排序吗? 随机排序? 这会影响排序算法的时间复杂度。 大多数算法会有最差和最好的情况 - 你想确保你没有对最坏情况的数据集使用算法。 时间复杂度与运行时间不一样。 请记住,时间复杂度仅描述算法的性能随着数据集大小的增加而变化。 总是可以通过所有输入的算法是O(n) - 它的性能与输入的大小成线性相关。 但是,总是对数据集进行两次遍历的算法也是O(n) - 即使常量(和实际运行时间)不同,相关性仍然是线性的。

同样,空间复杂性描述算法需要运行多少空间。 例如, 插入排序等简单排序需要额外的固定空间量来存储当前插入的元素的值。 这是O(1)的辅助空间复杂度 - 它不随输入大小而改变。 但是, 合并排序会在运行时在内存中创建额外的数组,其辅助空间复杂度为O(n)。 这意味着它需要的额外空间量与输入的大小呈线性相关。

当然,算法设计通常是时间和空间之间的折衷 - 具有低空间复杂度的算法可能需要更多时间,并且具有低时间复杂度的算法可能需要更多空间。

有关更多信息,您可能会发现本教程很有用。


要回答您更新的问题,您可能会发现堆排序上的维基百科页面有用。

The two main theoretical features of sorting algorithms are time complexity and space complexity.

In general, time complexity lets us know how the performance of the algorithm changes as the size of the data set increases. Things to consider:

How much data are you expecting to sort? This will help you know whether you need to look for an algorithm with a very low time complexity. How sorted will your data be already? Will it be partly sorted? Randomly sorted? This can affect the time complexity of the sorting algorithm. Most algorithms will have worst and best cases - you want to make sure you're not using an algorithm on a worst-case data set. Time complexity is not the same as running time. Remember that time complexity only describes how the performance of an algorithm varies as the size of the data set increases. An algorithm that always does one pass over all the input would be O(n) - it's performance is linearly correlated with the size of the input. But, an algorithm that always does two passes over the data set is also O(n) - the correlation is still linear, even if the constant (and actual running time) is different.

Similarly, space complexity describes how much space an algorithm needs to run. For example, a simple sort such as insertion sort needs an additional fixed amount of space to store the value of the element currently being inserted. This is an auxiliary space complexity of O(1) - it doesn't change with the size of the input. However, merge sort creates extra arrays in memory while it runs, with an auxiliary space complexity of O(n). This means the amount of extra space it requires is linearly correlated with the size of the input.

Of course, algorithm design is often a trade-off between time and space - algorithms with a low space complexity may require more time, and algoithms with a low time complexity may require more space.

For more information, you may find this tutorial useful.


To answer your updated question, you may find the wikipedia page on Heap Sort useful.

更多推荐

本文发布于:2023-04-17 08:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/3331012268dfff3117903b07aac956b8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   标准   criteria   algorithm   sorting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!