|
Home
Download
Screenshots
Adding your sorts
Algorithms
-Bubble Sort
-Comb Sort
-Gnome Sort
-Heap Sort
-Insertion Sort
-Merge Sort
-Odd/Even Sort
-Quick Sort
-Quick Sort with Bubble Sort
-Radix Sort
-Rob (Random) Sort
-Selection Sort
-Shaker Sort
-Shear Sort
-Shell Sort
Donation:
|
Odd/Even Sort
Description:
The Odd/Even sort is a "parallel sorting algorithm," so it is made to be
used on multiple processors. This uses the concept of the Bubble
Sort to move elements around, and sorts only the odds and then the evens
(indexes). The code below is not the multiprocessor code.
Instead, it is a threaded simulation of the algorithm.
Efficiency: O(N) (On N processors)
Source:
public class OddEvenSort: SortMethod
{
private int[] list;
private ReaderWriterLock l=new ReaderWriterLock();
public override void sort(int[] list)
{
this.list=list;
for(int i=0;i<list.Length/2;i++)
run();
}
private int[] List // the Number property
{
get
{
//Acquire a read lock on the
resource.
l.AcquireReaderLock(Timeout.Infinite);
return list;
}
}
private void run()
{
for(int x=0;x+1<this.list.Length;x+=2)
{
int[] list=List;
if(list[x]>list[x+1])
swap(list,x,x+1);
NumComparisons++;
NumIterations++;
l.ReleaseLock();
}
pause();pause();
for(int x=1;x+1<this.list.Length;x+=2)
{
int[] list=List;
if(list[x]>list[x+1])
swap(list,x,x+1);
NumComparisons++;
NumIterations++;
l.ReleaseLock();
}
pause();pause();
}
public override string ToString()
{
return "Odd/Even Sort";
}
} |
|