Skip to content

Commit 1753837

Browse files
added MergeSort algorithm
1 parent 0868b5a commit 1753837

File tree

6 files changed

+127
-23
lines changed

6 files changed

+127
-23
lines changed

Algorithms.NET.Debug/BenchmarkDemo.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@
77
using Algorithms.NET.Sorting.BubbleSort;
88
using Algorithms.NET.Sorting.SelectionSort;
99
using Algorithms.NET.Sorting.InsertionSort;
10+
using Algorithms.NET.Sorting.MergeSort;
1011

1112
namespace Algorithms.NET.Debug
1213
{
1314
[MemoryDiagnoser]
1415
public class BenchmarkDemo
1516
{
16-
private readonly List<double> _unsortedAsc = new() { 0D, 1D, 2D, 3D, 4D, 5D,6D,7D,8D,9D,10D,11D,12D,13D,14D,15D,16D,17D,18D,19D,20D };
17+
private readonly List<double> _unsortedAsc = new();
18+
19+
public BenchmarkDemo()
20+
{
21+
for(int i = 0; i < 1000;i++)
22+
{
23+
_unsortedAsc.Add((double)i);
24+
}
25+
}
1726

1827
[Benchmark]
1928
public void BubbleSort() =>
@@ -27,5 +36,9 @@ public void SelectionSort() =>
2736
public void InsertionSort() =>
2837
InsertionSortAlgorithm.SortDescending(_unsortedAsc);
2938

39+
[Benchmark]
40+
public void MergeSort() =>
41+
MergeSortAlgorithm.SortDescending(_unsortedAsc);
42+
3043
}
3144
}

Algorithms.NET.Debug/Program.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
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 };
6-
7-
var sortedDesc = Algorithms.NET.Sorting.InsertionSort.InsertionSortAlgorithm.SortDescending(unsortedAsc);
8-
var sortedAsc = Algorithms.NET.Sorting.InsertionSort.InsertionSortAlgorithm.SortAscending(unsortedDesc);
9-
10-
11-
foreach (var item in sortedDesc)
12-
{
13-
Console.WriteLine(item);
14-
}
15-
Console.WriteLine();
16-
foreach (var item in sortedAsc)
17-
{
18-
Console.WriteLine(item);
19-
}
4+
//List<double> unsortedAsc = new() { 0, 1, 2, 3, 4, 5 };
5+
//List<double> unsortedDesc = new() { 5, 4, 3, 2, 1, 0 };
6+
7+
//var sortedDesc = Algorithms.NET.Sorting.MergeSort.MergeSortAlgorithm.SortDescending(unsortedAsc);
8+
//var sortedAsc = Algorithms.NET.Sorting.MergeSort.MergeSortAlgorithm.SortAscending(unsortedDesc);
9+
10+
11+
//foreach (var item in sortedDesc)
12+
//{
13+
// Console.WriteLine(item);
14+
//}
15+
//Console.WriteLine();
16+
//foreach (var item in sortedAsc)
17+
//{
18+
// Console.WriteLine(item);
19+
//}
2020

2121

2222

