-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSortAlgorithm.cs
More file actions
68 lines (63 loc) · 2.2 KB
/
QuickSortAlgorithm.cs
File metadata and controls
68 lines (63 loc) · 2.2 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Diagnostics;
namespace QuickSortAlgorithm
{
class Program
{
//Generally at each step you choose an element called pivot and reorder the array into two sections:
//at the left side move all elements ≤ pivot and at the right side move all elements > pivot.
//Finally run the quicksort algorithm recursively over the left and the right sides.
static void Main(string[] args)
{
// Calculate the execution time of the algorithm
var watch = System.Diagnostics.Stopwatch.StartNew();
watch.Start();
int[] array = new int[5] { 34, 23, 56, 14, 57 };
// Print the sorted array
Console.WriteLine("Unsorted values: ");
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
QuickSort(array, 0, array.Length - 1);
Console.WriteLine("Sorted values: ");
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
watch.Stop();
Console.WriteLine("Time elapsed: {0}", watch.Elapsed);
Console.Read();
}
static void QuickSort(int[] array, int left, int right)
{
int i;
if(left < right)
{
i = Partition(array, left, right);
QuickSort(array, left, i - 1);
QuickSort(array, i + 1, right);
}
}
static int Partition(int[] array, int left, int right)
{
int temp;
int pivot = array[right];
int i = left - 1;
for(int j = left; j <= right - 1; j++)
{
if(array[j] <= pivot)
{
i++;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
temp = array[i + 1];
array[i + 1] = array[right];
array[right] = temp;
return i + 1;
}
}
}