0

I know that Array.Sort() in VB.NET uses the quicksort algorithm. But my question is, does it take advantage of multithreading?

I'm sorting a list of hundreds of thousands of records, and need to ensure the fastest sort times.

Thanks.

6
  • Looking at the code in mscorlib, is doesn't appear to. Have you tried just running it? Does it meet your current performance goals? Commented Jul 14, 2011 at 18:22
  • This is in production code, and it takes about 4-5 seconds to sort a list of 100k rows. It's ultimately faster for me to do a requery to the database and apply the sorting in the SQL query. I don't want to do this do, so I'm not putting an extra load on our database server just to sort the results. Commented Jul 14, 2011 at 18:25
  • What type of data are you sorting? If it's based on integer keys, then you might have some success with using a radix sort. You also might give the appearance of improved performance with an intelligent caching layer. Without knowing more specifics, it's hard to give much more advice. Commented Jul 14, 2011 at 18:28
  • I'm sorting my own object, which I call "Document". I contains around 20 or so properties, and is displayed in a grid. When a user clicks on a column, it basically calls the .Sort() method of a collection. I do have a class that implements IComparer to do the comparison, and there's nothing that can be done to improve its performance. Commented Jul 14, 2011 at 18:30
  • In that case, I can't think of a lot that can be done. Sorting 100K+ records is going to be a time-consuming operation, especially with non-trivial comparands. Commented Jul 14, 2011 at 18:32

1 Answer 1

1

I'm not sure how multi-threading would make your sorting faster.
Array.Sort does sorting in a single thread.

If by multi-threading you actually mean taking advantage of several processors when they are available, check out this answer that uses Parallel Extensions (available in .NET 4.0 and partly available for .NET 3.5).

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, this may be what I need. I'm surprised that Microsoft that does not have an impementation of this? I've been adjusting to using Parallel.ForEach() in .NET 4.0.
Well, I'm not sure if it's going to give you a real performance boost though. I would rather try to optimize the IComparer using a profiler.
Also, don't hesitate posting comparer code in another question—people might notice something you might've missed, e.g. expensive boxing operations.
I guess my thought was if we have 4 processors, divide up the list into four parts, and have the 4 processors each work on sorting those 4 parts. It may be just as expensive to combine these back together. Does anyone have any hard numbers on a proven sorting algorithm that takes advantage of multiple cores, or is there not a solution?
@user842818, check out Divide & Conquer en.wikipedia.org/wiki/Divide_and_conquer_algorithm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.