Algorithms.NET/Sorting/BubbleSort/BubbleSortAlgorithm.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public static IEnumerable<double> SortAscending(List<double> list)
3434
/// <returns>A sorted list</returns>
3535
private static IEnumerable<double> Sort(List<double> list, bool sortDescending)
3636
{
37-
var sortedList = new List<double>();
38-
sortedList.AddRange(list);
37+
var sortedList = new List<double>(list);
3938
bool isSorted;
4039

4140
for (int i = 0; i < sortedList.Count; i++)

Algorithms.NET/Sorting/InsertionSort/InsertionSortAlgorithm.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public static List<double> SortDescending(List<double> list)
3434
/// <returns>A sorted list</returns>
3535
private static List<double> Sort(List<double> list,bool sortDescending)
3636
{
37-
List<double> sortedList = new List<double>();
38-
sortedList.AddRange(list);
37+
List<double> sortedList = new List<double>(list);
3938

4039
for(int i = 1;i < list.Count;i++)
4140
{
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Algorithms.NET.Sorting.MergeSort
6+
{
7+
public class MergeSortAlgorithm
8+
{
9+
/// <summary>
10+
/// Sorting a list of numbers in ascending order using MergeSort algorithm, Time complexity of O(n Log n).
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+
return Sort(sortedList,false);
18+
}
19+
20+
/// <summary>
21+
/// Sorting a list of numbers in descending order using MergeSort algorithm, Time complexity of O(n Log n).
22+
/// </summary>
23+
/// <param name="list">List of numbers to sort</param>
24+
/// <returns>Sorted list in descending order.</returns>
25+
public static List<double> SortDescending(List<double> list)
26+
{
27+
List<double> sortedList = new List<double>(list);
28+
return Sort(sortedList, true);
29+
}
30+
31+
/// <summary>
32+
/// Sorting a list of numbers using MergeSort algorithm, Time complexity of O(n Log n).
33+
/// </summary>
34+
/// <param name="list">List of numbers to sort</param>
35+
/// <param name="sortDescending">Boolean value specifying whether sorting should be done in descending order</param>
36+
/// <returns>A sorted list</returns>
37+
private static List<double> Sort(List<double> list,bool sortDescending)
38+
{
39+
//We stop recursion when size is 1, which means an array of one element is sorted.
40+
if (list.Count < 2)
41+
return list;
42+
43+
//Divide list in half.
44+
int middle = list.Count / 2;
45+
46+
List<double> left = new List<double>();
47+
List<double> right = new List<double>();
48+
49+
//Fill each half
50+
for(int i = 0; i < middle; i++)
51+
left.Add(list[i]);
52+
53+
for(int j = middle; j < list.Count; j++)
54+
right.Add(list[j]);
55+
56+
//Sort each half.
57+
Sort(left,sortDescending);
58+
Sort(right,sortDescending);
59+
60+
//Merge the results
61+
Merge(left, right, list, sortDescending);
62+
63+
return list;
64+
}
65+
66+
/// <summary>
67+
/// Merging two arrays in correct order according to the boolean value of sortDescending
68+
/// </summary>
69+
/// <param name="leftHalf">A list of the left half</param>
70+
/// <param name="rightHalf">A list of the right half</param>
71+
/// <param name="list">Original list in which we want to insert items correctly</param>
72+
/// <param name="sortDescending">Boolean value of whether sorting should be done in descending order or not</param>
73+
private static void Merge(List<double> leftHalf, List<double> rightHalf, List<double> list,bool sortDescending)
74+
{
75+
int i = 0, j = 0, k = 0;
76+
77+
//We insert the first items in the correct order by comparing them to each others
78+
while(i < leftHalf.Count && j < rightHalf.Count)
79+
{
80+
if(!sortDescending && leftHalf[i] < rightHalf[j])
81+
list[k++] = leftHalf[i++];
82+
else
83+
list[k++] = rightHalf[j++];
84+
}
85+
86+
//We insert the remaining items
87+
while(j < rightHalf.Count)
88+
list[k++] = rightHalf[j++];
89+
90+
while(i < leftHalf.Count)
91+
list[k++] = leftHalf[i++];
92+
}
93+
}
94+
}

Algorithms.NET/Sorting/SelectionSort/SelectionSortAlgorithm.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public static IEnumerable<double> SortAscending(List<double> list)
3434
/// <returns>A sorted list</returns>
3535
private static IEnumerable<double> Sort(List<double> list,bool sortDescending)
3636
{
37-
var sortedList = new List<double>();
38-
sortedList.AddRange(list);
37+
var sortedList = new List<double>(list);
3938
for(int i = 0; i < sortedList.Count; i++)
4039
{
4140
double min = sortedList[i];

0 commit comments

Comments
 (0)