Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 547381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:58:02+00:00 2026-05-13T10:58:02+00:00

I am looking for a structure that holds a sorted set of double values.

  • 0

I am looking for a structure that holds a sorted set of double values. I want to query this set to find the closest value to a specified reference value.

I have looked at the SortedList<double, double>, and it does quite well for me. However, since I do not need explicit key/value pairs. this seems to be overkill to me, and i wonder if i could do faster.

Conditions:

  • The structure is initialised only once, and does never change (no insert/deletes)
  • The amount of values is in the range of 100k.
  • The structure is queried often with new references, which must execute fast.
  • For simplicity and speed, the set’s value just below of the reference may be returned, not actually the nearest value
  • I want to use LINQ for the query, if possible, for simplicity of code.
  • I want to use no 3rd party code if possible. .NET 3.5 is available.
  • Speed is more importand than memory footprint

I currently use the following code, where SortedValues is the aforementioned SortedList

    IEnumerable<double> nearest = from item in SortedValues.Keys
                                  where item <= suggestion
                                  select item;
    return nearest.ElementAt(nearest.Count() - 1);

Can I do faster?

Also I am not 100% percent sure, if this code is really safe. IEnumerable, the return type of my query is not by definition sorted anymore. However, a Unit test with a large test data base has shown that it is in practice, so this works for me. Have you hints regarding this aspect?

P.S. I know that there are many similar questions, but none actually answers my specific needs. Especially there is this one C# Data Structure Like Dictionary But Without A Value, but the questioner does just want to check the existence not find anything.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:58:02+00:00Added an answer on May 13, 2026 at 10:58 am

    The way you are doing it is incredibly slow as it must search from the beginning of the list each time giving O(n) performance.

    A better way is to put the elements into a List and then sort the list. You say you don’t need to change the contents once initialized, so sorting once is enough.

    Then you can use List<T>.BinarySearch to find elements or to find the insertion point of an element if it doesn’t already exist in the list.

    From the docs:

    Return Value

    The zero-based index of
    item in the sorted List<T>,
    if item is found; otherwise, a
    negative number that is the bitwise
    complement of the index of the next
    element that is larger than item or,
    if there is no larger element, the
    bitwise complement of Count.

    Once you have the insertion point, you need to check the elements on either side to see which is closest.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a data structure that operates similar to a hash table,
I'm looking for a data structure that would allow me to store an M
In C# I have an intrusive tree structure that looks like this: public abstract
I'm looking for material on persistent data structures that can be used to implement
I'm looking for an add-in to VS2008 that can enable structural highlighting ( vertical
I'm looking for the most ideal data structure (for performance and ease of use)
I'm looking for a consistent way to structure my use of formatting strings throughout
I was looking for a tree or graph data structure in C#, but I
I'm looking to automate the backup of a Windows XP file structure (a wiki)
I am looking in the Collections framework of Java for a LIFO Structure (Stack)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.