-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomizedQuickSort.cs
69 lines (59 loc) · 2.39 KB
/
RandomizedQuickSort.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
namespace Vector
{
public class RandomizedQuickSort : ISorter
{
public void Sort<K>(K[] array, int index, int num, IComparer<K> comparer) where K : IComparable<K>
{
// Verify parsed values
if (array == null) throw new ArgumentNullException("Array Cannot Be Null");
if (index < 0 || num < 0) throw new ArgumentOutOfRangeException("index or num was out of range");
if (index > array.Length || num + index > array.Length) throw new ArgumentException("One or more values is outside the array");
// Randomized Quick Sort
QuickSort(array, index, num - 1, comparer);
}
private void QuickSort<K>(K[] array, int lo, int hi, IComparer<K> comparer) where K : IComparable<K>
{
// Terminate sort branch once complete
if (lo >= hi || lo < 0) return;
// create a new partition with a random pivot
int p = rand_partition(array, lo, hi, comparer);
// Sort partion and sort
QuickSort(array, lo, p-1, comparer);
QuickSort(array, p+1, hi, comparer);
}
private int rand_partition<K>(K[] array, int lo, int hi, IComparer<K> comparer) where K : IComparable<K>
{
// Select a random pivot
var random = new Random();
int p = random.Next(lo, hi);
// swap p and hi
K temp = array[p];
array[p] = array[hi];
array[hi] = temp;
return partition(array, lo, hi, comparer);
}
private int partition<K>(K[] array, int lo, int hi, IComparer<K> comparer) where K : IComparable<K>
{
K temp; // variable for swaps
K pivot = array[hi];
int i = lo;
for (int j = lo; j < hi; j++)
{
if (comparer.Compare(array[j],pivot) <= 0)
{
// Shift i up if j is less than or equal to pivot
temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
}
}
// Swap i with hi
temp = array[i];
array[i] = array[hi];
array[hi] = temp;
// return new pivot
return i;
}
}
}