Skip to content

Commit a354d01

Browse files
authored
Switch to file-scoped namespaces (#433)
1 parent 691d270 commit a354d01

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2022
-2065
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,71 @@
11
using System;
22
using System.Collections.Generic;
33

4-
namespace Algorithms.Sorters.Comparison
4+
namespace Algorithms.Sorters.Comparison;
5+
6+
/// <summary>
7+
/// TODO.
8+
/// </summary>
9+
/// <typeparam name="T">TODO. 2.</typeparam>
10+
public class BinaryInsertionSorter<T> : IComparisonSorter<T>
511
{
612
/// <summary>
7-
/// TODO.
13+
/// Sorts array using specified comparer,
14+
/// variant of insertion sort where binary search is used to find place for next element
15+
/// internal, in-place, unstable,
16+
/// time complexity: O(n^2),
17+
/// space complexity: O(1),
18+
/// where n - array length.
819
/// </summary>
9-
/// <typeparam name="T">TODO. 2.</typeparam>
10-
public class BinaryInsertionSorter<T> : IComparisonSorter<T>
20+
/// <param name="array">Array to sort.</param>
21+
/// <param name="comparer">Compares elements.</param>
22+
public void Sort(T[] array, IComparer<T> comparer)
1123
{
12-
/// <summary>
13-
/// Sorts array using specified comparer,
14-
/// variant of insertion sort where binary search is used to find place for next element
15-
/// internal, in-place, unstable,
16-
/// time complexity: O(n^2),
17-
/// space complexity: O(1),
18-
/// where n - array length.
19-
/// </summary>
20-
/// <param name="array">Array to sort.</param>
21-
/// <param name="comparer">Compares elements.</param>
22-
public void Sort(T[] array, IComparer<T> comparer)
24+
for (var i = 1; i < array.Length; i++)
2325
{
24-
for (var i = 1; i < array.Length; i++)
25-
{
26-
var target = array[i];
27-
var moveIndex = i - 1;
28-
var targetInsertLocation = BinarySearch(array, 0, moveIndex, target, comparer);
29-
Array.Copy(array, targetInsertLocation, array, targetInsertLocation + 1, i - targetInsertLocation);
26+
var target = array[i];
27+
var moveIndex = i - 1;
28+
var targetInsertLocation = BinarySearch(array, 0, moveIndex, target, comparer);
29+
Array.Copy(array, targetInsertLocation, array, targetInsertLocation + 1, i - targetInsertLocation);
3030

31-
array[targetInsertLocation] = target;
32-
}
31+
array[targetInsertLocation] = target;
3332
}
33+
}
3434

35-
/// <summary>Implementation of Binary Search using an iterative approach.</summary>
36-
/// <param name="array">
37-
/// An array of values sorted in ascending order between the index values left and right to search
38-
/// through.
39-
/// </param>
40-
/// <param name="from">Left index to search from (inclusive).</param>
41-
/// <param name="to">Right index to search to (inclusive).</param>
42-
/// <param name="target">The value to find placefor in the provided array.</param>
43-
/// <param name="comparer">TODO.</param>
44-
/// <returns>The index where to insert target value.</returns>
45-
private static int BinarySearch(T[] array, int from, int to, T target, IComparer<T> comparer)
35+
/// <summary>Implementation of Binary Search using an iterative approach.</summary>
36+
/// <param name="array">
37+
/// An array of values sorted in ascending order between the index values left and right to search
38+
/// through.
39+
/// </param>
40+
/// <param name="from">Left index to search from (inclusive).</param>
41+
/// <param name="to">Right index to search to (inclusive).</param>
42+
/// <param name="target">The value to find placefor in the provided array.</param>
43+
/// <param name="comparer">TODO.</param>
44+
/// <returns>The index where to insert target value.</returns>
45+
private static int BinarySearch(T[] array, int from, int to, T target, IComparer<T> comparer)
46+
{
47+
var left = from;
48+
var right = to;
49+
while (right > left)
4650
{
47-
var left = from;
48-
var right = to;
49-
while (right > left)
50-
{
51-
var middle = (left + right) / 2;
52-
var comparisonResult = comparer.Compare(target, array[middle]);
53-
54-
if (comparisonResult == 0)
55-
{
56-
return middle + 1;
57-
}
51+
var middle = (left + right) / 2;
52+
var comparisonResult = comparer.Compare(target, array[middle]);
5853

59-
if (comparisonResult > 0)
60-
{
61-
left = middle + 1;
62-
}
63-
else
64-
{
65-
right = middle - 1;
66-
}
54+
if (comparisonResult == 0)
55+
{
56+
return middle + 1;
6757
}
6858

69-
return comparer.Compare(target, array[left]) < 0 ? left : left + 1;
59+
if (comparisonResult > 0)
60+
{
61+
left = middle + 1;
62+
}
63+
else
64+
{
65+
right = middle - 1;
66+
}
7067
}
68+
69+
return comparer.Compare(target, array[left]) < 0 ? left : left + 1;
7170
}
7271
}
+40-41
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,62 @@
11
using System;
22
using System.Collections.Generic;
33

4-
namespace Algorithms.Sorters.Comparison
4+
namespace Algorithms.Sorters.Comparison;
5+
6+
/// <summary>
7+
/// Class that implements bogo sort algorithm.
8+
/// </summary>
9+
/// <typeparam name="T">Type of array element.</typeparam>
10+
public class BogoSorter<T> : IComparisonSorter<T>
511
{
12+
private readonly Random random = new();
13+
614
/// <summary>
7-
/// Class that implements bogo sort algorithm.
15+
/// TODO.
816
/// </summary>
9-
/// <typeparam name="T">Type of array element.</typeparam>
10-
public class BogoSorter<T> : IComparisonSorter<T>
17+
/// <param name="array">TODO. 2.</param>
18+
/// <param name="comparer">TODO. 3.</param>
19+
public void Sort(T[] array, IComparer<T> comparer)
1120
{
12-
private readonly Random random = new();
13-
14-
/// <summary>
15-
/// TODO.
16-
/// </summary>
17-
/// <param name="array">TODO. 2.</param>
18-
/// <param name="comparer">TODO. 3.</param>
19-
public void Sort(T[] array, IComparer<T> comparer)
21+
while (!IsSorted(array, comparer))
2022
{
21-
while (!IsSorted(array, comparer))
22-
{
23-
Shuffle(array);
24-
}
23+
Shuffle(array);
2524
}
25+
}
2626

27-
private bool IsSorted(T[] array, IComparer<T> comparer)
27+
private bool IsSorted(T[] array, IComparer<T> comparer)
28+
{
29+
for (var i = 0; i < array.Length - 1; i++)
2830
{
29-
for (var i = 0; i < array.Length - 1; i++)
31+
if (comparer.Compare(array[i], array[i + 1]) > 0)
3032
{
31-
if (comparer.Compare(array[i], array[i + 1]) > 0)
32-
{
33-
return false;
34-
}
33+
return false;
3534
}
36-
37-
return true;
3835
}
3936

40-
private void Shuffle(T[] array)
37+
return true;
38+
}
39+
40+
private void Shuffle(T[] array)
41+
{
42+
var taken = new bool[array.Length];
43+
var newArray = new T[array.Length];
44+
for (var i = 0; i < array.Length; i++)
4145
{
42-
var taken = new bool[array.Length];
43-
var newArray = new T[array.Length];
44-
for (var i = 0; i < array.Length; i++)
46+
int nextPos;
47+
do
4548
{
46-
int nextPos;
47-
do
48-
{
49-
nextPos = random.Next(0, int.MaxValue) % array.Length;
50-
}
51-
while (taken[nextPos]);
52-
53-
taken[nextPos] = true;
54-
newArray[nextPos] = array[i];
49+
nextPos = random.Next(0, int.MaxValue) % array.Length;
5550
}
51+
while (taken[nextPos]);
5652

57-
for (var i = 0; i < array.Length; i++)
58-
{
59-
array[i] = newArray[i];
60-
}
53+
taken[nextPos] = true;
54+
newArray[nextPos] = array[i];
55+
}
56+
57+
for (var i = 0; i < array.Length; i++)
58+
{
59+
array[i] = newArray[i];
6160
}
6261
}
6362
}

Algorithms/Sorters/Comparison/BubbleSorter.cs

+28-29
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,41 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22

3-
namespace Algorithms.Sorters.Comparison
3+
namespace Algorithms.Sorters.Comparison;
4+
5+
/// <summary>
6+
/// Class that implements bubble sort algorithm.
7+
/// </summary>
8+
/// <typeparam name="T">Type of array element.</typeparam>
9+
public class BubbleSorter<T> : IComparisonSorter<T>
410
{
511
/// <summary>
6-
/// Class that implements bubble sort algorithm.
12+
/// Sorts array using specified comparer,
13+
/// internal, in-place, stable,
14+
/// time complexity: O(n^2),
15+
/// space complexity: O(1),
16+
/// where n - array length.
717
/// </summary>
8-
/// <typeparam name="T">Type of array element.</typeparam>
9-
public class BubbleSorter<T> : IComparisonSorter<T>
18+
/// <param name="array">Array to sort.</param>
19+
/// <param name="comparer">Compares elements.</param>
20+
public void Sort(T[] array, IComparer<T> comparer)
1021
{
11-
/// <summary>
12-
/// Sorts array using specified comparer,
13-
/// internal, in-place, stable,
14-
/// time complexity: O(n^2),
15-
/// space complexity: O(1),
16-
/// where n - array length.
17-
/// </summary>
18-
/// <param name="array">Array to sort.</param>
19-
/// <param name="comparer">Compares elements.</param>
20-
public void Sort(T[] array, IComparer<T> comparer)
22+
for (var i = 0; i < array.Length - 1; i++)
2123
{
22-
for (var i = 0; i < array.Length - 1; i++)
24+
var wasChanged = false;
25+
for (var j = 0; j < array.Length - i - 1; j++)
2326
{
24-
var wasChanged = false;
25-
for (var j = 0; j < array.Length - i - 1; j++)
27+
if (comparer.Compare(array[j], array[j + 1]) > 0)
2628
{
27-
if (comparer.Compare(array[j], array[j + 1]) > 0)
28-
{
29-
var temp = array[j];
30-
array[j] = array[j + 1];
31-
array[j + 1] = temp;
32-
wasChanged = true;
33-
}
29+
var temp = array[j];
30+
array[j] = array[j + 1];
31+
array[j + 1] = temp;
32+
wasChanged = true;
3433
}
34+
}
3535

36-
if (!wasChanged)
37-
{
38-
break;
39-
}
36+
if (!wasChanged)
37+
{
38+
break;
4039
}
4140
}
4241
}

0 commit comments

Comments
 (0)