Skip to content

Commit 2d16043

Browse files
Added quickSort algorithm
1 parent a2c7050 commit 2d16043

File tree

3 files changed

+104
-20
lines changed

3 files changed

+104
-20
lines changed

Algorithms.NET.Debug/BenchmarkDemo.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,50 @@
88
using Algorithms.NET.Sorting.SelectionSort;
99
using Algorithms.NET.Sorting.InsertionSort;
1010
using Algorithms.NET.Sorting.MergeSort;
11+
using Algorithms.NET.Sorting.QuickSort;
1112

1213
namespace Algorithms.NET.Debug
1314
{
1415
[MemoryDiagnoser]
1516
public class BenchmarkDemo
1617
{
17-
private List<double> _unsortedDesc = new();
18-
private List<double> _unsortedDesc2 = new();
18+
private List<double> _unsorted = new();
19+
private List<double> _unsorted2 = new();
1920

2021
public BenchmarkDemo()
2122
{
2223
var rand = new Random();
2324
for(int i = 0; i < 10000;i++)
2425
{
25-
_unsortedDesc.Add(rand.Next(1000000));
26-
_unsortedDesc2.Add(rand.Next(1000000));
26+
_unsorted.Add(rand.Next(1000000));
27+
_unsorted2.Add(rand.Next(1000000));
2728
}
2829
}
2930

3031
[Benchmark]
3132
public void BuiltInSort() =>
32-
_unsortedDesc.Sort();
33+
_unsorted2.Sort();
3334

3435

35-
[Benchmark]
36+
//[Benchmark]
3637
public void BubbleSort() =>
37-
BubbleSortAlgorithm.SortAscending(_unsortedDesc);
38+
BubbleSortAlgorithm.SortAscending(_unsorted);
3839

39-
[Benchmark]
40+
//[Benchmark]
4041
public void SelectionSort() =>
41-
SelectionSortAlgorithm.SortAscending(_unsortedDesc);
42+
SelectionSortAlgorithm.SortAscending(_unsorted);
4243

43-
[Benchmark]
44+
//[Benchmark]
4445
public void InsertionSort() =>
45-
InsertionSortAlgorithm.SortAscending(_unsortedDesc);
46+
InsertionSortAlgorithm.SortAscending(_unsorted);
4647

4748
[Benchmark]
4849
public void MergeSort() =>
49-
MergeSortAlgorithm.SortAscending(_unsortedDesc2);
50+
MergeSortAlgorithm.SortAscending(_unsorted);
51+
52+
[Benchmark]
53+
public void QuickSort() =>
54+
QuickSortAlgorithm.SortAscending(_unsorted);
5055

5156
}
5257
}

Algorithms.NET.Debug/Program.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
using Algorithms.NET.Debug;
22
using BenchmarkDotNet.Attributes;
33
using BenchmarkDotNet.Running;
4-
//List<double> unsortedAsc = new() { 0, 1, 2, 3, 4, 5 };
5-
//List<double> unsortedDesc = new() { 5, 4, 3, 2, 1, 0 };
4+
List<double> unsortedAsc = new() { 0, 1, 2, 3, 4, 5 };
5+
List<double> unsortedDesc = new() { 5, 4, 3, 2, 1, 0 };
6+
List<double> unsortedRand = new() { 8, 9, 3, 3, 10, 1 };
67

78
//var sortedDesc = Algorithms.NET.Sorting.MergeSort.MergeSortAlgorithm.SortDescending(unsortedAsc);
8-
//var sortedAsc = Algorithms.NET.Sorting.MergeSort.MergeSortAlgorithm.SortAscending(unsortedDesc);
9+
var sortedAsc = Algorithms.NET.Sorting.QuickSort.QuickSortAlgorithm.SortDescending(unsortedRand);
910

1011

1112
//foreach (var item in sortedDesc)
1213
//{
1314
// Console.WriteLine(item);
1415
//}
1516
//Console.WriteLine();
16-
//foreach (var item in sortedAsc)
17-
//{
18-
// Console.WriteLine(item);
19-
//}
17+
foreach (var item in sortedAsc)
18+
{
19+
Console.WriteLine(item);
20+
}
2021

2122
//List<double> _unsortedAsc = new();
2223
//var rand = new Random();
@@ -25,7 +26,7 @@
2526
// _unsortedAsc.Add(rand.Next(1000000));
2627
//}
2728

28-
var summary = BenchmarkRunner.Run<BenchmarkDemo>();
29+
//var summary = BenchmarkRunner.Run<BenchmarkDemo>();
2930

3031

3132

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Algorithms.NET.Sorting.QuickSort
6+
{
7+
public class QuickSortAlgorithm
8+
{
9+
/// <summary>
10+
/// Sorting a list of numbers in ascending order using QuickSort algorithm, Time complexity of mostly O(n Log n) in case of shuffled list and O(n2) in worst case or sorted list.
11+
/// </summary>
12+
/// <param name="list">List of numbers to sort</param>
13+
/// <returns>Sorted list in ascending order.</returns>
14+
public static List<double> SortAscending(List<double> list)
15+
{
16+
List<double> sortedList = new List<double>(list);
17+
18+
return Sort(sortedList, 0, sortedList.Count - 1,false);
19+
}
20+
21+
/// <summary>
22+
/// Sorting a list of numbers in Descending order using QuickSort algorithm, Time complexity of mostly O(n Log n) in case of shuffled list and O(n2) in worst case or sorted list.
23+
/// </summary>
24+
/// <param name="list">List of numbers to sort</param>
25+
/// <returns>Sorted list in ascending order.</returns>
26+
public static List<double> SortDescending(List<double> list)
27+
{
28+
List<double> sortedList = new List<double>(list);
29+
30+
return Sort(sortedList, 0, sortedList.Count - 1, true);
31+
}
32+
33+
34+
/// <summary>
35+
/// Sorting a list of numbers in Descending order using QuickSort algorithm, Time complexity of mostly O(n Log n) in case of shuffled list and O(n2) in worst case or sorted list.
36+
/// </summary>
37+
/// <param name="list">List of numbers to sort</param>
38+
/// <param name="end">Index of last item</param>
39+
/// <param name="start">Index of first item</param>
40+
/// <param name="sortDescending">Boolean value specifying whether sorting should be done in descending order</param>
41+
/// <returns>A sorted list</returns>
42+
private static List<double> Sort(List<double> list, int start, int end,bool sortDescending)
43+
{
44+
var i = start;
45+
var j = end;
46+
47+
//Select middle as the pivot
48+
var pivot = list[(start+end)/2];
49+
while(i < j)
50+
{
51+
//Skip elements that are less than pivot
52+
while ((!sortDescending && list[i] < pivot) || (sortDescending && list[i] > pivot))
53+
i++;
54+
55+
//Skip elements that are greater than pivot
56+
while ((!sortDescending && list[j] > pivot) || (sortDescending && list[j] < pivot))
57+
j--;
58+
59+
//Swap elements that are out of order
60+
if(i <= j)
61+
{
62+
(list[i], list[j]) = (list[j],list[i]);
63+
i++;
64+
j--;
65+
}
66+
}
67+
68+
if(start < j)
69+
//Sort the right side
70+
Sort(list,start,j,sortDescending);
71+
if(end > i)
72+
//Sort the left side
73+
Sort(list,i,end,sortDescending);
74+
75+
return list;
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)