HybridList - A fast N lg (N) sort algorithm for lists

Management of large dictionaries is always a big problem.  That's why I decided to create an efficient and simple algorithm to manage very long sorted lists.  The HybridList algorithm resembles skip-lists but it differs considerably in basic principles and memory usage.  I hate the idea of allocating memory blocks for an array of pointers and predicting the number of nodes in a nonexistent list.

Main features

  • Search, insert, delete operations require  O(lg n) comparisons in all cases
  • As every insertion sort it is stable - it retains the original ordering of  identical keys 
  • It is in-place algorithm - no additional memory or stack is required
  • Data  can be linked in simple as well as double-linked list
  • Only 4 bytes for 'Next' pointer are required in user data structure

Concept

There are two types of nodes: user bottom-nodes (blue circles) and top-nodes (green rectangles) that  create a kind of hierarchy. When user adds/removes bottom-node to/from the list, 'parent' top-node on the first level is checked to keep 10-30 bottom nodes. If node has more than 30 nodes, then new node is created and both nodes have 15 subnodes. If node has less than 10 subnodes, its right neighbor is removed. Therefore hierarchy is always quite balanced.

QList_scheme.gif (6718 bytes)

Simple example

 
 
  1. #include "HybridList.h"
  2. class SampleNode : public HListNode <SampleNode>
  3. {
  4. // defines Prev,Next members
  5. public:
  6. int Value; // random value to sort
  7. SampleNode() { Value = rand(); }
  8. bool operator ==(SampleNode& comp) {return Value ==comp.Value;}
  9. bool operator > (SampleNode& comp) {return Value > comp.Value;}
  10. bool operator < (SampleNode& comp) {return Value < comp.Value;}
  11. };
  12. bool TEST()
  13. {
  14. HybridList<SampleNode> List;
  15. for (int i=0; i < 1000; i++)
  16. List.InsertSorted (new SampleNode);
  17. SampleNode to_find;
  18. SampleNode * first_equal = List.FindEqual(to_find);
  19. while (true)
  20. {
  21. SampleNode * head = List.RemoveHead();
  22. if (!head)
  23. break; // end of list achieved
  24. printf ("%ld->",head->Value);
  25. delete head;
  26. }
  27. return true;
  28. }

Speed

There is no sense to compare O(lg n) algorithm with O(n) algorithm. But even  O(lg n) algorithms, like binary trees, red-black trees, skip-lists have different speed due to balancing techniques, memory usage, comparison function, etc. My preliminary tests of red-black tree, that seems to be the fastest algorithm for dictionaries, shows that HybridList is more than twice as fast. But it seems to me to be not so important with regard to memory usage issue: HybridList node require only additional 4 byte for 'Next' pointer and red-black node require 12-16 bytes.

More details  you can find on http://members.tripod.com/~DanKozub. Timing diagram and comparison with Quicksort and test exe-file are also available there.

Source

The algorithm is implemented in C++ . In principle it does not require any additional libraries or system support. But I think some insignificant changes will be needed to make it run on other platforms than Windows 95/NT.


转自:http://www.codeguru.com/cpp/cpp/cpp_mfc/collections/article.php/c835/HybridList--A-fast-N-lg-N-sort-algorithm-for-lists.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值