Solved

Most efficient Structures for sorting

Posted on 2006-06-08
3
273 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
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 250 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 250 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# HTTP GET method sample code 3 57
MediaHelp 4 21
Call windows 10 virtual keyboard from windows forms app 2 34
FTP file download using c# 3 22
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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