Last active
December 20, 2015 16:29
-
-
Save irsal/6161695 to your computer and use it in GitHub Desktop.
Quick Sort Recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Pseudo-randomization of the partition | |
/// </summary> | |
public int Partition(Particle[] a, int index, int start, int end) | |
{ | |
float pValue = a[index].depth; | |
Particle temp1 = a[index]; | |
a[index] = a[end]; | |
a[end] = temp1; | |
int nextLeft = start; | |
for (int i = start; i < end; i++) | |
{ | |
if (a[i].depth <= pValue) | |
{ | |
Particle temp2 = a[i]; | |
a[i] = a[nextLeft]; | |
a[nextLeft] = temp2; | |
nextLeft++; | |
} | |
} | |
Particle temp3 = a[nextLeft]; | |
a[nextLeft] = a[end]; | |
a[end] = temp3; | |
return nextLeft; | |
} | |
public void QuickSortRecursion(Particle[] a, int start, int end) | |
{ | |
Random random = new Random(); | |
if (end > start) | |
{ | |
int pIndex = random.Next(0, a.Length); | |
int newIndex = Partition(a, pIndex, start, end); | |
QuickSortRecursion(a, start, newIndex - 1); | |
QuickSortRecursion(a, newIndex + 1, end); | |
} | |
} | |
/// <summary> | |
/// Call for QuickSort Depth-first | |
/// </summary> | |
public void QuicksortDepthSort() | |
{ | |
QuickSortRecursion(particles, 0, particles.Length - 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment