How to choose the right data structure in different scenarios to bring out best performance?
2010-09-09 16:59
507 查看
A. HashTable, Dictionary<TKey, TValue>, SortedDictionary<TKey, TValue>, SortedList<TKey, TValue>
0. HashTable, Dictionary<TKey, TValue>, 基于哈希表。SortedDictionary<TKey, TValue>基于二叉搜索树,SortedList<TKey, TValue>基于二分法查找。
1. 首先,HashTable, Dictionary<TKey, TValue>查询数据都比后两种数据结构快,时间复杂度为O(1);对于插入数据,也比后两种快,因为后面的两种数据结构需要sort嘛。
2. 查询集合中的数据,SortedDictionary<TKey, TValue>, SortedList<TKey, TValue>二者的时间复杂度均为O(logn)
3. 对于未排序的数据,插入数据SortedDictionary<TKey, TValue>比SortedList<TKey, TValue>要快。
4. 对于已经排好序的数据,由于SortedList是基于二分法查找,所以插入数据SortedList<TKey, TValue>比SortedDictionary<TKey, TValue>快一些。
Skip List
A skip list is a data structure for storing a sorted list of items, using a hierarchy of linked lists that connect increasingly sparse subsequences of the items.
几种排序算法效率的比较
1.稳定性比较
插入排序、冒泡排序、二叉树排序、二路归并排序及其他线形排序是稳定的
选择排序、希尔排序、快速排序、堆排序是不稳定的
2.时间复杂性比较
插入排序、冒泡排序、选择排序的时间复杂性为O(n2)
其它非线形排序的时间复杂性为O(nlog2n)
线形排序的时间复杂性为O(n);
3.辅助空间的比较
线形排序、二路归并排序的辅助空间为O(n),其它排序的辅助空间为O(1);
4.其它比较
插入、冒泡排序的速度较慢,但参加排序的序列局部或整体有序时,这种排序能达到较快的速度。
反而在这种情况下,快速排序反而慢了。
当n较小时,对稳定性不作要求时宜用选择排序,对稳定性有要求时宜用插入或冒泡排序。
若待排序的记录的关键字在一个明显有限范围内时,且空间允许是用桶排序。
当n较大时,关键字元素比较随机,对稳定性没要求宜用快速排序。
当n较大时,关键字元素可能出现本身是有序的,对稳定性有要求时,空间允许的情况下。
宜用归并排序。
当n较大时,关键字元素可能出现本身是有序的,对稳定性没有要求时宜用堆排序。
0. HashTable, Dictionary<TKey, TValue>, 基于哈希表。SortedDictionary<TKey, TValue>基于二叉搜索树,SortedList<TKey, TValue>基于二分法查找。
1. 首先,HashTable, Dictionary<TKey, TValue>查询数据都比后两种数据结构快,时间复杂度为O(1);对于插入数据,也比后两种快,因为后面的两种数据结构需要sort嘛。
2. 查询集合中的数据,SortedDictionary<TKey, TValue>, SortedList<TKey, TValue>二者的时间复杂度均为O(logn)
3. 对于未排序的数据,插入数据SortedDictionary<TKey, TValue>比SortedList<TKey, TValue>要快。
4. 对于已经排好序的数据,由于SortedList是基于二分法查找,所以插入数据SortedList<TKey, TValue>比SortedDictionary<TKey, TValue>快一些。
Skip List
A skip list is a data structure for storing a sorted list of items, using a hierarchy of linked lists that connect increasingly sparse subsequences of the items.
几种排序算法效率的比较
1.稳定性比较
插入排序、冒泡排序、二叉树排序、二路归并排序及其他线形排序是稳定的
选择排序、希尔排序、快速排序、堆排序是不稳定的
2.时间复杂性比较
插入排序、冒泡排序、选择排序的时间复杂性为O(n2)
其它非线形排序的时间复杂性为O(nlog2n)
线形排序的时间复杂性为O(n);
3.辅助空间的比较
线形排序、二路归并排序的辅助空间为O(n),其它排序的辅助空间为O(1);
4.其它比较
插入、冒泡排序的速度较慢,但参加排序的序列局部或整体有序时,这种排序能达到较快的速度。
反而在这种情况下,快速排序反而慢了。
当n较小时,对稳定性不作要求时宜用选择排序,对稳定性有要求时宜用插入或冒泡排序。
若待排序的记录的关键字在一个明显有限范围内时,且空间允许是用桶排序。
当n较大时,关键字元素比较随机,对稳定性没要求宜用快速排序。
当n较大时,关键字元素可能出现本身是有序的,对稳定性有要求时,空间允许的情况下。
宜用归并排序。
当n较大时,关键字元素可能出现本身是有序的,对稳定性没有要求时宜用堆排序。
相关文章推荐
- How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC
- 【引】How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC
- How To contain multiple fileds in the querystrig, DataNavigateUrlFormatString=xxx.asp?ID={0}&Name={1}
- use noscript html tag when user disable the javascript in browser, guide user how to enable the js in different browser and retu
- Fiori2.0-How to pass the Parameterin in the different views?
- Problem 15 - Starting in the top left corner in a 20 by 20 grid, how many routes are there to the bottom right corner?
- The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
- mysql5.7.12/13在安装新实例时报错:InnoDB: auto-extending data file ./ibdata1 is of a different size 640 pages (rounded down to MB) than specified in the .cnf file: initial 768 pages, max 0 (relevant if non-zero
- How To Choose The Best XML Parser for Your iPhone Project
- What is the best way to handle Invalid CSRF token found in the request when session times out in Spring security
- How to hide my site and my document link in the right-top corner of sharepoint 2010
- HOW TO: Change the Owner of a User-Defined Data Type That Is in Use in SQL Server 2000
- EF 6.0 The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. c#中的时间
- How to avoid Excel from prompting "The file you are trying to open, , is in a different format than specified by the file extension"? (转)
- How to choose the number of topics/partitions in a Kafka cluster?
- How to Get the Frequency Table of a Categorical Variable as a Data Frame in R
- How to write the fast code/ high performance in C#
- How to use the System Restore API to save and to restore system data in Visual C++
- InnoDB: auto-extending data file ./ibdata1 is of a different size 640 pages (rounded down to MB) than specified in the .cnf file: initial 768 pages, max 0 (relevant if non-zero) pages!