LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2005, 07:59 AM   #1
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
sorting a map (C++)


hiho@ll

i have a std::map<string,float>
which maps ip's to their load averages for the last minute

now i need those ip's who has the lowest load!

anybody knows if there is a sort algorithm i can use for a map, which sorts using the value's
don't really have the time to write a complex algorithm by myself

for now i only have a simple loop which iterates through the map
but i think it will be a little bit slow

anybody has a better idea?

thx@ll
 
Old 09-13-2005, 08:34 AM   #2
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
If you need the entry with the lowest value only once, it is by far faster to iterate over the complete map (complexity: O(n)) than to sort the map and then pick the first element (complexity: O(n*log(n)).
However, if you want to sort nevertheless: Chapter 17.1 gives you the information you need.
 
Old 09-13-2005, 09:06 AM   #3
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Why not use a linkedlist with a paired values structure, which you keep sorted.
 
Old 09-13-2005, 10:13 AM   #4
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
i'm not sure, Proud, but to use a linked list i need my own sort algorithm, because the values of the linked list are pairs and the algorithm itself needs to know that they are pairs
so it's not a standard simple algorithm
(and i don't have time ;-( )

thx@addy86
 
Old 09-13-2005, 11:35 AM   #5
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Well iirc with a double linked list if you insert items in sorted order as you build it up (O(log(n) ?) then finding the lowest load would take O(1), and you wouldnt need to have a sorting algorithm as such, as you simply assume the list is sorted when inserting or deleting. But I'm rusty after my summer after my Algorithms and Data Structures module.

Edit: searching would take O(log(n)), finding the lowest load would take O(1).

Last edited by Proud; 09-13-2005 at 11:37 AM.
 
Old 09-13-2005, 12:54 PM   #6
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
hmm

well i understand

but because for now it's easier to search i do this
if it's too slow i will use "find"-way

thx!!
 
Old 09-13-2005, 04:52 PM   #7
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Quote:
for now i only have a simple loop which iterates through the map but i think it will be a little bit slow
Try it first! Unless you have millions of ip addresses, it probably isn't.

Quote:
... but to use a linked list i need my own sort algorithm ...
Use a std::vector and std::sort.
Write your own comparison operator i.e. function or functor that takes two objects and returns bool.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting data. unreal128 Linux - Software 1 09-20-2005 06:31 PM
SYSERR(root): hash map "generics": missing map file /etc/mail/genericstable.db? singying304 Linux - Networking 4 02-28-2005 06:49 AM
Which sorting algorithm? nodger Programming 6 01-28-2005 06:13 PM
Sorting Beppe83 Linux - Software 7 06-21-2004 09:10 AM
Sorting question AMMullan Linux - General 12 01-19-2004 03:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration