?
Solved

Most efficient Structures for sorting

Posted on 2006-06-08
3
Medium Priority
?
278 Views
Last Modified: 2010-04-16
What is the most efficient structure in C# for sorting between various types

For example I have

Strings
Integers
Booleans

As opposed to having them all scattered I want to sort my structure so that I have all strings then integers then booleans.

I was thinking a list would be the simplest solution.
0
Comment
Question by:directxBOB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Expert Comment

by:t_itanium
ID: 16867794
try to use the GetType property ...and sort according to it..

cheers
0
 
LVL 11

Accepted Solution

by:
anyoneis earned 1000 total points
ID: 16867816
Do they have to go into the same structure? Why not have three separate structures, one for each type? The reason this comes to mind is that if you store all of these together, you are going to have to store objects, and that means you will be boxing and unboxing integers and booleans.

Of course, if they are in the same structure already, that won't be any additional overhead. And the sorting algorithm shouldn't have to box/unbox, so maybe that's not a problem.

What about a simple array of objects?

David
0
 
LVL 9

Assisted Solution

by:pallosp
pallosp earned 1000 total points
ID: 16868236
If you store them in an ArrayList, the time complexity of the sorting is
   - 2*n*log2(n) GetType calls
   - n*log2(n) type comparisons
   - n*log2(n) value comparisons
   - 2*n*log2(n) boxings
If you use 3 generic lists for each type
   - n GetType calls and n boxings for distributing the elements into 3 lists
   - n/3 * log2(n/3) value comparisons for each list
   - n boxings for putting all together in 1 list

I think sorting using generic lists is 2-3 times faster.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question