-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cs
More file actions
43 lines (38 loc) · 1.03 KB
/
QuickSort.cs
File metadata and controls
43 lines (38 loc) · 1.03 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
using System;
namespace SortingAlgorithms
{
static partial class Program
{
static void QuickSort(int[] arr, int left, int right)
{
int l = left;
int r = right - 1;
int size = right - left;
if (size > 1)
{
Random rand = new();
int pivot = arr[rand.Next(0, size) + l];
while (l < r)
{
while (arr[r] > pivot && l <= r)
{
r--;
}
while (arr[l] < pivot && l <= r)
{
l++;
}
if (l < r)
{
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
l++;
}
}
QuickSort(arr, left, l);
QuickSort(arr, r, right);
}
}
}
}