-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cs
More file actions
51 lines (46 loc) · 1.24 KB
/
Copy pathQuickSort.cs
File metadata and controls
51 lines (46 loc) · 1.24 KB
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
namespace CodingProblems
{
public class QuickSort
{
private int Partition(int[] numbers, int left, int right)
{
int pivot = numbers[left];
while (true)
{
while (numbers[left] < pivot)
{
left++;
}
while (numbers[right] > pivot)
{
right--;
}
if (left < right)
{
int temp = numbers[right];
numbers[right] = numbers[left];
numbers[left] = temp;
}
else
{
return right;
}
}
}
public void QuickSortRecursive(int[] numbers, int left, int right)
{
if (left < right)
{
int pivot = Partition(numbers, left, right);
if (pivot > 1)
{
QuickSortRecursive(numbers, left, pivot - 1);
}
if (pivot + 1 < right)
{
QuickSortRecursive(numbers, pivot + 1, right);
}
}
}
}
}