473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Constant time insertion into a sorted list?

I'm trying to create a constant time event timer. Basically, a routine
can set a callback to be called n ms from the current time, and the
main event loop will wait until the delta between the current time and
the earliest event timer has elapsed. When the list is sorted,
checking for expiration is O(n) time where n is the number of timers
that have expired, or O(1) in the case where no timers have expired
(which is important in an I/O event/timer loop). We do this by walking
up the list from the tail until we reach the end, or the first timer
that will expire in the future.

Adding or updating a timer can be constant time if every time interval
from the current time is constant. If every timer will fire 5 seconds
from when the timer is set, it's a simple matter of pushing all new
timers to the tail of the list, and it remains sorted.

Has there been an algorithm invented yet that can insert a non-
constant integer (time until this timer expires) into a list of
integers (other timers) such that the list remains sorted in constant
time?

The only obvious choice that comes to mind is a divide-and-conquer
algorithm that continually divides the timer space in half until two
timers are found that are between the timer being inserted. But that
would take O(log n) where n is the number of timers in the list.
Getting progressively worse as more timers are added.

Timer wheels from the Linux kernel scheduler are fairly attractive
(radix sorting approach), but insertion isn't always constant if the
date is too far into the future.

Anyone have any ideas? If I think of something awesome, I will report
back. :)

Bob
Jul 16 '08 #1
1 2712
<ha*******@gmail.comwrote in message
news:70**********************************@59g2000h sb.googlegroups.com...
I'm trying to create a constant time event timer. Basically, a routine
can set a callback to be called n ms from the current time, and the
main event loop will wait until the delta between the current time and
the earliest event timer has elapsed. When the list is sorted,
checking for expiration is O(n) time where n is the number of timers
that have expired, or O(1) in the case where no timers have expired
(which is important in an I/O event/timer loop). We do this by walking
up the list from the tail until we reach the end, or the first timer
that will expire in the future.

Adding or updating a timer can be constant time if every time interval
from the current time is constant. If every timer will fire 5 seconds
from when the timer is set, it's a simple matter of pushing all new
timers to the tail of the list, and it remains sorted.

Has there been an algorithm invented yet that can insert a non-
constant integer (time until this timer expires) into a list of
integers (other timers) such that the list remains sorted in constant
time?

The only obvious choice that comes to mind is a divide-and-conquer
algorithm that continually divides the timer space in half until two
timers are found that are between the timer being inserted. But that
would take O(log n) where n is the number of timers in the list.
Getting progressively worse as more timers are added.

Timer wheels from the Linux kernel scheduler are fairly attractive
(radix sorting approach), but insertion isn't always constant if the
date is too far into the future.

Anyone have any ideas? If I think of something awesome, I will report
back. :)
Calendar queues and implicit binary heaps are approximately O(1) for the
enqueue operation.
http://www.leekillough.com/heaps/pap...7-ronngren.pdf

news:comp.programming is a better choice than news:comp.lang.c since you
don't appear to have a C question that I can discern.
** Posted from http://www.teranews.com **
Jul 16 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: chai | last post by:
I am trying out a program to insert an element to a sorted list(singly linked list)without using the temporary variable.Is there a solution for this problem?
3
by: Franco Perilli | last post by:
I've compiled this code and no problems, but when I run the program, it prints only the last entry i've inserted. Looks like a problem in the sorted insertion algorithm. Can u help me plz? ...
6
by: Amit Bhatia | last post by:
Hi, I am not sure if this belongs to this group. Anyway, my question is as follows: I have a list (STL list) whose elements are pairs of integers (STL pairs, say objects of class T). When I create...
19
by: tkpmep | last post by:
I have an ordered list e.g. x = , and given any positive integer y, I want to determine its appropriate position in the list (i.e the point at which I would have to insert it in order to keep the...
33
by: desktop | last post by:
In the C++ standard sec 23.1.2 table 69 it says that erase(q) where q is a pointer to an element can be done in amortized constant time. I guess that is not worst case since std::set is...
11
by: Trent | last post by:
Running this I see that on first run, both bubble and selection sort have 9 sort counts while insertion sort has ZERO. With a sorted list, should this be ZERO for all? Also bsort and Ssort have...
7
by: =?utf-8?B?5YiY5piK?= | last post by:
Hi, folks, Is it possible to delete an element from a sorted array with O(1) time? Best regards
13
by: bobg.hahc | last post by:
running access 2k; And before anything else is said - "Yes, Virginia, I know you can NOT use a variable to set a constant (that's why it's constant)". BUT - my problem is - I want a constant,...
6
by: Carter | last post by:
This is probably somewhat of a beginners question but I am unfamiliar with alot of aspects of C++. What is the correct way to append multiple STL containers together in constant time. Obviously you...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.