Question

When I run bubblesorts, cocktailsorts and quicksorts in C#, I can see that all 3 cores are engaged on my AMD X3 (X4 shipped with 1 broken core).

Why is this happening? My algorithm is sequential and my code doesn't have any thread tags. Especially something like sorting algorithms which is a highly sequential algorithm no, one event can't happen until the next is completed. How does it manage to split up the algorithm?

The bubblesort for example on request:

 public void BubbleSort()
    {
        for (int i = 1; i < amount; i++)
        {

            for (int j = 0; j < a; j++)
            {
                if (numbers[j] > numbers[j + 1])
                {
                    t = numbers[j + 1];
                    numbers[j + 1] = numbers[j];
                    numbers[j] = t;
                }
             }
          a--;
        }

    }
Was it helpful?

Solution

Your code can potentially swap cores on a context switch. But will only use one at a time.

OTHER TIPS

Sorting algorithms can be made to run in parallel and utilize multiple cores. Which sort routines are you using? It's very possible that they are not sequential algorithms.

As an example, Quicksort is very easy to parallelize via divide and conquer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top