Skip to content

Commit c509709

Browse files
Code cleanup
1 parent 2d16043 commit c509709

File tree

7 files changed

+49
-66
lines changed

7 files changed

+49
-66
lines changed

Algorithms.NET.Debug/BenchmarkDemo.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
using BenchmarkDotNet.Attributes;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
7-
using Algorithms.NET.Sorting.BubbleSort;
8-
using Algorithms.NET.Sorting.SelectionSort;
1+
using Algorithms.NET.Sorting.BubbleSort;
92
using Algorithms.NET.Sorting.InsertionSort;
103
using Algorithms.NET.Sorting.MergeSort;
114
using Algorithms.NET.Sorting.QuickSort;
5+
using Algorithms.NET.Sorting.SelectionSort;
6+
using BenchmarkDotNet.Attributes;
127

138
namespace Algorithms.NET.Debug
149
{
@@ -21,7 +16,7 @@ public class BenchmarkDemo
2116
public BenchmarkDemo()
2217
{
2318
var rand = new Random();
24-
for(int i = 0; i < 10000;i++)
19+
for (int i = 0; i < 10000; i++)
2520
{
2621
_unsorted.Add(rand.Next(1000000));
2722
_unsorted2.Add(rand.Next(1000000));
@@ -31,10 +26,10 @@ public BenchmarkDemo()
3126
[Benchmark]
3227
public void BuiltInSort() =>
3328
_unsorted2.Sort();
34-
29+
3530

3631
//[Benchmark]
37-
public void BubbleSort() =>
32+
public void BubbleSort() =>
3833
BubbleSortAlgorithm.SortAscending(_unsorted);
3934

4035
//[Benchmark]

Algorithms.NET.Debug/Program.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using Algorithms.NET.Debug;
2-
using BenchmarkDotNet.Attributes;
3-
using BenchmarkDotNet.Running;
4-
List<double> unsortedAsc = new() { 0, 1, 2, 3, 4, 5 };
1+
List<double> unsortedAsc = new() { 0, 1, 2, 3, 4, 5 };
52
List<double> unsortedDesc = new() { 5, 4, 3, 2, 1, 0 };
63
List<double> unsortedRand = new() { 8, 9, 3, 3, 10, 1 };
74

Algorithms.NET/Sorting/BubbleSort/BubbleSortAlgorithm.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32

43
namespace Algorithms.NET.Sorting.BubbleSort
54
{
@@ -48,7 +47,7 @@ private static IEnumerable<double> Sort(List<double> list, bool sortDescending)
4847
(sortedList[j - 1], sortedList[j]) = (sortedList[j], sortedList[j - 1]);
4948
isSorted = false;
5049
}
51-
else if(!sortDescending && sortedList[j - 1] > sortedList[j])
50+
else if (!sortDescending && sortedList[j - 1] > sortedList[j])
5251
{
5352
(sortedList[j - 1], sortedList[j]) = (sortedList[j], sortedList[j - 1]);
5453
isSorted = false;

Algorithms.NET/Sorting/InsertionSort/InsertionSortAlgorithm.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using System.Collections.Generic;
42

53
namespace Algorithms.NET.Sorting.InsertionSort
64
{
@@ -13,7 +11,7 @@ public class InsertionSortAlgorithm
1311
/// <returns>Sorted list in ascending order.</returns>
1412
public static List<double> SortAscending(List<double> list)
1513
{
16-
return Sort(list,false);
14+
return Sort(list, false);
1715
}
1816

1917
/// <summary>
@@ -32,29 +30,29 @@ public static List<double> SortDescending(List<double> list)
3230
/// <param name="list">List of numbers to sort</param>
3331
/// <param name="sortDescending">Boolean value specifying whether sorting should be done in descending order</param>
3432
/// <returns>A sorted list</returns>
35-
private static List<double> Sort(List<double> list,bool sortDescending)
33+
private static List<double> Sort(List<double> list, bool sortDescending)
3634
{
3735
List<double> sortedList = new List<double>(list);
3836

39-
for(int i = 1;i < list.Count;i++)
37+
for (int i = 1; i < list.Count; i++)
4038
{
4139
var current = list[i];
4240
int j = i - 1;
4341

44-
if(!sortDescending)
45-
while(j >= 0 && sortedList[j] > current)
42+
if (!sortDescending)
43+
while (j >= 0 && sortedList[j] > current)
4644
{
4745
sortedList[j + 1] = sortedList[j];
4846
j--;
4947
}
50-
else
48+
else
5149
while (j >= 0 && sortedList[j] < current)
5250
{
5351
sortedList[j + 1] = sortedList[j];
5452
j--;
5553
}
5654

57-
sortedList[j+1] = current;
55+
sortedList[j + 1] = current;
5856
}
5957

6058
return sortedList;

Algorithms.NET/Sorting/MergeSort/MergeSortAlgorithm.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using System.Collections.Generic;
42

53
namespace Algorithms.NET.Sorting.MergeSort
64
{
@@ -14,7 +12,7 @@ public class MergeSortAlgorithm
1412
public static List<double> SortAscending(List<double> list)
1513
{
1614
List<double> sortedList = new List<double>(list);
17-
return Sort(sortedList,false);
15+
return Sort(sortedList, false);
1816
}
1917

2018
/// <summary>
@@ -34,7 +32,7 @@ public static List<double> SortDescending(List<double> list)
3432
/// <param name="list">List of numbers to sort</param>
3533
/// <param name="sortDescending">Boolean value specifying whether sorting should be done in descending order</param>
3634
/// <returns>A sorted list</returns>
37-
private static List<double> Sort(List<double> list,bool sortDescending)
35+
private static List<double> Sort(List<double> list, bool sortDescending)
3836
{
3937
//We stop recursion when size is 1, which means an array of one element is sorted.
4038
if (list.Count < 2)
@@ -47,15 +45,15 @@ private static List<double> Sort(List<double> list,bool sortDescending)
4745
List<double> right = new List<double>();
4846

4947
//Fill each half
50-
for(int i = 0; i < middle; i++)
48+
for (int i = 0; i < middle; i++)
5149
left.Add(list[i]);
5250

53-
for(int j = middle; j < list.Count; j++)
51+
for (int j = middle; j < list.Count; j++)
5452
right.Add(list[j]);
5553

5654
//Sort each half.
57-
Sort(left,sortDescending);
58-
Sort(right,sortDescending);
55+
Sort(left, sortDescending);
56+
Sort(right, sortDescending);
5957

6058
//Merge the results
6159
Merge(left, right, list, sortDescending);
@@ -70,24 +68,24 @@ private static List<double> Sort(List<double> list,bool sortDescending)
7068
/// <param name="rightHalf">A list of the right half</param>
7169
/// <param name="list">Original list in which we want to insert items correctly</param>
7270
/// <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)
71+
private static void Merge(List<double> leftHalf, List<double> rightHalf, List<double> list, bool sortDescending)
7472
{
7573
int i = 0, j = 0, k = 0;
76-
74+
7775
//We insert the first items in the correct order by comparing them to each others
78-
while(i < leftHalf.Count && j < rightHalf.Count)
76+
while (i < leftHalf.Count && j < rightHalf.Count)
7977
{
80-
if(!sortDescending && leftHalf[i] < rightHalf[j])
78+
if (!sortDescending && leftHalf[i] < rightHalf[j])
8179
list[k++] = leftHalf[i++];
82-
else
80+
else
8381
list[k++] = rightHalf[j++];
8482
}
8583

8684
//We insert the remaining items
87-
while(j < rightHalf.Count)
85+
while (j < rightHalf.Count)
8886
list[k++] = rightHalf[j++];
8987

90-
while(i < leftHalf.Count)
88+
while (i < leftHalf.Count)
9189
list[k++] = leftHalf[i++];
9290
}
9391
}

Algorithms.NET/Sorting/QuickSort/QuickSortAlgorithm.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using System.Collections.Generic;
42

53
namespace Algorithms.NET.Sorting.QuickSort
64
{
@@ -14,8 +12,8 @@ public class QuickSortAlgorithm
1412
public static List<double> SortAscending(List<double> list)
1513
{
1614
List<double> sortedList = new List<double>(list);
17-
18-
return Sort(sortedList, 0, sortedList.Count - 1,false);
15+
16+
return Sort(sortedList, 0, sortedList.Count - 1, false);
1917
}
2018

2119
/// <summary>
@@ -39,14 +37,14 @@ public static List<double> SortDescending(List<double> list)
3937
/// <param name="start">Index of first item</param>
4038
/// <param name="sortDescending">Boolean value specifying whether sorting should be done in descending order</param>
4139
/// <returns>A sorted list</returns>
42-
private static List<double> Sort(List<double> list, int start, int end,bool sortDescending)
40+
private static List<double> Sort(List<double> list, int start, int end, bool sortDescending)
4341
{
4442
var i = start;
4543
var j = end;
4644

4745
//Select middle as the pivot
48-
var pivot = list[(start+end)/2];
49-
while(i < j)
46+
var pivot = list[(start + end) / 2];
47+
while (i < j)
5048
{
5149
//Skip elements that are less than pivot
5250
while ((!sortDescending && list[i] < pivot) || (sortDescending && list[i] > pivot))
@@ -57,20 +55,20 @@ private static List<double> Sort(List<double> list, int start, int end,bool sort
5755
j--;
5856

5957
//Swap elements that are out of order
60-
if(i <= j)
58+
if (i <= j)
6159
{
62-
(list[i], list[j]) = (list[j],list[i]);
60+
(list[i], list[j]) = (list[j], list[i]);
6361
i++;
6462
j--;
6563
}
6664
}
6765

68-
if(start < j)
66+
if (start < j)
6967
//Sort the right side
70-
Sort(list,start,j,sortDescending);
71-
if(end > i)
68+
Sort(list, start, j, sortDescending);
69+
if (end > i)
7270
//Sort the left side
73-
Sort(list,i,end,sortDescending);
71+
Sort(list, i, end, sortDescending);
7472

7573
return list;
7674
}

Algorithms.NET/Sorting/SelectionSort/SelectionSortAlgorithm.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using System.Collections.Generic;
42

53
namespace Algorithms.NET.Sorting.SelectionSort
64
{
@@ -23,7 +21,7 @@ public static IEnumerable<double> SortDescending(List<double> list)
2321
/// <returns>Sorted list in ascending order.</returns>
2422
public static IEnumerable<double> SortAscending(List<double> list)
2523
{
26-
return Sort(list,false);
24+
return Sort(list, false);
2725
}
2826

2927
/// <summary>
@@ -32,15 +30,15 @@ public static IEnumerable<double> SortAscending(List<double> list)
3230
/// <param name="list">List of numbers to sort</param>
3331
/// <param name="sortDescending">Boolean value specifying whether sorting should be done in descending order</param>
3432
/// <returns>A sorted list</returns>
35-
private static IEnumerable<double> Sort(List<double> list,bool sortDescending)
33+
private static IEnumerable<double> Sort(List<double> list, bool sortDescending)
3634
{
3735
var sortedList = new List<double>(list);
38-
for(int i = 0; i < sortedList.Count; i++)
36+
for (int i = 0; i < sortedList.Count; i++)
3937
{
4038
double min = sortedList[i];
4139
double max = sortedList[i];
4240
int index = i;
43-
for(int j = i; j< list.Count; j++)
41+
for (int j = i; j < list.Count; j++)
4442
{
4543
//Finding max or min according to boolean value of sortDescending
4644
if (!sortDescending && sortedList[j] < min)
@@ -55,7 +53,7 @@ private static IEnumerable<double> Sort(List<double> list,bool sortDescending)
5553
}
5654

5755
}
58-
if(index != i)
56+
if (index != i)
5957
(sortedList[i], sortedList[index]) = (sortedList[index], sortedList[i]);
6058
}
6159

0 commit comments

Comments
 (0)