473,836 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2729
<ha*******@gmai l.comwrote in message
news:70******** *************** ***********@59g 2000hsb.googleg roups.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.progr amming 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
2091
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
1503
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? #include <stdio.h> #include <stdlib.h> /*---data type---*/ typedef int type; /*---------------*/
6
4021
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 a new object of class T, I would like to check if this object already exists in the list: meaning one having same integers. This can be done in linear time in a list, and probably faster if I use STL Set instead of list. I am wondering however if...
19
2071
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 list sorted. I can clearly do this with a series of if statements: if y<x: n = 0 elif y < x: n = 1
33
5579
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 practically a red-black tree where insert/delete takes O(lg n) time. Or are there some other explanation for this complexity?
11
2022
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 the same exact sorting counts also..shouldn't they differ somewhat? Source and data file below. Thanks
7
5922
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
1872
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, that I can set from a variable (one time)!!! When my application starts, the user is prompted to make a selection from a list. I want that selection to go into a global variable, and NEVER CHANGE
6
5296
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 can do this with a linked list but is it possible to do the same thing with the standard containers? Thanks in advance, Carter.
0
9812
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9658
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10534
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10579
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10244
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5644
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5814
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3103
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.