Skip to content

Commit aaf63bb

Browse files
authored
Merge pull request deutranium#197 from gabrielhs1357/quickSort
implemented quick sort algo using c#
2 parents f9a2893 + f9f06e3 commit aaf63bb

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

sortingAlgo/quickSort/quickSort.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class QuickSort
2+
{
3+
static int Partition(int[] array, int low,
4+
int high)
5+
{
6+
// Select a pivot point.
7+
int pivot = array[high];
8+
9+
int lowIndex = (low - 1);
10+
11+
// Reorder the collection.
12+
for (int j = low; j < high; j++)
13+
{
14+
if (array[j] <= pivot)
15+
{
16+
lowIndex++;
17+
18+
int temp = array[lowIndex];
19+
array[lowIndex] = array[j];
20+
array[j] = temp;
21+
}
22+
}
23+
24+
int temp1 = array[lowIndex + 1];
25+
array[lowIndex + 1] = array[high];
26+
array[high] = temp1;
27+
28+
return lowIndex + 1;
29+
}
30+
31+
static void Sort(int[] array, int low, int high)
32+
{
33+
if (low < high)
34+
{
35+
int partitionIndex = Partition(array, low, high);
36+
37+
// Recursively continue sorting the array
38+
Sort(array, low, partitionIndex - 1);
39+
Sort(array, partitionIndex + 1, high);
40+
}
41+
}
42+
43+
public static void Main()
44+
{
45+
int[] array = { 72, 12, 6, 33, 81, 97, 37, 59, 52, 1, 20 };
46+
int length = array.Length;
47+
48+
Console.WriteLine("QuickSort");
49+
CommonFunctions.PrintInitial(array);
50+
Sort(array, 0, length - 1);
51+
52+
CommonFunctions.PrintFinal(array);
53+
54+
Console.ReadKey();
55+
}
56+
}

0 commit comments

Comments
 (0)