Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Selection sorting algorithm that sorts the internal Array.
- /// </summary>
- private void SelectionSort(Barrier barrier, Barrier errorMaker, SemaphoreSlim semaphore)
- {
- barrier.SignalAndWait();
- // degenerate cases
- if (_Array.Length <= 1) return;
- // index of leftmost unsorted element - first in the unsorted sublist
- for (int LMU = 0; LMU < _Array.Length - 1; LMU++)
- {
- // Critical section start
- if (CriticalSectionsEnabled) semaphore.Wait();
- // find the largest element in the unsorted sublist
- int min_i = LMU;
- for (int i = LMU + 1; i < _Array.Length; i++)
- {
- if (_Array[min_i] > _Array[i])
- {
- min_i = i;
- }
- }
- // if we need to see some sorting errors
- if (!CriticalSectionsEnabled)
- {
- // let the bubble sort cut in
- errorMaker.SignalAndWait();
- // *bubble sort does it's critical section*
- // do our own thing now
- errorMaker.SignalAndWait();
- }
- // insert the largest element into the sorted sublist
- var tmp = _Array[LMU];
- _Array[LMU] = _Array[min_i];
- _Array[min_i] = tmp;
- // Critical section end
- if (CriticalSectionsEnabled) semaphore.Release();
- }
- errorMaker.RemoveParticipant();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement