|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 |
|
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> |
5 | 11 | { |
6 | 12 | /// <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. |
8 | 19 | /// </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) |
11 | 23 | { |
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++) |
23 | 25 | { |
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); |
30 | 30 |
|
31 | | - array[targetInsertLocation] = target; |
32 | | - } |
| 31 | + array[targetInsertLocation] = target; |
33 | 32 | } |
| 33 | + } |
34 | 34 |
|
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) |
46 | 50 | { |
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]); |
58 | 53 |
|
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; |
67 | 57 | } |
68 | 58 |
|
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 | + } |
70 | 67 | } |
| 68 | + |
| 69 | + return comparer.Compare(target, array[left]) < 0 ? left : left + 1; |
71 | 70 | } |
72 | 71 | } |
0 commit comments