Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1.         /// <summary>
  2.         /// Selection sorting algorithm that sorts the internal Array.
  3.         /// </summary>
  4.         private void SelectionSort(Barrier barrier, Barrier errorMaker, SemaphoreSlim semaphore)
  5.         {
  6.             barrier.SignalAndWait();
  7.  
  8.             // degenerate cases
  9.             if (_Array.Length <= 1) return;
  10.  
  11.             // index of leftmost unsorted element - first in the unsorted sublist
  12.             for (int LMU = 0; LMU < _Array.Length - 1; LMU++)
  13.             {
  14.                 // Critical section start
  15.                 if (CriticalSectionsEnabled) semaphore.Wait();
  16.  
  17.                 // find the largest element in the unsorted sublist
  18.                 int min_i = LMU;
  19.                 for (int i = LMU + 1; i < _Array.Length; i++)
  20.                 {
  21.                     if (_Array[min_i] > _Array[i])
  22.                     {
  23.                         min_i = i;
  24.                     }
  25.                 }
  26.  
  27.                 // if we need to see some sorting errors
  28.                 if (!CriticalSectionsEnabled)
  29.                 {
  30.                     // let the bubble sort cut in
  31.                     errorMaker.SignalAndWait();
  32.  
  33.                     // *bubble sort does it's critical section*
  34.  
  35.                     // do our own thing now
  36.                     errorMaker.SignalAndWait();
  37.                 }
  38.  
  39.                 // insert the largest element into the sorted sublist
  40.                 var tmp = _Array[LMU];
  41.                 _Array[LMU] = _Array[min_i];
  42.                 _Array[min_i] = tmp;
  43.  
  44.                 // Critical section end
  45.                 if (CriticalSectionsEnabled) semaphore.Release();
  46.             }
  47.  
  48.             errorMaker.RemoveParticipant();
  49.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement