Skip to content

Commit 6370316

Browse files
committed
various optimizations
1 parent 0dde878 commit 6370316

23 files changed

+222
-245
lines changed

Advanced.Algorithms.Tests/Geometry/RectangleIntersection_Tests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,30 @@ public void RectIntersection_Smoke_Test()
1717
{
1818
var result = RectangleIntersection.FindIntersection(new Rectangle()
1919
{
20-
leftTopCorner = new Point() { x = 0, y = 10 },
21-
rightBottomCorner = new Point() { x = 10, y = 0 }
20+
LeftTopCorner = new Point() { x = 0, y = 10 },
21+
RightBottomCorner = new Point() { x = 10, y = 0 }
2222
},
2323
new Rectangle()
2424
{
25-
leftTopCorner = new Point() { x = 5, y = 5 },
26-
rightBottomCorner = new Point() { x = 15, y = 0 }
25+
LeftTopCorner = new Point() { x = 5, y = 5 },
26+
RightBottomCorner = new Point() { x = 15, y = 0 }
2727
});
2828

2929
Assert.AreEqual(result, new Rectangle()
3030
{
31-
leftTopCorner = new Point() { x = 5, y = 5 },
32-
rightBottomCorner = new Point() { x = 10, y = 0 }
31+
LeftTopCorner = new Point() { x = 5, y = 5 },
32+
RightBottomCorner = new Point() { x = 10, y = 0 }
3333
});
3434

3535
result = RectangleIntersection.FindIntersection(new Rectangle()
3636
{
37-
leftTopCorner = new Point() { x = 0, y = 10 },
38-
rightBottomCorner = new Point() { x = 4, y = 0 }
37+
LeftTopCorner = new Point() { x = 0, y = 10 },
38+
RightBottomCorner = new Point() { x = 4, y = 0 }
3939
},
4040
new Rectangle()
4141
{
42-
leftTopCorner = new Point() { x = 5, y = 5 },
43-
rightBottomCorner = new Point() { x = 15, y = 0 }
42+
LeftTopCorner = new Point() { x = 5, y = 5 },
43+
RightBottomCorner = new Point() { x = 15, y = 0 }
4444
});
4545

4646
Assert.AreEqual(result, default(Rectangle));

Advanced.Algorithms/Miscellaneous/MatrixMultiplication.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62

73
namespace Advanced.Algorithms.Miscellaneous
84
{
95
public class MatrixMultiplication
106
{
11-
internal static int[,] Multiply(int[,] a, int[,] b)
7+
public static int[,] Multiply(int[,] a, int[,] b)
128
{
139
if (a.GetLength(1) != b.GetLength(0))
1410
{

Advanced.Algorithms/NumericalMethods/Exponentiation.cs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,36 @@ public class FastExponentiation
99
/// <param name="power"></param>
1010
public static int BySquaring(int @base, int power)
1111
{
12-
//using the algebraic result
13-
//a^-n = (1/a)^n
14-
if (power < 0)
12+
while (true)
1513
{
16-
return BySquaring(1 / @base, -power);
17-
}
14+
//using the algebraic result
15+
//a^-n = (1/a)^n
16+
if (power < 0)
17+
{
18+
@base = 1 / @base;
19+
power = -power;
20+
continue;
21+
}
1822

19-
if (power == 0)
20-
{
21-
return 1;
22-
}
23-
else if (power == 1)
24-
{
25-
return @base;
26-
}
27-
//power is even
28-
else if (power % 2 == 0)
29-
{
30-
return BySquaring(@base * @base, power / 2);
31-
}
32-
//power is odd
33-
else
34-
{
35-
return @base * BySquaring(@base * @base, (power - 1) / 2);
23+
switch (power)
24+
{
25+
case 0:
26+
return 1;
27+
case 1:
28+
return @base;
29+
default:
30+
if (power % 2 == 0)
31+
{
32+
@base = @base * @base;
33+
power = power / 2;
34+
continue;
35+
}
36+
//power is odd
37+
else
38+
{
39+
return @base * BySquaring(@base * @base, (power - 1) / 2);
40+
}
41+
}
3642
}
3743
}
3844
}

Advanced.Algorithms/NumericalMethods/KthSmallest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Advanced.Algorithms.DataStructures;
2-
using System;
1+
using System;
32
using Advanced.Algorithms.DataStructures.Heap.Min;
43

54
namespace Advanced.Algorithms.NumericalMethods
@@ -30,7 +29,7 @@ public T FindKthSmallest(T[] input, int k)
3029
var minHeap = new BMinHeap<T>(input);
3130

3231
//0,1,2...(k-1) min extraction
33-
for (int i = 0; i < k - 1; i++)
32+
for (var i = 0; i < k - 1; i++)
3433
{
3534
minHeap.ExtractMin();
3635
}

Advanced.Algorithms/NumericalMethods/MedianStream.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Advanced.Algorithms.DataStructures;
2-
using Advanced.Algorithms.DataStructures.Heap.Max;
1+
using Advanced.Algorithms.DataStructures.Heap.Max;
32
using Advanced.Algorithms.DataStructures.Heap.Min;
43

54
namespace Advanced.Algorithms.NumericalMethods
@@ -32,8 +31,6 @@ public void Add(int newValue)
3231
{
3332
rightHeap.Insert(newValue);
3433
}
35-
36-
return;
3734
}
3835
//left has more elements
3936
else if (leftHeap.Count > rightHeap.Count)
@@ -88,13 +85,9 @@ public int GetMedian()
8885
}
8986

9087
//pick left top if left heap has greater count
91-
if (leftHeap.Count > rightHeap.Count)
92-
{
93-
return leftHeap.PeekMax();
94-
}
88+
return leftHeap.Count > rightHeap.Count ? leftHeap.PeekMax() : rightHeap.PeekMin();
9589

9690
//pick right top otherwise
97-
return rightHeap.PeekMin();
9891
}
9992
}
10093
}

