38 lines
1.6 KiB
C#
38 lines
1.6 KiB
C#
|
namespace SortingVisualization.Algorithms {
|
|||
|
public class Radixsort : SortingAlgorithm {
|
|||
|
public override string GetAlgorithmName() => "Radixsort";
|
|||
|
|
|||
|
public override void Sort(ref DataSet set) {
|
|||
|
Simulate(ref set);
|
|||
|
System.Console.WriteLine("Doing {0}...", this.GetAlgorithmName());
|
|||
|
int n; // Fachnummer
|
|||
|
int[] nPart = new int[2]; // Anzahl der Elemente in den beiden Faechern
|
|||
|
int[,] part = new int[2, set.Size]; // die beiden Faecher haben die Groesse des Arrays a
|
|||
|
|
|||
|
// Schleife ueber alle Bits der Schluessel (bei int: 32 Bit)
|
|||
|
for (int i = 0; i < 32; i++) {
|
|||
|
nPart[0] = 0;
|
|||
|
nPart[1] = 0;
|
|||
|
|
|||
|
// Partitionierungsphase: teilt "a" auf die Faecher auf
|
|||
|
for (int j = 0; j < set.Size; j++) {
|
|||
|
int tmp = set.ReadOperation(j);
|
|||
|
n = (tmp >> i) & 1; // ermittelt die Fachnummer: 0 oder 1
|
|||
|
part[n, nPart[n]++] = tmp; // kopiert j-tes Element ins richtige Fach
|
|||
|
}
|
|||
|
|
|||
|
// Sammelphase: kopiert die beiden Faecher wieder nach "a" zusammen
|
|||
|
for (int k = 0; k < nPart[0]; k++) {
|
|||
|
set.WriteOperation(k, part[0, k]);
|
|||
|
}
|
|||
|
for (int k = nPart[0]; k < set.Size; k++) {
|
|||
|
set.WriteOperation(k, part[1, k]);
|
|||
|
}
|
|||
|
}
|
|||
|
System.Console.WriteLine("{0} complete!", this.GetAlgorithmName());
|
|||
|
if (!set.SimulateMode)
|
|||
|
set.FinalizeVideo();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|