-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort_Partition3Way.cs
73 lines (63 loc) · 1.6 KB
/
QuickSort_Partition3Way.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
70
71
72
73
using System;
namespace QuickSort_3Way_Partition
{
class Program
{
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
int[] arr = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), temp => Convert.ToInt32(temp));
QuickSort(ref arr, 0, n-1);
for(int i=0;i<n;i++)
{
Console.Write(arr[i] + " ");
}
}
static void QuickSort(ref int[] arr, int low, int high)
{
if(low < high)
{
Tuple<int, int> pivot = Partition_3Way(ref arr, low, high);
QuickSort(ref arr, low, pivot.Item1);
QuickSort(ref arr, pivot.Item2, high);
}
}
static Tuple<int, int> Partition_3Way(ref int[] arr, int low, int high)
{
int pivot = new Random().Next(low, high);
int pivotElement = arr[pivot];
swap(ref arr, high, pivot);
pivot = high;
int curIndex = low - 1;
for(int i = low;i<=pivot-1;i++)
{
if(arr[i] == pivotElement)
{
pivot = pivot - 1;
swap(ref arr, i, pivot);
}
if(arr[i] < pivotElement)
{
curIndex ++;
swap(ref arr, i, curIndex);
}
}
int m1 = curIndex;
while(pivot<=high)
{
curIndex = curIndex + 1;
swap(ref arr, curIndex, pivot);
pivot = pivot + 1;
}
int m2 = curIndex+1;
Tuple<int, int> tuple = new Tuple<int, int>(m1, m2);
return tuple;
}
static void swap(ref int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}