Advanced.Algorithms/NumericalMethods/PrimeGenerator.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,21 @@ public static List<int> GetAllPrimes(int max)
1919
for (int i = 2; i < sqrt; i++)
2020
{
2121
//mark multiples of current number as true
22-
if (!primeTable[i])
22+
if (primeTable[i])
23+
{
24+
continue;
25+
}
26+
27+
for (var j = 2 * i; j <= max; j = j + i)
2328
{
24-
for (int j = 2 * i; j <= max; j = j + i)
25-
{
26-
primeTable[j] = true;
27-
}
29+
primeTable[j] = true;
2830
}
2931
}
3032

3133
//now write back results
3234
var result = new List<int>();
3335

34-
for (int i = 2; i < primeTable.Length; i++)
36+
for (var i = 2; i < primeTable.Length; i++)
3537
{
3638
if (!primeTable[i])
3739
{

Advanced.Algorithms/NumericalMethods/PrimeTester.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static bool IsPrime(int number)
3636
//inside the loop
3737
//check until square root of number
3838
var sqrt = Math.Sqrt(number);
39-
for (int i = 5; i <= sqrt; i = i + 6)
39+
for (var i = 5; i <= sqrt; i = i + 6)
4040
{
4141
//check for two potential primes
4242
if (number % i == 0 || number % (i + 2) == 0)

Advanced.Algorithms/Search/BinarySearch.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,38 @@ public class BinarySearch
44
{
55
public static int Search(int[] input, int element)
66
{
7-
return SearchRecursive(input, 0, input.Length - 1, element);
7+
return search(input, 0, input.Length - 1, element);
88
}
99

10-
private static int SearchRecursive(int[] input, int i, int j, int element)
10+
private static int search(int[] input, int i, int j, int element)
1111
{
12-
if (i == j)
12+
while (true)
1313
{
14-
if (input[i] == element)
14+
if (i == j)
1515
{
16-
return i;
16+
if (input[i] == element)
17+
{
18+
return i;
19+
}
20+
21+
return -1;
1722
}
1823

19-
return -1;
20-
}
24+
var mid = (i + j) / 2;
2125

22-
var mid = (i + j) / 2;
26+
if (input[mid] == element)
27+
{
28+
return mid;
29+
}
2330

24-
if (input[mid] == element)
25-
{
26-
return mid;
27-
}
31+
if (input[mid] > element)
32+
{
33+
j = mid;
34+
continue;
35+
}
2836

29-
if (input[mid] > element)
30-
{
31-
return SearchRecursive(input, i, mid, element);
37+
i = mid + 1;
3238
}
33-
34-
return SearchRecursive(input, mid + 1, j, element);
35-
3639
}
3740
}
3841
}
Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,52 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace Advanced.Algorithms.Search
1+
namespace Advanced.Algorithms.Search
82
{
93

104
public class SearchAlmostSorted
115
{
126
public static int Search(int[] input, int element)
137
{
14-
return SearchRecursive(input, 0, input.Length - 1, element);
8+
return search(input, 0, input.Length - 1, element);
159
}
1610

17-
private static int SearchRecursive(int[] input, int i, int j, int element)
11+
private static int search(int[] input, int i, int j, int element)
1812
{
19-
if (i == j)
13+
while (true)
2014
{
21-
if (input[i] == element)
15+
if (i == j)
2216
{
23-
return i;
17+
if (input[i] == element)
18+
{
19+
return i;
20+
}
21+
22+
return -1;
2423
}
2524

26-
return -1;
27-
}
25+
var mid = (i + j) / 2;
2826

29-
var mid = (i + j) / 2;
27+
if (input[mid] == element)
28+
{
29+
return mid;
30+
}
3031

31-
if (input[mid] == element)
32-
{
33-
return mid;
34-
}
32+
if (mid > 0 && input[mid - 1] == element)
33+
{
34+
return mid - 1;
35+
}
3536

36-
if (mid > 0 && input[mid - 1] == element)
37-
{
38-
return mid - 1;
39-
}
37+
if (mid < input.Length - 1 && input[mid + 1] == element)
38+
{
39+
return mid + 1;
40+
}
4041

41-
if (mid < input.Length - 1 && input[mid + 1] == element)
42-
{
43-
return mid + 1;
44-
}
42+
if (input[mid] > element)
43+
{
44+
j = mid;
45+
continue;
46+
}
4547

46-
if (input[mid] > element)
47-
{
48-
return SearchRecursive(input, i, mid, element);
48+
i = mid + 1;
4949
}
50-
51-
return SearchRecursive(input, mid + 1, j, element);
52-
5350
}
5451
}
5552
}

Advanced.Algorithms/Sorting/BucketSort.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ public class BucketSort
1313
/// <summary>
1414
/// Sort given integers using bucket sort with merge sort as sub sort
1515
/// </summary>
16-
/// <param name="array"></param>
17-
/// <param name="mst"></param>
1816
/// <returns></returns>
1917
public static int[] Sort(int[] array, int bucketSize)
2018
{
@@ -28,17 +26,19 @@ public static int[] Sort(int[] array, int bucketSize)
2826
int i;
2927
for (i = 0; i < array.Length; i++)
3028
{
31-
if (bucketSize != 0)
29+
if (bucketSize == 0)
3230
{
33-
var bucketIndex = array[i] / bucketSize;
31+
continue;
32+
}
3433

35-
if (!buckets.ContainsKey(bucketIndex))
36-
{
37-
buckets.Add(bucketIndex, new List<int>());
38-
}
34+
var bucketIndex = array[i] / bucketSize;
3935

40-
buckets[bucketIndex].Add(array[i]);
36+
if (!buckets.ContainsKey(bucketIndex))
37+
{
38+
buckets.Add(bucketIndex, new List<int>());
4139
}
40+
41+
buckets[bucketIndex].Add(array[i]);
4242
}
4343

4444
i = 0;

0 commit comments

Comments
 (0)