forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.cs
40 lines (32 loc) · 909 Bytes
/
QuickSort.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
public class QuickSort
{
private static int partition(int[] array, int low, int high)
{
int pivot = array[high];
// index of smaller element
int i = (low - 1);
for (int j = low; j < high; j++)
{
if (array[j] < pivot)
{
i++;
(array[i], array[j]) = (array[j], array[i]);
}
}
(array[i + 1], array[high]) = (array[high], array[i + 1]);
return i + 1;
}
public static void quickSort(int[] array, int low, int high)
{
if (low < high)
{
int partitioningIndex = partition(array, low, high);
quickSort(array, low, partitioningIndex - 1);
quickSort(array, partitioningIndex + 1, high);
}
}
public static void Sort(int[] array)
{
quickSort(array, 0, array.Length - 1);
}
}