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

  • SEARCH
  • Home
  • 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 3309146
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:36:22+00:00 2026-05-17T21:36:22+00:00

Any multi-core sorting implementation in .NET?

  • 0

Any multi-core sorting implementation in .NET?

  • 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-17T21:36:22+00:00Added an answer on May 17, 2026 at 9:36 pm

    Here’s a multi-threaded QuickSort I put together a while back using async/await. Under a certain sort size, it “reverts” back to an elementary sort known as Double-Ended Selection Sort:

    public static class SortExtensions
    {
        /// <summary>
        /// Sorts the list.
        /// </summary>
        /// <typeparam name="T">
        /// The IComparable element type of the list.
        /// </typeparam>
        /// <param name="list">The list to sort.</param>
        public static async Task SortIt<T>(this IList<T> list) where T : IComparable<T> =>
            await SortIt(list, 0, list.Count - 1);
    
        /// <summary>
        /// Sorts the list.
        /// </summary>
        /// <typeparam name="T">The element type of the list.</typeparam>
        /// <param name="list">The list to sort.</param>
        /// <param name="lowerbound">The lower bound.</param>
        /// <param name="upperbound">The upper bound.</param>
        private static async Task SortIt<T>(
            this IList<T> list,
            int lowerbound,
            int upperbound) where T : IComparable<T>
        {
            // If list is non-existent or has zero or one element, no need to sort, so exit.
            if ((list == null) || (upperbound - lowerbound < 1))
            {
                return;
            }
    
            const int MinListLength = 6;
    
            if (upperbound - lowerbound > MinListLength)
            {
                await list.QuickSort(lowerbound, upperbound);
                return;
            }
    
            await list.DoubleEndedSelectionSort(lowerbound, upperbound);
        }
    
        /// <summary>
        /// QuickSorts the list.
        /// </summary>
        /// <typeparam name="T">The element type of the list.</typeparam>
        /// <param name="list">The list to sort.</param>
        /// <param name="lowerbound">The lower bound.</param>
        /// <param name="upperbound">The upper bound.</param>
        private static async Task QuickSort<T>(
            this IList<T> list,
            int lowerbound,
            int upperbound) where T : IComparable<T>
        {
            int left = lowerbound;
            int right = upperbound;
            int middle = (lowerbound + upperbound) >> 1;
            int pivotindex = (list[lowerbound].CompareTo(list[middle]) <= 0)
                && (list[middle].CompareTo(list[upperbound]) <= 0)
                ? middle
                : ((list[middle].CompareTo(list[lowerbound]) <= 0)
                    && (list[lowerbound].CompareTo(list[upperbound]) <= 0)
                    ? lowerbound
                    : upperbound);
            T pivotvalue = list[pivotindex];
    
            do
            {
                while ((left < upperbound) && (list[left].CompareTo(pivotvalue) < 0))
                {
                    left++;
                }
    
                while ((right > lowerbound) && (list[right].CompareTo(pivotvalue) > 0))
                {
                    right--;
                }
    
                if (left > right)
                {
                    continue;
                }
    
                (list[left], list[right]) = (list[right], list[left]);
                left++;
                right--;
            }
            while (right > left);
    
            if (lowerbound < right)
            {
                await list.SortIt(lowerbound, right);
            }
    
            if (left < upperbound)
            {
                await list.SortIt(left, upperbound);
            }
        }
    
        /// <summary>
        /// Double-ended selection sorts the list.
        /// </summary>
        /// <typeparam name="T">The element type of the list.</typeparam>
        /// <param name="list">The list to sort.</param>
        /// <param name="lowerbound">The lower bound.</param>
        /// <param name="upperbound">The upper bound.</param>
        private static async Task DoubleEndedSelectionSort<T>(
            this IList<T> list,
            int lowerbound,
            int upperbound) where T : IComparable<T>
        {
            // Initialize some working variables that will hold the sortable sublist indices.
            int left = lowerbound;
            int right = upperbound;
    
            // Keep sorting the list while the upper bound of the sublist is greater than the lower bound.
            while (right > left)
            {
                // Find get the indices of the smallest and largest elements of the sublist.
                (int smallest, int largest) = await list.FindSmallestAndLargest(left, right);
    
                if (smallest != largest)
                {
                    // If they're different elements, swap the smallest with left element of the sublist.
                    (list[left], list[smallest]) = (list[smallest], list[left]);
                    if (largest == left)
                    {
                        // If the largest element of the sublist was the left element, then now, it has to be the
                        // smallest due to the swap.
                        largest = smallest;
                    }
                }
    
                if (largest != right)
                {
                    // If the largest element of the sublist is the rightmost element, then they need to be swapped.
                    (list[right], list[largest]) = (list[largest], list[right]);
                }
    
                // Move the sublist search indices one in from the left and the right.
                left++;
                right--;
            }
        }
    
        /// <summary>
        /// Finds the smallest and largest list element indices.
        /// </summary>
        /// <typeparam name="T">The element type of the list.</typeparam>
        /// <param name="list">The list to search.</param>
        /// <param name="lowerbound">The lower bound.</param>
        /// <param name="upperbound">The upper bound.</param>
        private static async Task<(int, int)> FindSmallestAndLargest<T>(
            this IList<T> list,
            int lowerbound,
            int upperbound) where T : IComparable<T>
        {
            int smallest = (list[lowerbound].CompareTo(list[upperbound]) < 0) ? lowerbound : upperbound;
            int largest = (list[lowerbound].CompareTo(list[upperbound]) < 0) ? upperbound : lowerbound;
    
            lowerbound++;
    
            int loop = upperbound;
    
            while (loop > lowerbound)
            {
                loop--;
                if (list[loop].CompareTo(list[smallest]) < 0)
                {
                    smallest = loop;
                }
    
                if (list[loop].CompareTo(list[largest]) > 0)
                {
                    largest = loop;
                }
            }
    
            return (smallest, largest);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is memcached capable of making full use of multi-core? Or is there any way
Does anyone know if there are any multi-line row grid controls for sale or
Has anyone had any experience scaling out SQL Server in a multi reader single
I'm wondering if anyone has any experience using log4net in a multi-threaded environment like
As I am using for-loops on large multi-dim arrays, any saving on the for-loop
Any ideas on how to implement tab completion for a .NET (C#) Console Application?
As multi-core processors become more and more popular, I think it would be wise
Is it possible to run a particular thread/process on any core we want? Since
I am totally a novice in Multi-Core Programming, but I do know how to
I have some questions about multi-threaded programming and multi-core usage. In particular I'm wondering

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.