-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.cs
79 lines (70 loc) · 1.87 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
using System;
namespace Variables
{
class Program{
static void Main(string[] args)
{
int[] arr = { 67, 12, 95, 56, 85, 1, 100, 23, 60, 9 };
int n = 10, i;
Console.WriteLine("Quick Sort");
Console.Write("Initial array is: ");
for (i = 0; i < n; i++)
{
Console.Write(arr[i] + " ");
}
Solution solution = new Solution();
solution.quickSort(arr, 0, 9);
Console.Write("\nSorted Array is: ");
for (i = 0; i < n; i++)
{
Console.Write(arr[i] + " ");
}
}
}
public class Solution
{
static public int Partition(int[] arr, int left, int right)
{
int pivot;
pivot = arr[left];
while (true)
{
while (arr[left] < pivot)
{
left++;
}
while (arr[right] > pivot)
{
right--;
}
if (left < right)
{
int temp = arr[right];
arr[right] = arr[left];
arr[left] = temp;
}
else
{
return right;
}
}
}
public Array quickSort(int[] arr, int left, int right)
{
int pivot;
if (left < right)
{
pivot = Partition(arr, left, right);
if (pivot > 1)
{
quickSort(arr, left, pivot - 1);
}
if (pivot + 1 < right)
{
quickSort(arr, pivot + 1, right);
}
}
return arr;
}
}
}