-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.cs
executable file
·179 lines (159 loc) · 5.15 KB
/
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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace SortsBenchmark
{
public static partial class Sorts
{
// recursive
public static List<int> QuickSort(List<int> numbers, int switchlimit = 10)
{
if (numbers.Count <= switchlimit)
{
//return Sorts.BubbleSort(numbers);
return Sorts.InsertionSort(numbers);
}
// choice of pivotIndex
int tmpMidIndex = numbers.Count / 2;
int tmpFirstIndex = 1;
int tmpLastIndex = numbers.Count - 1;
int pivot;
int a = numbers[tmpMidIndex];
int b = numbers[tmpFirstIndex];
int c = numbers[tmpLastIndex];
pivot = getMedian(a, b, c);
List<int> M = new List<int>();
List<int> P = new List<int>();
List<int> V = new List<int>();
List<int> ret = new List<int>();
for (int i = 0; i < numbers.Count; i++)
{
if (numbers[i] < pivot)
{
M.Add(numbers[i]);
}
else if (numbers[i] == pivot)
{
P.Add(numbers[i]);
}
else if (numbers[i] >= pivot)
{
V.Add(numbers[i]);
}
}
M = QuickSort(M, switchlimit);
V = QuickSort(V, switchlimit);
ret.AddRange(M);
ret.AddRange(P);
ret.AddRange(V);
return ret;
}
private static int getMedian(int a, int b, int c)
{
int pivot;
int small, large;
if (a >= b)
{
large = a;
small = b;
}
else
{
large = b;
small = a;
}
if (c >= large)
{
pivot = large;
}
else
{
if (c < small)
{
pivot = small;
}
else
{
pivot = c;
}
}
return pivot;
}
//in-array
public static List<int> IterativeQuickSort(List<int> numbers, int switchlimit = 10)
{
Stack<int> stack = new Stack<int>();
stack.Push(0);
stack.Push(numbers.Count-1);
while (stack.Count > 0)
{
int right = stack.Pop();
int left = stack.Pop();
if (left < right)
{
if (right-left < switchlimit)
{
InsertionSort(numbers, left, right+1);
continue;
}
// choice of pivotIndex
int tmpMidIndex = (left+right)/2;
int tmpFirstIndex = left;
int tmpLastIndex = right;
int pivot;
int a = numbers[tmpMidIndex];
int b = numbers[tmpFirstIndex];
int c = numbers[tmpLastIndex];
pivot = getMedian(a, b, c);
int pivotIndex;
if (pivot == a)
{
pivotIndex = tmpMidIndex;
}
else if (pivot==b)
{
pivotIndex = tmpFirstIndex;
} else
{
pivotIndex = tmpLastIndex;
}
int pivotNewIndex = partition(numbers, left, right, pivotIndex);
stack.Push(left);
stack.Push(pivotNewIndex - 1);
stack.Push(pivotNewIndex + 1);
stack.Push(right);
}
}
return numbers;
}
private static int partition(List<int> arr, int left, int right, int pivotIndex)
{
int pivotVal = arr[pivotIndex];
//inlined :-)
//Swap(arr, pivotIndex, right);
int tmp = arr[pivotIndex];
arr[pivotIndex] = arr[right];
arr[right] = tmp;
int storeIndex = left;
for (int i = left; i < right; i++)
{
if (arr[i] < pivotVal)
{
//inlined
//Swap(arr, i, storeIndex++);
int tmp2 = arr[i];
arr[i] = arr[storeIndex];
arr[storeIndex] = tmp2;
storeIndex++;
}
}
//Swap(arr, storeIndex, right);
int tmp3 = arr[storeIndex];
arr[storeIndex] = arr[right];
arr[right] = tmp3;
return storeIndex;
}
}
}