├── .github └── workflows │ └── restore-build-test.yml ├── .gitignore ├── BasicAlgorithms.Tests ├── Arrays │ ├── DataProviders │ │ ├── ReverseSortedProviderTests.cs │ │ ├── SortedAndUniformProviderTests.cs │ │ ├── SortedProviderTests.cs │ │ └── UnsortedProviderTests.cs │ ├── SearchAlgorithms │ │ ├── BinarySearchTests.cs │ │ ├── InterpolationSearchTests.cs │ │ ├── JumpSearchTests.cs │ │ └── LinearSearchTests.cs │ └── SortingAlgorithms │ │ ├── BubbleSortTests.cs │ │ ├── HeapSortTests.cs │ │ ├── InsertionSortTests.cs │ │ ├── MergeSortTests.cs │ │ ├── QuickSortTests.cs │ │ └── SelectionSortTests.cs ├── BasicAlgorithms.Tests.csproj ├── Practice │ └── VariousProblemsTests.cs └── Trees │ ├── DataProviders │ └── HeapTreeProviderTests.cs │ ├── Traversals │ └── BreadthFirstTests.cs │ └── TreeAlgorithms │ ├── BinarySearchTreeTests.cs │ └── HeapTreeTests.cs ├── BasicAlgorithms.sln ├── BasicAlgorithms ├── Arrays │ ├── ArraySearchFactory.cs │ ├── ArraySortFactory.cs │ ├── DataProviders │ │ ├── Interfaces │ │ │ └── IArrayDataProvider.cs │ │ ├── Models │ │ │ └── EnumArrayDataProviders.cs │ │ └── Providers │ │ │ ├── ReverseSortedProvider.cs │ │ │ ├── SortedAndUniformProvider.cs │ │ │ ├── SortedProvider.cs │ │ │ └── UnsortedProvider.cs │ ├── DataProvidersFactory.cs │ ├── SearchAlgorithms │ │ ├── BinarySearch.cs │ │ ├── Interfaces │ │ │ └── ISearch.cs │ │ ├── InterpolationSearch.cs │ │ ├── JumpSearch.cs │ │ ├── LinearSearch.cs │ │ └── Models │ │ │ ├── EnumArraySearchAlgorithms.cs │ │ │ ├── SearchResult.cs │ │ │ └── SearchResults.cs │ └── SortingAlgorithms │ │ ├── BubbleSort.cs │ │ ├── HeapSort.cs │ │ ├── InsertionSort.cs │ │ ├── Interfaces │ │ └── ISort.cs │ │ ├── MergeSort.cs │ │ ├── Models │ │ ├── EnumArraySortAlgorithms.cs │ │ └── SortResults.cs │ │ ├── QuickSort.cs │ │ └── SelectionSort.cs ├── BasicAlgorithms.csproj ├── Practice │ └── VariousProblems.cs ├── Program.cs ├── Trees │ ├── DataProviders │ │ ├── Interfaces │ │ │ └── ITreeDataProvider.cs │ │ ├── Models │ │ │ └── EnumTreeProvider.cs │ │ └── Providers │ │ │ └── TreeDataProvider.cs │ ├── DataProvidersFactory.cs │ ├── TraversalFactory.cs │ ├── TreeAlgorithms │ │ ├── Interfaces │ │ │ ├── ITraversals.cs │ │ │ └── ITree.cs │ │ ├── Models │ │ │ ├── BinaryTree.cs │ │ │ ├── BinaryTreeEstimation.cs │ │ │ ├── BinaryTreeResults.cs │ │ │ ├── EnumTraversalTypes.cs │ │ │ ├── EnumTreeTypes.cs │ │ │ └── TraversalResults.cs │ │ ├── Traversals │ │ │ ├── BreadthFirstTraversal.cs │ │ │ ├── InOrderTraversal.cs │ │ │ ├── PostOrderTraversal.cs │ │ │ └── PreOrderTraversal.cs │ │ └── TypedTrees │ │ │ ├── BinarySearchTree.cs │ │ │ └── HeapTree.cs │ └── TreeFactory.cs └── UI │ ├── ArraySearchUI.cs │ ├── ArraySortUI.cs │ ├── Grid.cs │ ├── TraversalUI.cs │ └── TreeUI.cs ├── README.md └── README ├── search_results.png ├── sort_results.png ├── traversal_results.png └── tree_results.png /.github/workflows/restore-build-test.yml: -------------------------------------------------------------------------------- 1 | name: .NET 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | - name: Setup .NET 17 | uses: actions/setup-dotnet@v1 18 | with: 19 | dotnet-version: 6.0.x 20 | - name: Restore dependencies 21 | run: dotnet restore 22 | - name: Build 23 | run: dotnet build --no-restore 24 | - name: Test 25 | run: dotnet test --no-build --verbosity normal 26 | 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled source # 2 | ################### 3 | bin/ 4 | obj/ 5 | .vs/ 6 | 7 | # OS generated files # 8 | ###################### 9 | .DS_Store 10 | .DS_Store? 11 | ._* 12 | .Spotlight-V100 13 | .Trashes 14 | ehthumbs.db 15 | Thumbs.db 16 | 17 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Arrays/DataProviders/ReverseSortedProviderTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.DataProviders.Providers; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | 4 | namespace BasicAlgorithms.Tests.Arrays.DataProviders 5 | { 6 | [TestClass] 7 | public class ReverseSortedProviderTests 8 | { 9 | [TestMethod] 10 | public void ReverseSortedProvider_GetSortedList() 11 | { 12 | var data = new ReverseSortedProvider(10); 13 | 14 | Assert.AreEqual(data.Data[0], 10); 15 | Assert.AreEqual(10, data.Data.Count); 16 | Assert.AreEqual(1, data.MinValue); 17 | Assert.AreEqual(10, data.MaxValue); 18 | Assert.AreEqual(6, data.AvgValue); 19 | Assert.AreEqual(11, data.NotFoundValue); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Arrays/DataProviders/SortedAndUniformProviderTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.DataProviders.Providers; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | 4 | namespace BasicAlgorithms.Tests.Arrays.DataProviders 5 | { 6 | [TestClass] 7 | public class SortedAndUniformProviderTests 8 | { 9 | [TestMethod] 10 | public void SortedAndUniformProvider_GetSortedList() 11 | { 12 | var data = new SortedAndUniformProvider(10); 13 | 14 | Assert.AreEqual(10, data.Data.Count); 15 | Assert.AreEqual(1, data.MinValue); 16 | Assert.AreEqual(10, data.MaxValue); 17 | Assert.AreEqual(6, data.AvgValue); 18 | Assert.AreEqual(11, data.NotFoundValue); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Arrays/DataProviders/SortedProviderTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.DataProviders.Providers; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | 4 | namespace BasicAlgorithms.Tests.Arrays.DataProviders 5 | { 6 | [TestClass] 7 | public class SortedProviderTests 8 | { 9 | [TestMethod] 10 | public void SortedProvider_GetSortedList() 11 | { 12 | var data = new SortedProvider(10); 13 | 14 | Assert.AreEqual(10, data.Data.Count); 15 | Assert.AreEqual(0, data.MinValue); 16 | Assert.AreEqual(2147483638, data.MaxValue); 17 | Assert.AreEqual(2147483642, data.AvgValue); 18 | Assert.AreEqual(2147483643, data.NotFoundValue); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Arrays/DataProviders/UnsortedProviderTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.DataProviders.Providers; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | 4 | namespace BasicAlgorithms.Tests.Arrays.DataProviders 5 | { 6 | [TestClass] 7 | public class UnsortedProviderTests 8 | { 9 | [TestMethod] 10 | public void UnsortedProvider_GetSortedList() 11 | { 12 | var data = new UnsortedProvider(10); 13 | 14 | Assert.AreEqual(10, data.Data.Count); 15 | Assert.AreEqual(10, data.MinValue); 16 | Assert.AreEqual(94, data.MaxValue); 17 | Assert.AreEqual(64, data.AvgValue); 18 | Assert.AreEqual(95, data.NotFoundValue); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Arrays/SearchAlgorithms/BinarySearchTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SearchAlgorithms; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Tests.Arrays.SearchAlgorithms 6 | { 7 | [TestClass] 8 | public class BinarySearchTests 9 | { 10 | [TestMethod] 11 | public void BinarySearch_Find_FindFirst() 12 | { 13 | var search = new BinarySearch(); 14 | 15 | var list = new List() { 2, 4, 6 }; 16 | var result = search.Find(list, 2); 17 | Assert.AreEqual(0, result.PositionFound); 18 | } 19 | 20 | [TestMethod] 21 | public void BinarySearch_Find_FindLast() 22 | { 23 | var search = new BinarySearch(); 24 | 25 | var list = new List() { 2, 4, 6 }; 26 | var result = search.Find(list, 6); 27 | Assert.AreEqual(2, result.PositionFound); 28 | 29 | list = new List() { 2, 4, 6, 8 }; 30 | result = search.Find(list, 8); 31 | Assert.AreEqual(3, result.PositionFound); 32 | } 33 | 34 | [TestMethod] 35 | public void BinarySearch_Find_FindMiddle() 36 | { 37 | var search = new BinarySearch(); 38 | 39 | var list = new List() { 2, 4, 6 }; 40 | var result = search.Find(list, 4); 41 | Assert.AreEqual(1, result.PositionFound); 42 | 43 | list = new List() { 2, 4, 6, 8 }; 44 | result = search.Find(list, 6); 45 | Assert.AreEqual(2, result.PositionFound); 46 | } 47 | 48 | [TestMethod] 49 | public void BinarySearch_Find_NoFind() 50 | { 51 | var search = new BinarySearch(); 52 | 53 | var list = new List() { 2, 4, 6 }; 54 | var result = search.Find(list, 5); 55 | Assert.IsNull(result.PositionFound); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Arrays/SearchAlgorithms/InterpolationSearchTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SearchAlgorithms; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Tests.Arrays.SearchAlgorithms 6 | { 7 | [TestClass] 8 | public class InterpolationSearchTests 9 | { 10 | [TestMethod] 11 | public void InterpolationSearch_Find_FindFirst() 12 | { 13 | var search = new InterpolationSearch(); 14 | 15 | var list = new List() { 2, 4, 6 }; 16 | var result = search.Find(list, 2); 17 | Assert.AreEqual(0, result.PositionFound); 18 | } 19 | 20 | [TestMethod] 21 | public void InterpolationSearch_Find_FindLast() 22 | { 23 | var search = new InterpolationSearch(); 24 | 25 | var list = new List() { 2, 4, 6 }; 26 | var result = search.Find(list, 6); 27 | Assert.AreEqual(2, result.PositionFound); 28 | 29 | list = new List() { 2, 4, 6, 8 }; 30 | result = search.Find(list, 8); 31 | Assert.AreEqual(3, result.PositionFound); 32 | } 33 | 34 | [TestMethod] 35 | public void InterpolationSearch_Find_FindMiddle() 36 | { 37 | var search = new InterpolationSearch(); 38 | 39 | var list = new List() { 2, 4, 6 }; 40 | var result = search.Find(list, 4); 41 | Assert.AreEqual(1, result.PositionFound); 42 | 43 | list = new List() { 2, 4, 6, 8 }; 44 | result = search.Find(list, 6); 45 | Assert.AreEqual(2, result.PositionFound); 46 | } 47 | 48 | [TestMethod] 49 | public void InterpolationSearch_Find_NoFind() 50 | { 51 | var search = new InterpolationSearch(); 52 | 53 | var list = new List() { 2, 4, 6 }; 54 | var result = search.Find(list, 5); 55 | Assert.IsNull(result.PositionFound); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Arrays/SearchAlgorithms/JumpSearchTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SearchAlgorithms; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Tests.Arrays.SearchAlgorithms 6 | { 7 | [TestClass] 8 | public class JumpSearchTests 9 | { 10 | [TestMethod] 11 | public void JumpSearch_Find_FindFirst() 12 | { 13 | var search = new JumpSearch(); 14 | 15 | var list = new List() { 2, 4, 6 }; 16 | var result = search.Find(list, 2); 17 | Assert.AreEqual(0, result.PositionFound); 18 | } 19 | 20 | [TestMethod] 21 | public void JumpSearch_Find_FindLast() 22 | { 23 | var search = new JumpSearch(); 24 | 25 | var list = new List() { 2, 4, 6 }; 26 | var result = search.Find(list, 6); 27 | Assert.AreEqual(2, result.PositionFound); 28 | 29 | list = new List() { 2, 4, 6, 8 }; 30 | result = search.Find(list, 8); 31 | Assert.AreEqual(3, result.PositionFound); 32 | } 33 | 34 | [TestMethod] 35 | public void JumpSearch_Find_FindMiddle() 36 | { 37 | var search = new JumpSearch(); 38 | 39 | var list = new List() { 2, 4, 6 }; 40 | var result = search.Find(list, 4); 41 | Assert.AreEqual(1, result.PositionFound); 42 | 43 | list = new List() { 2, 4, 6, 8 }; 44 | result = search.Find(list, 6); 45 | Assert.AreEqual(2, result.PositionFound); 46 | } 47 | 48 | [TestMethod] 49 | public void JumpSearch_Find_NoFind() 50 | { 51 | var search = new JumpSearch(); 52 | 53 | var list = new List() { 2, 4, 6 }; 54 | var result = search.Find(list, 5); 55 | Assert.IsNull(result.PositionFound); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Arrays/SearchAlgorithms/LinearSearchTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SearchAlgorithms; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Tests.Arrays.SearchAlgorithms 6 | { 7 | [TestClass] 8 | public class LinearSearchTests 9 | { 10 | [TestMethod] 11 | public void LinearSearch_Find_FindFirst() 12 | { 13 | var search = new LinearSearch(); 14 | 15 | var list = new List() { 2, 4, 6 }; 16 | var result = search.Find(list, 2); 17 | Assert.AreEqual(0, result.PositionFound); 18 | } 19 | 20 | [TestMethod] 21 | public void LinearSearch_Find_FindLast() 22 | { 23 | var search = new LinearSearch(); 24 | 25 | var list = new List() { 2, 4, 6 }; 26 | var result = search.Find(list, 6); 27 | Assert.AreEqual(2, result.PositionFound); 28 | } 29 | 30 | [TestMethod] 31 | public void LinearSearch_Find_FindMiddle() 32 | { 33 | var search = new LinearSearch(); 34 | 35 | var list = new List() { 2, 4, 6 }; 36 | var result = search.Find(list, 4); 37 | Assert.AreEqual(1, result.PositionFound); 38 | } 39 | 40 | [TestMethod] 41 | public void LinearSearch_Find_NoFind() 42 | { 43 | var search = new LinearSearch(); 44 | 45 | var list = new List() { 2, 4, 6 }; 46 | var result = search.Find(list, 5); 47 | Assert.IsNull(result.PositionFound); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Arrays/SortingAlgorithms/BubbleSortTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SortingAlgorithms; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Tests.Arrays.SortingAlgorithms 6 | { 7 | [TestClass] 8 | public class BubbleSortTests 9 | { 10 | [TestMethod] 11 | public void BubbleSort_Sort() 12 | { 13 | var search = new BubbleSort(); 14 | 15 | var list = new List() { 5, 1, 3, 2, 4 }; 16 | var result = search.Sort(list); 17 | Assert.AreEqual(1, result.SortedData[0]); 18 | Assert.AreEqual(2, result.SortedData[1]); 19 | Assert.AreEqual(3, result.SortedData[2]); 20 | Assert.AreEqual(4, result.SortedData[3]); 21 | Assert.AreEqual(5, result.SortedData[4]); 22 | } 23 | 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Arrays/SortingAlgorithms/HeapSortTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SortingAlgorithms; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Tests.Arrays.SortingAlgorithms 6 | { 7 | [TestClass] 8 | public class HeapSortTests 9 | { 10 | [TestMethod] 11 | public void HeapSort_Sort() 12 | { 13 | var search = new HeapSort(); 14 | 15 | var list = new List() { 5, 1, 3, 2, 4 }; 16 | var result = search.Sort(list); 17 | Assert.AreEqual(1, result.SortedData[0]); 18 | Assert.AreEqual(2, result.SortedData[1]); 19 | Assert.AreEqual(3, result.SortedData[2]); 20 | Assert.AreEqual(4, result.SortedData[3]); 21 | Assert.AreEqual(5, result.SortedData[4]); 22 | } 23 | 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Arrays/SortingAlgorithms/InsertionSortTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SortingAlgorithms; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Tests.Arrays.SortingAlgorithms 6 | { 7 | [TestClass] 8 | public class InsertionSortTests 9 | { 10 | [TestMethod] 11 | public void InsertionSort_Sort() 12 | { 13 | var search = new InsertionSort(); 14 | 15 | var list = new List() { 5, 1, 3, 2, 4 }; 16 | var result = search.Sort(list); 17 | Assert.AreEqual(1, result.SortedData[0]); 18 | Assert.AreEqual(2, result.SortedData[1]); 19 | Assert.AreEqual(3, result.SortedData[2]); 20 | Assert.AreEqual(4, result.SortedData[3]); 21 | Assert.AreEqual(5, result.SortedData[4]); 22 | } 23 | 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Arrays/SortingAlgorithms/MergeSortTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SortingAlgorithms; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Tests.Arrays.SortingAlgorithms 6 | { 7 | [TestClass] 8 | public class MergeSortTests 9 | { 10 | [TestMethod] 11 | public void MergeSort_Sort() 12 | { 13 | var search = new MergeSort(); 14 | 15 | var list = new List() { 5, 1, 3, 2, 4 }; ; 16 | var result = search.Sort(list); 17 | Assert.AreEqual(1, result.SortedData[0]); 18 | Assert.AreEqual(2, result.SortedData[1]); 19 | Assert.AreEqual(3, result.SortedData[2]); 20 | Assert.AreEqual(4, result.SortedData[3]); 21 | Assert.AreEqual(5, result.SortedData[4]); 22 | } 23 | 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Arrays/SortingAlgorithms/QuickSortTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SortingAlgorithms; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Tests.Arrays.SortingAlgorithms 6 | { 7 | [TestClass] 8 | public class QuickSortTests 9 | { 10 | [TestMethod] 11 | public void QuickSort_Sort() 12 | { 13 | var search = new QuickSort(); 14 | 15 | var list = new List() { 5, 1, 3, 2, 4 }; 16 | var result = search.Sort(list); 17 | Assert.AreEqual(1, result.SortedData[0]); 18 | Assert.AreEqual(2, result.SortedData[1]); 19 | Assert.AreEqual(3, result.SortedData[2]); 20 | Assert.AreEqual(4, result.SortedData[3]); 21 | Assert.AreEqual(5, result.SortedData[4]); 22 | } 23 | 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Arrays/SortingAlgorithms/SelectionSortTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SortingAlgorithms; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Tests.Arrays.SortingAlgorithms 6 | { 7 | [TestClass] 8 | public class SelectionSortTests 9 | { 10 | [TestMethod] 11 | public void SelectionSort_Sort() 12 | { 13 | var search = new SelectionSort(); 14 | 15 | var list = new List() { 4, 1, 3, 5, 2 }; 16 | var result = search.Sort(list); 17 | Assert.AreEqual(1, result.SortedData[0]); 18 | Assert.AreEqual(2, result.SortedData[1]); 19 | Assert.AreEqual(3, result.SortedData[2]); 20 | Assert.AreEqual(4, result.SortedData[3]); 21 | Assert.AreEqual(5, result.SortedData[4]); 22 | } 23 | 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/BasicAlgorithms.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Practice/VariousProblemsTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Practice; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Tests.Practice 6 | { 7 | [TestClass] 8 | public class VariousProblemsTests 9 | { 10 | 11 | [TestMethod] 12 | public void VariousProblems_LongestPalindrom() 13 | { 14 | string result; 15 | 16 | result = VariousProblems.LongestPalindrom("bb"); 17 | Assert.AreEqual("bb", result); 18 | 19 | result = VariousProblems.LongestPalindrom("babad"); 20 | Assert.AreEqual("aba", result); 21 | 22 | result = VariousProblems.LongestPalindrom("eabcb"); 23 | Assert.AreEqual("bcb", result); 24 | 25 | } 26 | 27 | 28 | [TestMethod] 29 | public void VariousProblems_LeftRotateMatrix() 30 | { 31 | var rotated = VariousProblems.LeftRotateMatrix(2, 2, 3, new List { 1, 2, 3, 4 }); 32 | 33 | Assert.AreEqual(2, rotated[0]); 34 | Assert.AreEqual(1, rotated[1]); 35 | Assert.AreEqual(4, rotated[2]); 36 | Assert.AreEqual(3, rotated[3]); 37 | } 38 | 39 | 40 | [TestMethod] 41 | public void VariousProblems_PrimeNumberSets() 42 | { 43 | Assert.AreEqual(4, VariousProblems.PrimeNumberSets(6, 10)); 44 | Assert.AreEqual(5, VariousProblems.PrimeNumberSets(10, 15)); 45 | } 46 | 47 | 48 | [TestMethod] 49 | public void VariousProblems_FindCharacterBinary() 50 | { 51 | Assert.AreEqual(1, VariousProblems.FindCharacterBinary(5, 5, 3)); 52 | Assert.AreEqual(1, VariousProblems.FindCharacterBinary(11, 6, 4)); 53 | } 54 | 55 | 56 | [TestMethod] 57 | public void VariousProblems_MaximumTipCalculator() 58 | { 59 | var orders = new List { 5, 3, 3 }; 60 | var tipsA = new List { 1, 2, 3, 4, 5 }; 61 | var tipsB = new List { 5, 4, 3, 2, 1 }; 62 | Assert.AreEqual(21, VariousProblems.MaximumTipCalculator(orders, tipsA, tipsB)); 63 | 64 | orders = new List { 8, 4, 4 }; 65 | tipsA = new List { 1, 4, 3, 2, 7, 5, 9, 6 }; 66 | tipsB = new List { 1, 2, 3, 6, 5, 4, 9, 8 }; 67 | Assert.AreEqual(43, VariousProblems.MaximumTipCalculator(orders, tipsA, tipsB)); 68 | } 69 | 70 | [TestMethod] 71 | public void VariousProblems_ArrayContiguousIntegers() 72 | { 73 | var list = new List { 5, 2, 3, 6, 4, 4, 6, 6 }; 74 | Assert.IsTrue(VariousProblems.ArrayContiguousIntegers(list));//2,3,4,5,6 75 | 76 | list = new List { 10, 14, 10, 12, 12, 13, 15 }; 77 | Assert.IsFalse(VariousProblems.ArrayContiguousIntegers(list));//no continous numbers 78 | } 79 | 80 | [TestMethod] 81 | public void VariousProblems_OrderByAbsoluteOrder() 82 | { 83 | var list = new List { 1, -3, 2, 3, 6, -1 }; 84 | var result = VariousProblems.OrderByAbsoluteOrder(list); 85 | Assert.AreEqual(-1, result[0]); 86 | Assert.AreEqual(1, result[1]); 87 | Assert.AreEqual(-3, result[2]); 88 | Assert.AreEqual(3, result[3]); 89 | 90 | list = new List { 4, 8, 9, -4, 1, -1, -8, -9 }; 91 | result = VariousProblems.OrderByAbsoluteOrder(list); 92 | Assert.AreEqual(-1, result[0]); 93 | Assert.AreEqual(1, result[1]); 94 | Assert.AreEqual(-4, result[2]); 95 | Assert.AreEqual(4, result[3]); 96 | Assert.AreEqual(-8, result[4]); 97 | Assert.AreEqual(8, result[5]); 98 | Assert.AreEqual(-9, result[6]); 99 | Assert.AreEqual(9, result[7]); 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Trees/DataProviders/HeapTreeProviderTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees.DataProviders.Providers; 2 | using BasicAlgorithms.Trees.TreeAlgorithms.TypedTrees; 3 | using Microsoft.VisualStudio.TestTools.UnitTesting; 4 | 5 | namespace BasicAlgorithms.Tests.Trees.DataProviders 6 | { 7 | [TestClass] 8 | public class HeapTreeProviderTests 9 | { 10 | [TestMethod] 11 | public void HeapTreeProvider_GetTree() 12 | { 13 | var data = new TreeDataProvider(new HeapTree(), 10); 14 | 15 | Assert.AreEqual(1, data.MinValue); 16 | Assert.AreEqual(10, data.MaxValue); 17 | Assert.AreEqual(6, data.AvgValue); 18 | Assert.AreEqual(11, data.NotFoundValue); 19 | 20 | Assert.AreEqual(10, data.Tree.Data); 21 | Assert.AreEqual(9, data.Tree.LeftNode.Data); 22 | Assert.AreEqual(7, data.Tree.LeftNode.LeftNode.Data); 23 | Assert.AreEqual(6, data.Tree.LeftNode.RightNode.Data); 24 | Assert.AreEqual(8, data.Tree.RightNode.Data); 25 | Assert.AreEqual(5, data.Tree.RightNode.LeftNode.Data); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Trees/Traversals/BreadthFirstTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees.TreeAlgorithms.Traversals; 2 | using BasicAlgorithms.Trees.TreeAlgorithms.TypedTrees; 3 | using Microsoft.VisualStudio.TestTools.UnitTesting; 4 | using System.Collections.Generic; 5 | 6 | namespace BasicAlgorithms.Tests.Trees.Traversals 7 | { 8 | [TestClass] 9 | public class BreadthFirstTests 10 | { 11 | 12 | 13 | [TestMethod] 14 | public void BreadthFirst_Traversal() 15 | { 16 | var treeCreation = new BinarySearchTree(); 17 | var list = new List() { 2, 4, 3, 1 }; 18 | var tree = treeCreation.CreateTree(list).Result; 19 | 20 | list = new BreadthFirstTraversal().Traverse(tree); 21 | 22 | Assert.AreEqual(2, list[0]); 23 | Assert.AreEqual(1, list[1]); 24 | Assert.AreEqual(3, list[2]); 25 | Assert.AreEqual(4, list[3]); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Trees/TreeAlgorithms/BinarySearchTreeTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees.TreeAlgorithms.TypedTrees; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Tests.Trees.TreeAlgorithms 6 | { 7 | [TestClass] 8 | public class BinarySearchTreeTests 9 | { 10 | 11 | [TestMethod] 12 | public void BinarySearchTree_Deserialize() 13 | { 14 | var tree = new BinarySearchTree(); 15 | 16 | var list = new List() { 1, 2, 3, 4, 5, 6 }; 17 | var result = tree.CreateTree(list).Result; 18 | 19 | Assert.AreEqual(3, result.Data); 20 | Assert.AreEqual(1, result.LeftNode.Data); 21 | Assert.AreEqual(2, result.LeftNode.RightNode.Data); 22 | Assert.AreEqual(5, result.RightNode.Data); 23 | Assert.AreEqual(4, result.RightNode.LeftNode.Data); 24 | Assert.AreEqual(6, result.RightNode.RightNode.Data); 25 | } 26 | 27 | [TestMethod] 28 | public void BinarySearchTree_Deserialize_SmallTest() 29 | { 30 | var tree = new BinarySearchTree(); 31 | 32 | var list = new List() { 1, 2, 3 }; 33 | var result = tree.CreateTree(list).Result; 34 | 35 | Assert.AreEqual(2, result.Data); 36 | Assert.AreEqual(1, result.LeftNode.Data); 37 | Assert.AreEqual(3, result.RightNode.Data); 38 | } 39 | 40 | [TestMethod] 41 | public void BinarySearchTree_Insert_AtTop() 42 | { 43 | var binarySearchTree = new BinarySearchTree(); 44 | 45 | var list = new List() { 2, 4, 3, 1 }; 46 | var tree = binarySearchTree.CreateTree(list).Result; 47 | 48 | Assert.AreEqual(2, tree.Data); 49 | Assert.AreEqual(1, tree.LeftNode.Data); 50 | Assert.AreEqual(3, tree.RightNode.Data); 51 | Assert.AreEqual(4, tree.RightNode.RightNode.Data); 52 | 53 | tree = binarySearchTree.Insert(tree, 5).Result; 54 | Assert.AreEqual(2, tree.Data); 55 | Assert.AreEqual(1, tree.LeftNode.Data); 56 | Assert.AreEqual(3, tree.RightNode.Data); 57 | Assert.AreEqual(4, tree.RightNode.RightNode.Data); 58 | Assert.AreEqual(5, tree.RightNode.RightNode.RightNode.Data); 59 | } 60 | 61 | [TestMethod] 62 | public void BinarySearchTree_Insert_AtMiddle() 63 | { 64 | var binarySearchTree = new BinarySearchTree(); 65 | 66 | var list = new List() { 2, 4, 6 }; 67 | var tree = binarySearchTree.CreateTree(list).Result; 68 | 69 | Assert.AreEqual(4, tree.Data); 70 | Assert.AreEqual(2, tree.LeftNode.Data); 71 | Assert.AreEqual(6, tree.RightNode.Data); 72 | 73 | tree = binarySearchTree.Insert(tree, 5).Result; 74 | Assert.AreEqual(4, tree.Data); 75 | Assert.AreEqual(2, tree.LeftNode.Data); 76 | Assert.AreEqual(6, tree.RightNode.Data); 77 | Assert.AreEqual(5, tree.RightNode.LeftNode.Data); 78 | } 79 | 80 | [TestMethod] 81 | public void BinarySearchTree_Search() 82 | { 83 | var binarySearchTree = new BinarySearchTree(); 84 | 85 | var list = new List() { 2, 4, 6 }; 86 | var tree = binarySearchTree.CreateTree(list).Result; 87 | 88 | var item = binarySearchTree.Search(tree, 2).Result; 89 | Assert.AreEqual(2, item.Data); 90 | } 91 | 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /BasicAlgorithms.Tests/Trees/TreeAlgorithms/HeapTreeTests.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees.TreeAlgorithms.TypedTrees; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Tests.Trees.TreeAlgorithms 6 | { 7 | [TestClass] 8 | public class HeapTreeTests 9 | { 10 | 11 | [TestMethod] 12 | public void HeapTree_Deserialize() 13 | { 14 | var tree = new HeapTree(); 15 | 16 | var list = new List() { 3, 6, 2, 4, 5, 1 }; 17 | var result = tree.CreateTree(list).Result; 18 | 19 | Assert.AreEqual(6, result.Data); 20 | Assert.AreEqual(5, result.LeftNode.Data); 21 | Assert.AreEqual(4, result.LeftNode.LeftNode.Data); 22 | Assert.AreEqual(3, result.LeftNode.RightNode.Data); 23 | Assert.AreEqual(2, result.RightNode.Data); 24 | Assert.AreEqual(1, result.RightNode.LeftNode.Data); 25 | } 26 | 27 | [TestMethod] 28 | public void HeapTree_Deserialize_SmallTest() 29 | { 30 | var tree = new HeapTree(); 31 | 32 | var list = new List() { 2, 3, 1 }; 33 | var result = tree.CreateTree(list).Result; 34 | 35 | Assert.AreEqual(3, result.Data); 36 | Assert.AreEqual(2, result.LeftNode.Data); 37 | Assert.AreEqual(1, result.RightNode.Data); 38 | } 39 | 40 | [TestMethod] 41 | public void HeapTree_Insert_AtTop() 42 | { 43 | var heapTree = new HeapTree(); 44 | 45 | var list = new List() { 2, 4, 3, 1 }; 46 | var tree = heapTree.CreateTree(list).Result; 47 | 48 | Assert.AreEqual(4, tree.Data); 49 | Assert.AreEqual(2, tree.LeftNode.Data); 50 | Assert.AreEqual(1, tree.LeftNode.LeftNode.Data); 51 | Assert.AreEqual(3, tree.RightNode.Data); 52 | 53 | tree = heapTree.Insert(tree, 5).Result; 54 | Assert.AreEqual(5, tree.Data); 55 | Assert.AreEqual(4, tree.LeftNode.Data); 56 | Assert.AreEqual(1, tree.LeftNode.LeftNode.Data); 57 | Assert.AreEqual(2, tree.LeftNode.RightNode.Data); 58 | Assert.AreEqual(3, tree.RightNode.Data); 59 | 60 | } 61 | 62 | [TestMethod] 63 | public void HeapTree_Insert_AtMiddle() 64 | { 65 | var heapTree = new HeapTree(); 66 | 67 | var list = new List() { 2, 4, 6 }; 68 | var tree = heapTree.CreateTree(list).Result; 69 | 70 | Assert.AreEqual(6, tree.Data); 71 | Assert.AreEqual(4, tree.LeftNode.Data); 72 | Assert.AreEqual(2, tree.RightNode.Data); 73 | 74 | tree = heapTree.Insert(tree, 5).Result; 75 | Assert.AreEqual(6, tree.Data); 76 | Assert.AreEqual(5, tree.LeftNode.Data); 77 | Assert.AreEqual(4, tree.LeftNode.LeftNode.Data); 78 | Assert.AreEqual(2, tree.RightNode.Data); 79 | 80 | } 81 | 82 | //[TestMethod] 83 | //public void HeapTree_Serialize() 84 | //{ 85 | // var heapTree = new HeapTree(); 86 | // var list = new List() { 2, 4, 6 }; 87 | // var tree = heapTree.Deserialize(list).Result; 88 | // list = heapTree.Serialize(tree).Result; 89 | // Assert.AreEqual(6, list[0]); 90 | // Assert.AreEqual(4, list[1]); 91 | // Assert.AreEqual(2, list[2]); 92 | // tree = heapTree.Deserialize(list).Result; 93 | // Assert.AreEqual(6, tree.Data); 94 | // Assert.AreEqual(4, tree.LeftNode.Data); 95 | // Assert.AreEqual(2, tree.RightNode.Data); 96 | //} 97 | 98 | [TestMethod] 99 | public void HeapTree_Search() 100 | { 101 | var heapTree = new HeapTree(); 102 | 103 | var list = new List() { 2, 4, 6 }; 104 | var tree = heapTree.CreateTree(list).Result; 105 | 106 | var item = heapTree.Search(tree, 2).Result; 107 | Assert.AreEqual(2, item.Data); 108 | } 109 | 110 | 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /BasicAlgorithms.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27130.2024 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasicAlgorithms.Tests", "BasicAlgorithms.Tests\BasicAlgorithms.Tests.csproj", "{632D9DBB-748E-4252-8FE8-7047689A3857}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasicAlgorithms", "BasicAlgorithms\BasicAlgorithms.csproj", "{D46AA58F-B4AB-48A4-B5D5-8A41137EEFD2}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {632D9DBB-748E-4252-8FE8-7047689A3857}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {632D9DBB-748E-4252-8FE8-7047689A3857}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {632D9DBB-748E-4252-8FE8-7047689A3857}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {632D9DBB-748E-4252-8FE8-7047689A3857}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {D46AA58F-B4AB-48A4-B5D5-8A41137EEFD2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {D46AA58F-B4AB-48A4-B5D5-8A41137EEFD2}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {D46AA58F-B4AB-48A4-B5D5-8A41137EEFD2}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {D46AA58F-B4AB-48A4-B5D5-8A41137EEFD2}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {A674CB4B-FCFA-4539-9A7F-D0041A3800C3} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/ArraySearchFactory.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.DataProviders.Models; 2 | using BasicAlgorithms.Arrays.SearchAlgorithms; 3 | using BasicAlgorithms.Arrays.SearchAlgorithms.Interfaces; 4 | using BasicAlgorithms.Arrays.SearchAlgorithms.Models; 5 | using System; 6 | 7 | namespace BasicAlgorithms.Arrays; 8 | 9 | public class ArraySearchFactory 10 | { 11 | public int SampleSize { get; } 12 | 13 | public ArraySearchFactory(int sample) 14 | { 15 | SampleSize = sample; 16 | } 17 | 18 | public SearchResults Estimate(EnumArraysSearchAlgorithms searchAlgorithm, EnumArrayDataProviders searchDataProvider) 19 | { 20 | var _search = GetSearch(searchAlgorithm); 21 | var _searchData = new DataProvidersFactory(SampleSize).GetProvider(searchDataProvider); 22 | 23 | var searchResults = new SearchResults() 24 | { 25 | ArrayCount = _searchData.Data.Count 26 | }; 27 | 28 | searchResults.MinValue = _search.Find(_searchData.Data, _searchData.MinValue); 29 | searchResults.AvgValue = _search.Find(_searchData.Data, _searchData.AvgValue); 30 | searchResults.MaxValue = _search.Find(_searchData.Data, _searchData.MaxValue); 31 | searchResults.RandomValue = _search.Find(_searchData.Data, _searchData.RandomValue); 32 | searchResults.NotFoundValue = _search.Find(_searchData.Data, _searchData.NotFoundValue); 33 | 34 | return searchResults; 35 | } 36 | 37 | private static ISearch GetSearch(EnumArraysSearchAlgorithms searchAlgorithm) 38 | { 39 | return searchAlgorithm switch 40 | { 41 | EnumArraysSearchAlgorithms.Linear => new LinearSearch(), 42 | EnumArraysSearchAlgorithms.Jump => new JumpSearch(), 43 | EnumArraysSearchAlgorithms.Binary => new BinarySearch(), 44 | EnumArraysSearchAlgorithms.Interpolation => new InterpolationSearch(), 45 | _ => throw new NotImplementedException("Unknown algorithm '" + nameof(searchAlgorithm) + "'"), 46 | }; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/ArraySortFactory.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.DataProviders.Models; 2 | using BasicAlgorithms.Arrays.SortingAlgorithms; 3 | using BasicAlgorithms.Arrays.SortingAlgorithms.Interfaces; 4 | using BasicAlgorithms.Arrays.SortingAlgorithms.Models; 5 | using System; 6 | 7 | namespace BasicAlgorithms.Arrays; 8 | 9 | public class ArraySortFactory 10 | { 11 | public int SampleSize { get; } 12 | 13 | public ArraySortFactory(int sample) 14 | { 15 | SampleSize = sample; 16 | } 17 | 18 | public SortResults Estimate(EnumArraySortAlgorithms sortAlgorithm, EnumArrayDataProviders searchDataProvider) 19 | { 20 | var _sort = GetSort(sortAlgorithm); 21 | var _searchData = new DataProvidersFactory(SampleSize).GetProvider(searchDataProvider); 22 | 23 | return _sort.Sort(_searchData.Data); 24 | } 25 | 26 | private static ISort GetSort(EnumArraySortAlgorithms sortAlgorithm) 27 | { 28 | return sortAlgorithm switch 29 | { 30 | EnumArraySortAlgorithms.Insertion => new InsertionSort(), 31 | EnumArraySortAlgorithms.Selection => new SelectionSort(), 32 | EnumArraySortAlgorithms.Bubble => new BubbleSort(), 33 | EnumArraySortAlgorithms.Heap => new HeapSort(), 34 | EnumArraySortAlgorithms.Merge => new MergeSort(), 35 | EnumArraySortAlgorithms.Quick => new QuickSort(), 36 | _ => throw new NotImplementedException("Unknown algorithm '" + nameof(sortAlgorithm) + "'"), 37 | }; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/DataProviders/Interfaces/IArrayDataProvider.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace BasicAlgorithms.Arrays.DataProviders.Interfaces; 4 | 5 | public interface IArrayDataProvider 6 | { 7 | int MinValue { get; } 8 | int AvgValue { get; } 9 | int RandomValue { get; } 10 | int MaxValue { get; } 11 | int NotFoundValue { get; } 12 | List Data { get; } 13 | } 14 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/DataProviders/Models/EnumArrayDataProviders.cs: -------------------------------------------------------------------------------- 1 | namespace BasicAlgorithms.Arrays.DataProviders.Models; 2 | 3 | public enum EnumArrayDataProviders { SortedAndUniform, Sorted, Unsorted, ReverseSorted } 4 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/DataProviders/Providers/ReverseSortedProvider.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.DataProviders.Interfaces; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace BasicAlgorithms.Arrays.DataProviders.Providers; 7 | 8 | public class ReverseSortedProvider : IArrayDataProvider 9 | { 10 | public int MinValue { get; private set; } 11 | public int MaxValue { get; private set; } 12 | public int AvgValue { get; private set; } 13 | public int RandomValue { get; private set; } 14 | public int NotFoundValue { get; private set; } 15 | public List Data { get; private set; } 16 | 17 | /// 18 | /// Provides a list of sorted but not uniformely distributed data 19 | /// 20 | /// Size of the list 21 | public ReverseSortedProvider(int size) 22 | { 23 | Data = new List(); 24 | for (var i = size; i > 0; i--) 25 | { 26 | Data.Add(i); 27 | } 28 | 29 | MinValue = Data.Min(); 30 | MaxValue = Data.Max(); 31 | AvgValue = Data.Where(x => x > (MinValue + MaxValue) / 2).OrderBy(x => x).First(); 32 | RandomValue = Data[new Random((int)DateTime.Now.Ticks).Next(0, Data.Count - 1)]; 33 | NotFoundValue = Data.Max() + 1; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/DataProviders/Providers/SortedAndUniformProvider.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.DataProviders.Interfaces; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace BasicAlgorithms.Arrays.DataProviders.Providers; 7 | 8 | public class SortedAndUniformProvider : IArrayDataProvider 9 | { 10 | public int MinValue { get; private set; } 11 | public int MaxValue { get; private set; } 12 | public int AvgValue { get; private set; } 13 | public int RandomValue { get; private set; } 14 | public int NotFoundValue { get; private set; } 15 | public List Data { get; private set; } 16 | 17 | /// 18 | /// Provides a list of sorted and uniformly distributed integerss 19 | /// 20 | /// ize of the list 21 | public SortedAndUniformProvider(int size) 22 | { 23 | Data = Enumerable.Range(1, size).ToList(); 24 | 25 | MinValue = Data[0]; 26 | MaxValue = Data[size - 1]; 27 | AvgValue = Data.First(x => x > (MinValue + MaxValue) / 2); 28 | RandomValue = Data[new Random((int)DateTime.Now.Ticks).Next(0, Data.Count - 1)]; 29 | NotFoundValue = Data.Max() + 1; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/DataProviders/Providers/SortedProvider.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.DataProviders.Interfaces; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace BasicAlgorithms.Arrays.DataProviders.Providers; 7 | 8 | public class SortedProvider : IArrayDataProvider 9 | { 10 | public int MinValue { get; private set; } 11 | public int MaxValue { get; private set; } 12 | public int AvgValue { get; private set; } 13 | public int RandomValue { get; private set; } 14 | public int NotFoundValue { get; private set; } 15 | public List Data { get; private set; } 16 | 17 | /// 18 | /// Provides a list of sorted but not uniformely distributed data 19 | /// 20 | /// Size of the list 21 | public SortedProvider(int size) 22 | { 23 | Data = new List(); 24 | for (var i = 0; i < size; i++) 25 | { 26 | 27 | if (i < size / 2) 28 | { 29 | Data.Add(i); 30 | } 31 | else 32 | { 33 | Data.Add(int.MaxValue - i); 34 | } 35 | } 36 | 37 | MinValue = Data[0]; 38 | MaxValue = Data[size - 1]; 39 | AvgValue = Data.First(x => x > (MinValue + MaxValue) / 2); 40 | RandomValue = Data[new Random((int)DateTime.Now.Ticks).Next(0, Data.Count - 1)]; 41 | NotFoundValue = Data.Max() + 1; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/DataProviders/Providers/UnsortedProvider.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.DataProviders.Interfaces; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace BasicAlgorithms.Arrays.DataProviders.Providers; 7 | 8 | public class UnsortedProvider : IArrayDataProvider 9 | { 10 | public int MinValue { get; private set; } 11 | public int MaxValue { get; private set; } 12 | public int AvgValue { get; private set; } 13 | public int RandomValue { get; private set; } 14 | public int NotFoundValue { get; private set; } 15 | public List Data { get; private set; } 16 | 17 | /// 18 | /// Provides a list of random integers 19 | /// 20 | /// Size of the list 21 | public UnsortedProvider(int size) 22 | { 23 | Data = new List(); 24 | var rnd = new Random(1); 25 | for (var i = 0; i < size; i++) 26 | { 27 | Data.Add(rnd.Next(0, size * 10)); 28 | } 29 | 30 | MinValue = Data.Min(); 31 | MaxValue = Data.Max(); 32 | AvgValue = Data.OrderBy(x => x).First(x => x > (MinValue + MaxValue) / 2); 33 | RandomValue = Data[new Random((int)DateTime.Now.Ticks).Next(0, Data.Count - 1)]; 34 | NotFoundValue = Data.Max() + 1; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/DataProvidersFactory.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.DataProviders.Interfaces; 2 | using BasicAlgorithms.Arrays.DataProviders.Models; 3 | using BasicAlgorithms.Arrays.DataProviders.Providers; 4 | using System; 5 | 6 | namespace BasicAlgorithms.Arrays; 7 | 8 | public class DataProvidersFactory 9 | { 10 | public int SampleSize { get; } 11 | public DataProvidersFactory(int sampleSize) 12 | { 13 | SampleSize = sampleSize; 14 | } 15 | public IArrayDataProvider GetProvider(EnumArrayDataProviders arrayDataProvider) 16 | { 17 | return arrayDataProvider switch 18 | { 19 | EnumArrayDataProviders.SortedAndUniform => new SortedAndUniformProvider(SampleSize), 20 | EnumArrayDataProviders.Sorted => new SortedProvider(SampleSize), 21 | EnumArrayDataProviders.Unsorted => new UnsortedProvider(SampleSize), 22 | EnumArrayDataProviders.ReverseSorted => new SortedAndUniformProvider(SampleSize), 23 | _ => throw new NotImplementedException("Unknown data provider '" + nameof(arrayDataProvider) + "'"), 24 | }; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/SearchAlgorithms/BinarySearch.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SearchAlgorithms.Interfaces; 2 | using BasicAlgorithms.Arrays.SearchAlgorithms.Models; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Arrays.SearchAlgorithms; 6 | 7 | public class BinarySearch : ISearch 8 | { 9 | /// 10 | /// Binary Search [Time: O(log(n)), Space: O(1)] 11 | /// 12 | /// The array to search 13 | /// The value to search 14 | /// SearchResult object with statistics 15 | public SearchResult Find(List data, int value) 16 | { 17 | var watch = System.Diagnostics.Stopwatch.StartNew(); 18 | var searchResult = new SearchResult(); 19 | 20 | var start = 0; 21 | var end = data.Count - 1; 22 | var middle = (end - start) / 2;//floor 23 | 24 | while (start < end) 25 | { 26 | if (data[middle] == value) 27 | { 28 | break; 29 | } 30 | 31 | if (value > data[middle]) 32 | { 33 | start = middle + 1; 34 | } 35 | else 36 | { 37 | end = middle - 1; 38 | } 39 | 40 | middle = start + ((end - start) / 2); 41 | } 42 | 43 | watch.Stop(); 44 | searchResult.Ticks = watch.ElapsedTicks; 45 | 46 | if (data[middle] == value) 47 | { 48 | searchResult.PositionFound = middle; 49 | } 50 | 51 | return searchResult; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/SearchAlgorithms/Interfaces/ISearch.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SearchAlgorithms.Models; 2 | using System.Collections.Generic; 3 | 4 | namespace BasicAlgorithms.Arrays.SearchAlgorithms.Interfaces; 5 | 6 | public interface ISearch 7 | { 8 | SearchResult Find(List data, int value); 9 | } 10 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/SearchAlgorithms/InterpolationSearch.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SearchAlgorithms.Interfaces; 2 | using BasicAlgorithms.Arrays.SearchAlgorithms.Models; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Arrays.SearchAlgorithms; 6 | 7 | public class InterpolationSearch : ISearch 8 | { 9 | /// 10 | /// Interpolation Search [Time: O(log(log(n))), Space: O(1)] 11 | /// 12 | /// The array to search 13 | /// The value to search 14 | /// SearchResult object with statistics 15 | public SearchResult Find(List data, int value) 16 | { 17 | var watch = System.Diagnostics.Stopwatch.StartNew(); 18 | var searchResult = new SearchResult(); 19 | 20 | var start = 0; 21 | var end = data.Count - 1; 22 | int pos = -1; 23 | while (start <= end) 24 | { 25 | pos = (int)(start + (((double)(end - start) / (data[end] - data[start])) * (value - data[start]))); 26 | if (pos < 0 || pos > data.Count - 1) 27 | { 28 | pos = -1; 29 | break; 30 | } 31 | 32 | if (data[pos] == value) 33 | { 34 | break; 35 | } 36 | 37 | if (value > data[pos]) 38 | { 39 | start = pos + 1; 40 | } 41 | else 42 | { 43 | end = pos - 1; 44 | } 45 | } 46 | 47 | watch.Stop(); 48 | searchResult.Ticks = watch.ElapsedTicks; 49 | 50 | if (pos >= 0 && data[pos] == value) 51 | { 52 | searchResult.PositionFound = pos; 53 | } 54 | 55 | return searchResult; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/SearchAlgorithms/JumpSearch.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SearchAlgorithms.Interfaces; 2 | using BasicAlgorithms.Arrays.SearchAlgorithms.Models; 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | namespace BasicAlgorithms.Arrays.SearchAlgorithms; 7 | 8 | public class JumpSearch : ISearch 9 | { 10 | /// 11 | /// Jump Search [Time: O(sqrt(n)), Space: O(sqrt(n))] 12 | /// 13 | /// The array to search 14 | /// The value to search 15 | /// SearchResult object with statistics 16 | public SearchResult Find(List data, int value) 17 | { 18 | var watch = System.Diagnostics.Stopwatch.StartNew(); 19 | var searchResult = new SearchResult(); 20 | 21 | var length = data.Count; 22 | //first find block 23 | int blockSize = Convert.ToInt32(Math.Sqrt(length)); 24 | int block = 0; 25 | while (block < length / blockSize) 26 | { 27 | var blockUpperLimit = Math.Min((1 + block) * blockSize, length - 1); 28 | if (value <= data[blockUpperLimit])//we ve jsut passed it 29 | { 30 | break; 31 | } 32 | 33 | block++; 34 | } 35 | 36 | //then find block 37 | for (var i = block * blockSize; i < length; i++) 38 | { 39 | if (value == data[i]) 40 | { 41 | searchResult.PositionFound = i; 42 | break; 43 | } 44 | } 45 | 46 | watch.Stop(); 47 | searchResult.Ticks = watch.ElapsedTicks; 48 | 49 | return searchResult; 50 | 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/SearchAlgorithms/LinearSearch.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SearchAlgorithms.Interfaces; 2 | using BasicAlgorithms.Arrays.SearchAlgorithms.Models; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Arrays.SearchAlgorithms; 6 | 7 | public class LinearSearch : ISearch 8 | { 9 | /// 10 | /// Linear Search [Time: O(n), Space: O(1)] 11 | /// 12 | /// The array to search 13 | /// The value to search 14 | /// SearchResult object with statistics 15 | public SearchResult Find(List data, int value) 16 | { 17 | var searchResult = new SearchResult(); 18 | var watch = System.Diagnostics.Stopwatch.StartNew(); 19 | 20 | for (var i = 0; i < data.Count; i++) 21 | { 22 | if (data[i] == value) 23 | { 24 | searchResult.PositionFound = i; 25 | break; 26 | } 27 | } 28 | 29 | watch.Stop(); 30 | searchResult.Ticks = watch.ElapsedTicks; 31 | return searchResult; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/SearchAlgorithms/Models/EnumArraySearchAlgorithms.cs: -------------------------------------------------------------------------------- 1 | namespace BasicAlgorithms.Arrays.SearchAlgorithms.Models; 2 | 3 | public enum EnumArraysSearchAlgorithms { Linear, Jump, Binary, Interpolation } 4 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/SearchAlgorithms/Models/SearchResult.cs: -------------------------------------------------------------------------------- 1 | namespace BasicAlgorithms.Arrays.SearchAlgorithms.Models; 2 | 3 | public class SearchResult 4 | { 5 | public int? PositionFound { get; set; } 6 | public long Ticks { get; set; } 7 | } 8 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/SearchAlgorithms/Models/SearchResults.cs: -------------------------------------------------------------------------------- 1 | namespace BasicAlgorithms.Arrays.SearchAlgorithms.Models; 2 | 3 | public class SearchResults 4 | { 5 | public int ArrayCount { get; set; } 6 | public SearchResult MinValue { get; set; } = new SearchResult(); 7 | public SearchResult MaxValue { get; set; } = new SearchResult(); 8 | public SearchResult AvgValue { get; set; } = new SearchResult(); 9 | public SearchResult RandomValue { get; set; } = new SearchResult(); 10 | public SearchResult NotFoundValue { get; set; } = new SearchResult(); 11 | } 12 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/SortingAlgorithms/BubbleSort.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SortingAlgorithms.Interfaces; 2 | using BasicAlgorithms.Arrays.SortingAlgorithms.Models; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Arrays.SortingAlgorithms; 6 | 7 | public class BubbleSort : ISort 8 | { 9 | 10 | /// 11 | /// Bubble Sort Algorithm [Time: O(n^2), Space: O(1)] 12 | /// 13 | /// 14 | /// 15 | public SortResults Sort(List data) 16 | { 17 | var results = new SortResults(); 18 | var watch = System.Diagnostics.Stopwatch.StartNew(); 19 | 20 | for (var i = 0; i < data.Count; i++) 21 | { 22 | for (var j = 0; j < data.Count - 1; j++) 23 | { 24 | if (data[j] > data[j + 1]) 25 | { 26 | var tmp = data[j]; 27 | data[j] = data[j + 1]; 28 | data[j + 1] = tmp; 29 | } 30 | } 31 | } 32 | 33 | watch.Stop(); 34 | results.Ticks = watch.ElapsedTicks; 35 | 36 | results.SortedData = data; 37 | return results; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/SortingAlgorithms/HeapSort.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SortingAlgorithms.Interfaces; 2 | using BasicAlgorithms.Arrays.SortingAlgorithms.Models; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Arrays.SortingAlgorithms; 6 | 7 | public class HeapSort : ISort 8 | { 9 | 10 | /// 11 | /// Heap Sort Algorithm [Time: O(n*Logn), Space: O(n)] 12 | /// 13 | /// 14 | /// 15 | public SortResults Sort(List data) 16 | { 17 | var results = new SortResults(); 18 | var length = data.Count; 19 | 20 | var watch = System.Diagnostics.Stopwatch.StartNew(); 21 | 22 | //rearrange array to heap array sort 23 | for (var i = length / 2 - 1; i >= 0; i--) 24 | { 25 | Heapify(data, length, i); 26 | } 27 | 28 | //the largest element is first, move it to the end 29 | // reheapify everything (except the last) to bring the largest at the first position 30 | for (int i = length - 1; i >= 0; i--) 31 | { 32 | var tmp = data[0]; 33 | data[0] = data[i]; 34 | data[i] = tmp; 35 | 36 | Heapify(data, i, 0); 37 | } 38 | 39 | watch.Stop(); 40 | results.Ticks = watch.ElapsedTicks; 41 | results.SortedData = data; 42 | return results; 43 | } 44 | 45 | 46 | //space O(n) because of recursion, could be modified to a loop for O(1) complexity 47 | private void Heapify(List data, int length, int largest) 48 | { 49 | 50 | //assume largest and get left and right that should be smaller 51 | var new_largest = largest; 52 | var left = 2 * largest + 1;//left branch of heap tree 53 | var right = 2 * largest + 2;//right branch of heap tree 54 | 55 | //if left is larger, get that as the new largest 56 | if (left < length && data[left] > data[new_largest]) 57 | { 58 | new_largest = left; 59 | } 60 | 61 | //if right is larger, get that as the new largest 62 | if (right < length && data[right] > data[new_largest]) 63 | { 64 | new_largest = right; 65 | } 66 | 67 | //if there is a change in largest swap 68 | // and recurse with the new largest 69 | if (new_largest != largest) 70 | { 71 | var tmp = data[largest]; 72 | data[largest] = data[new_largest]; 73 | data[new_largest] = tmp; 74 | 75 | Heapify(data, length, new_largest); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/SortingAlgorithms/InsertionSort.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SortingAlgorithms.Interfaces; 2 | using BasicAlgorithms.Arrays.SortingAlgorithms.Models; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Arrays.SortingAlgorithms; 6 | 7 | public class InsertionSort : ISort 8 | { 9 | 10 | /// 11 | /// Insertion Sort Algorithm [Time: O(n^2), Space: O(1)] 12 | /// 13 | /// 14 | /// 15 | public SortResults Sort(List data) 16 | { 17 | var results = new SortResults(); 18 | 19 | var watch = System.Diagnostics.Stopwatch.StartNew(); 20 | for (var i = 1; i < data.Count; i++) 21 | { 22 | for (var j = i; j > 0; j--) 23 | { 24 | if (data[j - 1] > data[j]) 25 | { 26 | var tmp = data[j - 1]; 27 | data[j - 1] = data[j]; 28 | data[j] = tmp; 29 | } 30 | } 31 | } 32 | watch.Stop(); 33 | results.Ticks = watch.ElapsedTicks; 34 | 35 | results.SortedData = data; 36 | return results; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/SortingAlgorithms/Interfaces/ISort.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SortingAlgorithms.Models; 2 | using System.Collections.Generic; 3 | 4 | namespace BasicAlgorithms.Arrays.SortingAlgorithms.Interfaces; 5 | 6 | public interface ISort 7 | { 8 | SortResults Sort(List data); 9 | } 10 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/SortingAlgorithms/MergeSort.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SortingAlgorithms.Interfaces; 2 | using BasicAlgorithms.Arrays.SortingAlgorithms.Models; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Arrays.SortingAlgorithms; 6 | 7 | public class MergeSort : ISort 8 | { 9 | 10 | /// 11 | /// Merge Sort Algorithm [Time: O(n*logn), Space: O(n)] 12 | /// 13 | /// 14 | /// 15 | public SortResults Sort(List data) 16 | { 17 | var results = new SortResults(); 18 | var watch = System.Diagnostics.Stopwatch.StartNew(); 19 | 20 | HelperSort(data, 0, data.Count - 1); 21 | 22 | watch.Stop(); 23 | results.Ticks = watch.ElapsedTicks; 24 | 25 | results.SortedData = data; 26 | return results; 27 | } 28 | 29 | private void HelperSort(List data, int start, int end) 30 | { 31 | if (start < end) 32 | { 33 | var middle = (start + end) / 2; 34 | HelperSort(data, start, middle); 35 | HelperSort(data, middle + 1, end); 36 | HelperMerge(data, start, middle, end); 37 | } 38 | } 39 | 40 | private static void HelperMerge(List data, int start, int middle, int end) 41 | { 42 | //create the two arrays to merge 43 | var a1 = new List(); 44 | var a2 = new List(); 45 | for (var i = start; i <= middle; i++) 46 | { 47 | a1.Add(data[i]); 48 | } 49 | for (var i = middle + 1; i <= end; i++) 50 | { 51 | a2.Add(data[i]); 52 | } 53 | 54 | //loop through both arrays and check order 55 | var i1 = 0; 56 | var i2 = 0; 57 | var iData = start; 58 | while (i1 < a1.Count && i2 < a2.Count) 59 | { 60 | if (a1[i1] <= a2[i2]) 61 | { 62 | data[iData] = a1[i1]; 63 | i1++; 64 | } 65 | else 66 | { 67 | data[iData] = a2[i2]; 68 | i2++; 69 | } 70 | iData++; 71 | } 72 | 73 | //add any item that left out 74 | while (i1 < a1.Count) 75 | { 76 | data[iData++] = a1[i1++]; 77 | } 78 | 79 | while (i2 < a2.Count) 80 | { 81 | data[iData++] = a2[i2++]; 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/SortingAlgorithms/Models/EnumArraySortAlgorithms.cs: -------------------------------------------------------------------------------- 1 | namespace BasicAlgorithms.Arrays.SortingAlgorithms.Models; 2 | 3 | public enum EnumArraySortAlgorithms { Insertion, Selection, Bubble, Heap, Merge, Quick } 4 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/SortingAlgorithms/Models/SortResults.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace BasicAlgorithms.Arrays.SortingAlgorithms.Models; 4 | 5 | public class SortResults 6 | { 7 | public List SortedData { get; set; } 8 | public long Ticks { get; set; } 9 | } 10 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/SortingAlgorithms/QuickSort.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SortingAlgorithms.Interfaces; 2 | using BasicAlgorithms.Arrays.SortingAlgorithms.Models; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Arrays.SortingAlgorithms; 6 | 7 | public class QuickSort : ISort 8 | { 9 | 10 | /// 11 | /// Quick Sort Algorithm [Time: O(n*logn), Space: O(n)] 12 | /// 13 | /// 14 | /// 15 | public SortResults Sort(List data) 16 | { 17 | var results = new SortResults(); 18 | var watch = System.Diagnostics.Stopwatch.StartNew(); 19 | 20 | HelperSort(data, 0, data.Count - 1); 21 | 22 | watch.Stop(); 23 | results.Ticks = watch.ElapsedTicks; 24 | 25 | results.SortedData = data; 26 | return results; 27 | } 28 | 29 | private void HelperSort(List data, int start, int end) 30 | { 31 | if (start < end) 32 | { 33 | var middle = HelperPartition(data, start, end); 34 | HelperSort(data, start, middle - 1); 35 | HelperSort(data, middle + 1, end); 36 | } 37 | } 38 | 39 | private static int HelperPartition(List data, int start, int end) 40 | { 41 | //loop partition and swap with last of partition (last as pivot) 42 | for (var i = start; i < end; i++) 43 | { 44 | if (data[i] <= data[end]) 45 | { 46 | var tmp = data[start]; 47 | data[start] = data[i]; 48 | data[i] = tmp; 49 | start++;//move new pivot 50 | } 51 | } 52 | 53 | //replace new pivot item with last 54 | var tmp2 = data[start]; 55 | data[start] = data[end]; 56 | data[end] = tmp2; 57 | 58 | return start; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /BasicAlgorithms/Arrays/SortingAlgorithms/SelectionSort.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays.SortingAlgorithms.Interfaces; 2 | using BasicAlgorithms.Arrays.SortingAlgorithms.Models; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Arrays.SortingAlgorithms; 6 | 7 | public class SelectionSort : ISort 8 | { 9 | 10 | /// 11 | /// Selection Sort Algorithm [Time: O(n^2), Space: O(1)] 12 | /// 13 | /// 14 | /// 15 | public SortResults Sort(List data) 16 | { 17 | var results = new SortResults(); 18 | var watch = System.Diagnostics.Stopwatch.StartNew(); 19 | 20 | for (var i = 0; i < data.Count - 1; i++) 21 | { 22 | var max = i; 23 | for (var j = i + 1; j < data.Count; j++) 24 | { 25 | if (data[j] < data[max]) 26 | { 27 | max = j; 28 | } 29 | } 30 | if (max != i) 31 | { 32 | var tmp = data[max]; 33 | data[max] = data[i]; 34 | data[i] = tmp; 35 | } 36 | } 37 | 38 | watch.Stop(); 39 | results.Ticks = watch.ElapsedTicks; 40 | 41 | results.SortedData = data; 42 | return results; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /BasicAlgorithms/BasicAlgorithms.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /BasicAlgorithms/Practice/VariousProblems.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | 6 | namespace BasicAlgorithms.Practice; 7 | 8 | public class VariousProblems 9 | { 10 | 11 | /// 12 | /// Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. 13 | /// 14 | /// 15 | public static string LongestPalindrom(string s) 16 | { 17 | var start = 0; 18 | var end = 0; 19 | for (var i = 0; i < s.Length; i++) 20 | { 21 | var tmp = LongestPalindrom_CheckLength(s, i, i); 22 | var tmp2 = LongestPalindrom_CheckLength(s, i, i + 1); 23 | var max = Math.Max(tmp, tmp2); 24 | if (max > end - start) 25 | { 26 | start = i - (max - 1) / 2; 27 | end = i + max / 2; 28 | } 29 | } 30 | return s.Substring(start, end - start + 1); 31 | } 32 | private static int LongestPalindrom_CheckLength(string s, int left, int right) 33 | { 34 | var _left = left; 35 | var _right = right; 36 | 37 | while (_left >= 0 && _right < s.Length && s.Substring(_left, 1) == s.Substring(_right, 1)) 38 | { 39 | _left--; 40 | _right++; 41 | } 42 | //if (_left <= 0) 43 | // _left = 0; 44 | //else 45 | // _left++; 46 | 47 | //_right = Math.Min(_right, s.Length); 48 | return _right - _left - 1;// - (right - left); 49 | } 50 | 51 | /// 52 | /// Given a Matrix of size M x N. Your task is to print the matrix K times left rotated. 53 | /// https://practice.geeksforgeeks.org/problems/left-rotate-matrix-k-times/0 54 | /// 55 | 56 | public static List LeftRotateMatrix(int M, int N, int K, List data, int k = 1) 57 | { 58 | 59 | var rotated = new List(); 60 | while (data.Count > 0) 61 | { 62 | var tmp = new List(); 63 | for (var i = N - 1; i >= 0; i--) 64 | { 65 | tmp.Add(data[i]); 66 | data.Remove(data[i]); 67 | } 68 | rotated.AddRange(tmp); 69 | 70 | } 71 | 72 | if(k == K) 73 | { 74 | return rotated; 75 | } 76 | 77 | return LeftRotateMatrix(M, N, K, rotated, ++k); 78 | 79 | 80 | } 81 | 82 | /// 83 | /// Given two integers ‘L’ and ‘R’, write a program that finds the count of numbers having prime number of set bits in their binary representation in the range [L, R]. 84 | /// https://practice.geeksforgeeks.org/problems/prime-number-of-set-bits/0 85 | /// 86 | public static int PrimeNumberSets(int L, int R) 87 | { 88 | var primesCount = 0; 89 | for (var i = L; i <= R; i++) 90 | { 91 | var setBitsCount = Convert.ToString(i, 2).Where(x => x == '1').Count(); 92 | if (setBitsCount == 1) 93 | { 94 | continue; 95 | } 96 | 97 | if (setBitsCount == 2 || setBitsCount == 3) 98 | { 99 | primesCount++; 100 | continue; 101 | } 102 | 103 | var root = (int)Math.Sqrt(setBitsCount); 104 | var isPrime = true; 105 | for (var j = 2; j <= root; j++) 106 | { 107 | if (setBitsCount % j == 0) 108 | { 109 | isPrime = false; 110 | break; 111 | } 112 | } 113 | if (isPrime) 114 | { 115 | primesCount++; 116 | } 117 | } 118 | 119 | return primesCount; 120 | 121 | } 122 | 123 | /// 124 | /// https://practice.geeksforgeeks.org/problems/find-k-th-character-in-string/0 125 | /// 126 | public static int FindCharacterBinary(int m, int k, int n) 127 | { 128 | var result = Convert.ToString(m, 2); 129 | for (var i = 0; i < n; i++) 130 | { 131 | var iteration = ""; 132 | foreach (var c in result) 133 | { 134 | iteration += c == '0' ? "01" : "10"; 135 | } 136 | result = iteration; 137 | } 138 | return Convert.ToInt32(result.Substring(k, 1)); 139 | } 140 | 141 | /// 142 | /// https://practice.geeksforgeeks.org/problems/maximum-tip-calculator/0 143 | /// N=orders[0], X=orders[1], Y=orders[2] 144 | /// 145 | public static int MaximumTipCalculator(List orders, List tipsA, List tipsB) 146 | { 147 | var countOrders = 0; 148 | var countA = 0; 149 | var countB = 0; 150 | var sumOrders = 0; 151 | for (var i = 0; i < tipsA.Count; i++) 152 | { 153 | if (countOrders >= orders[0]) 154 | { 155 | break; 156 | } 157 | 158 | if (countA <= orders[1] && tipsA[i] >= tipsB[i]) 159 | { 160 | sumOrders += tipsA[i]; 161 | countA++; 162 | countOrders++; 163 | continue; 164 | } 165 | if (countB <= orders[2] && tipsA[i] <= tipsB[i]) 166 | { 167 | sumOrders += tipsB[i]; 168 | countB++; 169 | countOrders++; 170 | continue; 171 | } 172 | } 173 | 174 | return sumOrders; 175 | } 176 | 177 | /// 178 | /// Given an array of n integers(duplicates allowed). Print “Yes” if it is a set of contiguous integers else print “No”. 179 | /// https://practice.geeksforgeeks.org/problems/check-if-array-contains-contiguous-integers-with-duplicates-allowed/0 180 | /// 181 | public static bool ArrayContiguousIntegers(List data) 182 | { 183 | data.Sort(); 184 | var list = data.Distinct().ToList(); 185 | return list[^1] == list[0] + list.Count - 1; 186 | 187 | } 188 | 189 | /// 190 | /// Given an array of distinct integers, print all the pairs having positive value and negative value of a number that exists in the array. 191 | /// https://practice.geeksforgeeks.org/problems/pairs-with-positive-negative-values/0 192 | /// 193 | public static List OrderByAbsoluteOrder(List data) 194 | { 195 | var dup = new List(); 196 | 197 | while (data.Count > 0) 198 | { 199 | var i = data.First(); 200 | data.Remove(i); 201 | 202 | for (var j = 0; j < data.Count; j++) 203 | { 204 | if (i == -1 * data[j]) 205 | { 206 | int k; 207 | for (k = 0; k < dup.Count; k += 2) 208 | { 209 | if (Math.Abs(i) < Math.Abs(dup[k])) 210 | { 211 | break; 212 | } 213 | } 214 | if (data[j] > i) 215 | { 216 | dup.Insert(k, data[j]); 217 | dup.Insert(k, i); 218 | } 219 | else 220 | { 221 | dup.Insert(k, i); 222 | dup.Insert(k, data[j]); 223 | } 224 | 225 | data.Remove(data[j]); 226 | break; 227 | } 228 | } 229 | } 230 | return dup; 231 | } 232 | } 233 | -------------------------------------------------------------------------------- /BasicAlgorithms/Program.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays; 2 | using BasicAlgorithms.Trees; 3 | using BasicAlgorithms.UI; 4 | using System; 5 | 6 | namespace BasicAlgorithms; 7 | 8 | internal class Program 9 | { 10 | private static void Main(string[] args) 11 | { 12 | Home: 13 | ConsoleKeyInfo keyInfo; 14 | do 15 | { 16 | keyInfo = Home(); 17 | } while (keyInfo.KeyChar is not '1' and not '2' and not '3' and not '4'); 18 | 19 | if (keyInfo.KeyChar == '1') 20 | { 21 | SortScreen(); 22 | } 23 | 24 | if (keyInfo.KeyChar == '2') 25 | { 26 | SearchScreen(); 27 | } 28 | 29 | if (keyInfo.KeyChar == '3') 30 | { 31 | TreeScreen(); 32 | } 33 | 34 | if (keyInfo.KeyChar == '4') 35 | { 36 | TraversalScreen(); 37 | } 38 | 39 | goto Home; 40 | } 41 | 42 | private static ConsoleKeyInfo Home() 43 | { 44 | Console.Clear(); 45 | Console.WriteLine("Hello! Please choose:"); 46 | Console.WriteLine("1: Sort Algorithms"); 47 | Console.WriteLine("2: Search Algorithms"); 48 | Console.WriteLine("3: Tree Algorithms"); 49 | Console.WriteLine("4: Traversal Algorithms"); 50 | return Console.ReadKey(true); 51 | } 52 | 53 | private static ConsoleKeyInfo TraversalScreen() 54 | { 55 | Console.Clear(); 56 | new TraversalUI( 57 | new TraversalFactory(6), 58 | 90 59 | ).Print(); 60 | 61 | Console.WriteLine(); 62 | Console.WriteLine("Press any key for home screen!"); 63 | 64 | return Console.ReadKey(true); 65 | } 66 | 67 | private static ConsoleKeyInfo TreeScreen() 68 | { 69 | Console.Clear(); 70 | new TreeUI( 71 | new TreeFactory(1000000), 72 | 90 73 | ).Print(); 74 | 75 | Console.WriteLine(); 76 | Console.WriteLine("Press any key for home screen!"); 77 | 78 | return Console.ReadKey(true); 79 | } 80 | 81 | private static ConsoleKeyInfo SortScreen() 82 | { 83 | Console.Clear(); 84 | new ArraySortUI( 85 | new ArraySortFactory(10000), 86 | 90 87 | ).Print(); 88 | 89 | Console.WriteLine(); 90 | Console.WriteLine("Press any key for home screen!"); 91 | 92 | return Console.ReadKey(true); 93 | } 94 | 95 | private static ConsoleKeyInfo SearchScreen() 96 | { 97 | Console.Clear(); 98 | new ArraySearchUI( 99 | new ArraySearchFactory(1000000), 100 | 90 101 | ).Print(); 102 | 103 | Console.WriteLine(); 104 | Console.WriteLine("Press any key for home screen!"); 105 | 106 | return Console.ReadKey(true); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/DataProviders/Interfaces/ITreeDataProvider.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees.TreeAlgorithms.Models; 2 | using System.Collections.Generic; 3 | 4 | namespace BasicAlgorithms.Trees.DataProviders.Interfaces; 5 | 6 | public interface ITreeDataProvider 7 | { 8 | int AvgValue { get; } 9 | int MaxValue { get; } 10 | int MinValue { get; } 11 | int NotFoundValue { get; } 12 | int RandomValue { get; } 13 | List Data { get; } 14 | BinaryTree Tree { get; } 15 | } 16 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/DataProviders/Models/EnumTreeProvider.cs: -------------------------------------------------------------------------------- 1 | namespace BasicAlgorithms.Trees.DataProviders.Models; 2 | 3 | public enum EnumTreeDataProvider { Heap, BST } 4 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/DataProviders/Providers/TreeDataProvider.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees.DataProviders.Interfaces; 2 | using BasicAlgorithms.Trees.TreeAlgorithms.Interfaces; 3 | using BasicAlgorithms.Trees.TreeAlgorithms.Models; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | 8 | namespace BasicAlgorithms.Trees.DataProviders.Providers; 9 | 10 | public class TreeDataProvider : ITreeDataProvider 11 | { 12 | public int MinValue { get; private set; } 13 | public int MaxValue { get; private set; } 14 | public int AvgValue { get; private set; } 15 | public int RandomValue { get; private set; } 16 | public int NotFoundValue { get; private set; } 17 | public BinaryTree Tree { get; private set; } 18 | public List Data { get; private set; } 19 | 20 | /// 21 | /// Provides a list of sorted but not uniformely distributed data 22 | /// 23 | /// Size of the list 24 | public TreeDataProvider(ITree tree, int size) 25 | { 26 | Data = new List(); 27 | for (var i = size; i > 0; i--) 28 | { 29 | Data.Add(i); 30 | } 31 | 32 | MinValue = Data.Min(); 33 | MaxValue = Data.Max(); 34 | AvgValue = Data.Where(x => x > (MinValue + MaxValue) / 2).OrderBy(x => x).First(); 35 | RandomValue = Data[new Random((int)DateTime.Now.Ticks).Next(0, Data.Count - 1)]; 36 | NotFoundValue = Data.Max() + 1; 37 | 38 | Tree = tree.CreateTree(Data).Result; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/DataProvidersFactory.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees.DataProviders.Interfaces; 2 | using BasicAlgorithms.Trees.DataProviders.Models; 3 | using BasicAlgorithms.Trees.DataProviders.Providers; 4 | using BasicAlgorithms.Trees.TreeAlgorithms.TypedTrees; 5 | using System; 6 | 7 | namespace BasicAlgorithms.Trees; 8 | 9 | public class DataProvidersFactory 10 | { 11 | public int SampleSize { get; } 12 | public DataProvidersFactory(int sampleSize) 13 | { 14 | SampleSize = sampleSize; 15 | } 16 | 17 | public ITreeDataProvider GetProvider(EnumTreeDataProvider treeDataProvider) 18 | { 19 | return treeDataProvider switch 20 | { 21 | EnumTreeDataProvider.Heap => new TreeDataProvider(new HeapTree(), SampleSize), 22 | EnumTreeDataProvider.BST => new TreeDataProvider(new BinarySearchTree(), SampleSize), 23 | _ => throw new NotImplementedException("Unknown data provider '" + nameof(treeDataProvider) + "'"), 24 | }; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/TraversalFactory.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees.DataProviders.Interfaces; 2 | using BasicAlgorithms.Trees.DataProviders.Models; 3 | using BasicAlgorithms.Trees.TreeAlgorithms.Models; 4 | using BasicAlgorithms.Trees.TreeAlgorithms.Traversals; 5 | using System; 6 | 7 | namespace BasicAlgorithms.Trees; 8 | 9 | public class TraversalFactory 10 | { 11 | public int SampleSize { get; } 12 | public TraversalFactory(int sample) 13 | { 14 | SampleSize = sample; 15 | } 16 | 17 | public TraversalResults Traverse(EnumTreeTypes treeType) 18 | { 19 | //var _tree = GetTree(treeType); 20 | var treeData = GetTreeData(treeType); 21 | 22 | return new TraversalResults() 23 | { 24 | Tree = treeData.Tree, 25 | BreadthFirstResult = new BreadthFirstTraversal().Traverse(treeData.Tree), 26 | InOrderResult = new InOrderTraversal().Traverse(treeData.Tree), 27 | PostOrderResult = new PostOrderTraversal().Traverse(treeData.Tree), 28 | PreOrderResult = new PreOrderTraversal().Traverse(treeData.Tree), 29 | }; 30 | } 31 | 32 | private ITreeDataProvider GetTreeData(EnumTreeTypes treeType) 33 | { 34 | return treeType switch 35 | { 36 | EnumTreeTypes.Heap => new DataProvidersFactory(SampleSize).GetProvider(EnumTreeDataProvider.Heap), 37 | EnumTreeTypes.BST => new DataProvidersFactory(SampleSize).GetProvider(EnumTreeDataProvider.BST), 38 | _ => throw new NotImplementedException("Unknown tree type '" + nameof(treeType) + "'"), 39 | }; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/TreeAlgorithms/Interfaces/ITraversals.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees.TreeAlgorithms.Models; 2 | using System.Collections.Generic; 3 | 4 | namespace BasicAlgorithms.Trees.TreeAlgorithms.Interfaces; 5 | 6 | public interface ITraversals 7 | { 8 | BinaryTree UnTraverse(List data); 9 | List Traverse(BinaryTree tree); 10 | } 11 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/TreeAlgorithms/Interfaces/ITree.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees.TreeAlgorithms.Models; 2 | using System.Collections.Generic; 3 | 4 | namespace BasicAlgorithms.Trees.TreeAlgorithms.Interfaces; 5 | 6 | public interface ITree 7 | { 8 | BinaryTreeResults CreateTree(List data); 9 | BinaryTreeResults Insert(BinaryTree tree, int item); 10 | BinaryTreeResults Search(BinaryTree tree, int item); 11 | } 12 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/TreeAlgorithms/Models/BinaryTree.cs: -------------------------------------------------------------------------------- 1 | namespace BasicAlgorithms.Trees.TreeAlgorithms.Models; 2 | 3 | public class BinaryTree 4 | { 5 | public int Data { get; set; } 6 | public BinaryTree LeftNode { get; set; } 7 | public BinaryTree RightNode { get; set; } 8 | } 9 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/TreeAlgorithms/Models/BinaryTreeEstimation.cs: -------------------------------------------------------------------------------- 1 | namespace BasicAlgorithms.Trees.TreeAlgorithms.Models; 2 | 3 | public class BinaryTreeEstimation 4 | { 5 | public BinaryTreeResults Deserialize { get; set; } 6 | public BinaryTreeResults Search { get; set; } 7 | public BinaryTreeResults Insert { get; set; } 8 | } 9 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/TreeAlgorithms/Models/BinaryTreeResults.cs: -------------------------------------------------------------------------------- 1 | namespace BasicAlgorithms.Trees.TreeAlgorithms.Models; 2 | 3 | public class BinaryTreeResults 4 | { 5 | public T Result { get; set; } 6 | public long Ticks { get; set; } 7 | } -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/TreeAlgorithms/Models/EnumTraversalTypes.cs: -------------------------------------------------------------------------------- 1 | namespace BasicAlgorithms.Trees.TreeAlgorithms.Models; 2 | 3 | public enum EnumTraversalTypes { BreadthFirst, InOrder, PreOrder, PostOrder } 4 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/TreeAlgorithms/Models/EnumTreeTypes.cs: -------------------------------------------------------------------------------- 1 | namespace BasicAlgorithms.Trees.TreeAlgorithms.Models; 2 | 3 | public enum EnumTreeTypes { Heap, BST } 4 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/TreeAlgorithms/Models/TraversalResults.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace BasicAlgorithms.Trees.TreeAlgorithms.Models; 4 | 5 | public class TraversalResults 6 | { 7 | public List BreadthFirstResult { get; set; } 8 | public List InOrderResult { get; set; } 9 | public List PostOrderResult { get; set; } 10 | public List PreOrderResult { get; set; } 11 | public BinaryTree Tree { get; set; } 12 | } 13 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/TreeAlgorithms/Traversals/BreadthFirstTraversal.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees.TreeAlgorithms.Interfaces; 2 | using BasicAlgorithms.Trees.TreeAlgorithms.Models; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | namespace BasicAlgorithms.Trees.TreeAlgorithms.Traversals; 8 | 9 | public class BreadthFirstTraversal : ITraversals 10 | { 11 | 12 | public BinaryTree UnTraverse(List data) 13 | { 14 | throw new NotImplementedException(); 15 | } 16 | 17 | public List Traverse(BinaryTree tree) 18 | { 19 | var data = new List(); 20 | var queue = new Queue(); 21 | queue.Enqueue(tree); 22 | while (queue.Count > 0) 23 | { 24 | var node = queue.Dequeue(); 25 | data.Add(node.Data); 26 | if (node.LeftNode != null) 27 | { 28 | queue.Enqueue(node.LeftNode); 29 | } 30 | 31 | if (node.RightNode != null) 32 | { 33 | queue.Enqueue(node.RightNode); 34 | } 35 | } 36 | 37 | return data; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/TreeAlgorithms/Traversals/InOrderTraversal.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees.TreeAlgorithms.Interfaces; 2 | using BasicAlgorithms.Trees.TreeAlgorithms.Models; 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | namespace BasicAlgorithms.Trees.TreeAlgorithms.Traversals; 7 | 8 | public class InOrderTraversal : ITraversals 9 | { 10 | 11 | public BinaryTree UnTraverse(List data) 12 | { 13 | throw new NotImplementedException(); 14 | } 15 | 16 | public List Traverse(BinaryTree tree) 17 | { 18 | var list = new List(); 19 | 20 | if (tree.LeftNode != null) 21 | { 22 | list.AddRange(Traverse(tree.LeftNode)); 23 | } 24 | 25 | list.Add(tree.Data); 26 | 27 | if (tree.RightNode != null) 28 | { 29 | list.AddRange(Traverse(tree.RightNode)); 30 | } 31 | 32 | return list; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/TreeAlgorithms/Traversals/PostOrderTraversal.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees.TreeAlgorithms.Interfaces; 2 | using BasicAlgorithms.Trees.TreeAlgorithms.Models; 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | namespace BasicAlgorithms.Trees.TreeAlgorithms.Traversals; 7 | 8 | public class PostOrderTraversal : ITraversals 9 | { 10 | 11 | public BinaryTree UnTraverse(List data) 12 | { 13 | throw new NotImplementedException(); 14 | } 15 | 16 | public List Traverse(BinaryTree tree) 17 | { 18 | var list = new List(); 19 | 20 | if (tree.LeftNode != null) 21 | { 22 | list.AddRange(Traverse(tree.LeftNode)); 23 | } 24 | 25 | if (tree.RightNode != null) 26 | { 27 | list.AddRange(Traverse(tree.RightNode)); 28 | } 29 | 30 | list.Add(tree.Data); 31 | 32 | return list; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/TreeAlgorithms/Traversals/PreOrderTraversal.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees.TreeAlgorithms.Interfaces; 2 | using BasicAlgorithms.Trees.TreeAlgorithms.Models; 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | namespace BasicAlgorithms.Trees.TreeAlgorithms.Traversals; 7 | 8 | public class PreOrderTraversal : ITraversals 9 | { 10 | 11 | public BinaryTree UnTraverse(List data) 12 | { 13 | throw new NotImplementedException(); 14 | } 15 | 16 | public List Traverse(BinaryTree tree) 17 | { 18 | var list = new List 19 | { 20 | tree.Data 21 | }; 22 | 23 | if (tree.LeftNode != null) 24 | { 25 | list.AddRange(Traverse(tree.LeftNode)); 26 | } 27 | 28 | if (tree.RightNode != null) 29 | { 30 | list.AddRange(Traverse(tree.RightNode)); 31 | } 32 | 33 | return list; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/TreeAlgorithms/TypedTrees/BinarySearchTree.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees.TreeAlgorithms.Interfaces; 2 | using BasicAlgorithms.Trees.TreeAlgorithms.Models; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Trees.TreeAlgorithms.TypedTrees; 6 | 7 | public class BinarySearchTree : ITree 8 | { 9 | 10 | public BinaryTreeResults CreateTree(List data) 11 | { 12 | var length = data.Count; 13 | var watch = System.Diagnostics.Stopwatch.StartNew(); 14 | data.Sort(); 15 | var tree = HelperCreateTree(data, 0, length - 1); 16 | watch.Stop(); 17 | 18 | return new BinaryTreeResults() 19 | { 20 | Result = tree, 21 | Ticks = watch.ElapsedTicks 22 | }; 23 | } 24 | 25 | public BinaryTreeResults Search(BinaryTree tree, int item) 26 | { 27 | var watch = System.Diagnostics.Stopwatch.StartNew(); 28 | var result = HelperSearch(tree, item); 29 | watch.Stop(); 30 | 31 | return new BinaryTreeResults() 32 | { 33 | Result = result, 34 | Ticks = watch.ElapsedTicks 35 | }; 36 | } 37 | 38 | public BinaryTreeResults Insert(BinaryTree tree, int item) 39 | { 40 | var watch = System.Diagnostics.Stopwatch.StartNew(); 41 | tree = HelperInsert(tree, item); 42 | watch.Stop(); 43 | 44 | return new BinaryTreeResults() 45 | { 46 | Result = tree, 47 | Ticks = watch.ElapsedTicks 48 | }; 49 | } 50 | 51 | private BinaryTree HelperCreateTree(List data, int start, int end) 52 | { 53 | if (start > end) 54 | { 55 | return null; 56 | } 57 | 58 | var middle = (start + end) / 2; 59 | 60 | return new BinaryTree() 61 | { 62 | Data = data[middle], 63 | LeftNode = HelperCreateTree(data, start, middle - 1), 64 | RightNode = HelperCreateTree(data, middle + 1, end) 65 | }; 66 | } 67 | 68 | private BinaryTree HelperSearch(BinaryTree tree, int item) 69 | { 70 | if (tree == null) 71 | { 72 | return null; 73 | } 74 | 75 | if (tree.Data == item) 76 | { 77 | return tree; 78 | } 79 | else 80 | { 81 | if (item > tree.Data) 82 | { 83 | return HelperSearch(tree.RightNode, item); 84 | } 85 | else 86 | { 87 | return HelperSearch(tree.LeftNode, item); 88 | } 89 | } 90 | 91 | } 92 | 93 | private BinaryTree HelperInsert(BinaryTree tree, int item) 94 | { 95 | if (tree == null) 96 | { 97 | return new BinaryTree() { Data = item }; 98 | } 99 | 100 | if (item < tree.Data) 101 | { 102 | tree.LeftNode = HelperInsert(tree.LeftNode, item); 103 | } 104 | else if (item > tree.Data) 105 | { 106 | tree.RightNode = HelperInsert(tree.RightNode, item); 107 | } 108 | 109 | return tree; 110 | } 111 | 112 | //private void _FixTree(BinaryTree tree) 113 | //{ 114 | // if (tree.LeftNode != null) 115 | // _FixTree(tree.LeftNode); 116 | // if (tree.RightNode != null) 117 | // _FixTree(tree.RightNode); 118 | 119 | // if (tree.LeftNode != null && tree.Data < tree.LeftNode.Data) 120 | // { 121 | // var tmp = tree.LeftNode.Data; 122 | // tree.LeftNode.Data = tree.Data; 123 | // tree.Data = tmp; 124 | // } 125 | 126 | // if (tree.RightNode != null && tree.Data < tree.RightNode.Data) 127 | // { 128 | // var tmp = tree.RightNode.Data; 129 | // tree.RightNode.Data = tree.Data; 130 | // tree.Data = tmp; 131 | // } 132 | 133 | //} 134 | 135 | } 136 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/TreeAlgorithms/TypedTrees/HeapTree.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees.TreeAlgorithms.Interfaces; 2 | using BasicAlgorithms.Trees.TreeAlgorithms.Models; 3 | using System.Collections.Generic; 4 | 5 | namespace BasicAlgorithms.Trees.TreeAlgorithms.TypedTrees; 6 | 7 | public class HeapTree : ITree 8 | { 9 | 10 | public BinaryTreeResults CreateTree(List data) 11 | { 12 | var length = data.Count; 13 | 14 | var watch = System.Diagnostics.Stopwatch.StartNew(); 15 | var tree = HelperCreateTree(data, length, 0); 16 | watch.Stop(); 17 | 18 | return new BinaryTreeResults() 19 | { 20 | Result = tree, 21 | Ticks = watch.ElapsedTicks 22 | }; 23 | } 24 | 25 | public BinaryTreeResults Search(BinaryTree tree, int item) 26 | { 27 | var watch = System.Diagnostics.Stopwatch.StartNew(); 28 | var result = HelperSearch(tree, item); 29 | watch.Stop(); 30 | 31 | return new BinaryTreeResults() 32 | { 33 | Result = result, 34 | Ticks = watch.ElapsedTicks 35 | }; 36 | } 37 | 38 | public BinaryTreeResults Insert(BinaryTree tree, int item) 39 | { 40 | var watch = System.Diagnostics.Stopwatch.StartNew(); 41 | HelperInsert(tree, item); 42 | watch.Stop(); 43 | 44 | return new BinaryTreeResults() 45 | { 46 | Result = tree, 47 | Ticks = watch.ElapsedTicks 48 | }; 49 | } 50 | 51 | //public BinaryTreeResults Delete(BinaryTree tree, int item) 52 | //{ 53 | // var watch = System.Diagnostics.Stopwatch.StartNew(); 54 | // _Delete(tree, item); 55 | // watch.Stop(); 56 | 57 | // return new BinaryTreeResults() 58 | // { 59 | // Result = tree, 60 | // Ticks = watch.ElapsedTicks 61 | // }; 62 | //} 63 | //private void _Delete(BinaryTree tree, int item) 64 | //{ 65 | 66 | // //find last item and disconnected from its parent 67 | // BinaryTree deleteNode = new BinaryTree(); 68 | // var queue = new List>(); 69 | // queue.Insert(0, new Tuple(null, tree)); 70 | // while (queue.Count > 0) 71 | // { 72 | // var lastItem = queue.Last(); 73 | // queue.Remove(lastItem); 74 | // if (lastItem.Item2.Data == item) 75 | // deleteNode = lastItem.Item2; 76 | 77 | // if (lastItem.Item2.LeftNode != null) 78 | // queue.Insert(0, new Tuple(lastItem.Item2, lastItem.Item2.LeftNode)); 79 | 80 | // if (lastItem.Item2.RightNode != null) 81 | // queue.Insert(0, new Tuple(lastItem.Item2, lastItem.Item2.RightNode)); 82 | 83 | // if (queue.Count == 0) 84 | // { 85 | // if (lastItem.Item1.RightNode == null) 86 | // lastItem.Item1.LeftNode = null; 87 | // else 88 | // lastItem.Item1.RightNode = null; 89 | 90 | // deleteNode.Data = lastItem.Item2.Data; 91 | // _FixTree(deleteNode); 92 | // } 93 | 94 | // } 95 | //} 96 | 97 | 98 | private BinaryTree HelperCreateTree(List data, int length, int largest) 99 | { 100 | //assume largest and get left and right that should be smaller 101 | var new_largest = largest; 102 | var left = 2 * largest + 1;//left branch of heap tree 103 | var right = 2 * largest + 2;//right branch of heap tree 104 | 105 | //if left is larger, get that as the new largest 106 | if (left < length && data[left] > data[new_largest]) 107 | { 108 | new_largest = left; 109 | } 110 | 111 | //if right is larger, get that as the new largest 112 | if (right < length && data[right] > data[new_largest]) 113 | { 114 | new_largest = right; 115 | } 116 | 117 | //if there is a change in largest swap 118 | // and recurse with the new largest 119 | if (new_largest != largest) 120 | { 121 | var tmp = data[largest]; 122 | data[largest] = data[new_largest]; 123 | data[new_largest] = tmp; 124 | } 125 | 126 | //return the tree 127 | return new BinaryTree() 128 | { 129 | Data = data[largest], 130 | LeftNode = (2 * largest + 1 < length) ? HelperCreateTree(data, length, 2 * largest + 1) : null, 131 | RightNode = (2 * largest + 2 < length) ? HelperCreateTree(data, length, 2 * largest + 2) : null 132 | }; 133 | } 134 | 135 | private BinaryTree HelperSearch(BinaryTree tree, int item) 136 | { 137 | var result = new BinaryTree(); 138 | 139 | if (tree == null) 140 | { 141 | return null; 142 | } 143 | 144 | if (tree.Data == item) 145 | { 146 | return tree; 147 | } 148 | 149 | if (tree.LeftNode != null && item <= tree.LeftNode.Data) 150 | { 151 | result = HelperSearch(tree.LeftNode, item); 152 | } 153 | 154 | if (tree.RightNode != null && item <= tree.RightNode.Data) 155 | { 156 | result = HelperSearch(tree.RightNode, item); 157 | } 158 | 159 | return result; 160 | } 161 | 162 | private void HelperInsert(BinaryTree tree, int item) 163 | { 164 | var insertItem = item; 165 | if (item >= tree.Data) 166 | { 167 | insertItem = tree.Data; 168 | tree.Data = item; 169 | } 170 | 171 | if (tree.LeftNode == null) 172 | { 173 | tree.LeftNode = new BinaryTree() { Data = insertItem }; 174 | } 175 | else if (tree.RightNode == null) 176 | { 177 | tree.RightNode = new BinaryTree() { Data = insertItem }; 178 | } 179 | else 180 | { 181 | if (tree.LeftNode.Data <= insertItem) 182 | { 183 | HelperInsert(tree.LeftNode, insertItem); 184 | } 185 | else 186 | { 187 | HelperInsert(tree.RightNode, insertItem); 188 | } 189 | } 190 | 191 | } 192 | 193 | private void HelperFixTree(BinaryTree tree) 194 | { 195 | if (tree.LeftNode != null) 196 | { 197 | HelperFixTree(tree.LeftNode); 198 | } 199 | 200 | if (tree.RightNode != null) 201 | { 202 | HelperFixTree(tree.RightNode); 203 | } 204 | 205 | if (tree.LeftNode != null && tree.Data < tree.LeftNode.Data) 206 | { 207 | var tmp = tree.LeftNode.Data; 208 | tree.LeftNode.Data = tree.Data; 209 | tree.Data = tmp; 210 | } 211 | 212 | if (tree.RightNode != null && tree.Data < tree.RightNode.Data) 213 | { 214 | var tmp = tree.RightNode.Data; 215 | tree.RightNode.Data = tree.Data; 216 | tree.Data = tmp; 217 | } 218 | 219 | } 220 | 221 | } 222 | -------------------------------------------------------------------------------- /BasicAlgorithms/Trees/TreeFactory.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees.DataProviders.Models; 2 | using BasicAlgorithms.Trees.TreeAlgorithms.Interfaces; 3 | using BasicAlgorithms.Trees.TreeAlgorithms.Models; 4 | using BasicAlgorithms.Trees.TreeAlgorithms.TypedTrees; 5 | using System; 6 | 7 | namespace BasicAlgorithms.Trees; 8 | 9 | public class TreeFactory 10 | { 11 | public int SampleSize { get; } 12 | public TreeFactory(int sample) 13 | { 14 | SampleSize = sample; 15 | } 16 | 17 | public BinaryTreeEstimation Estimate(EnumTreeTypes treeType) 18 | { 19 | var _tree = GetTree(treeType); 20 | var _treeData = new DataProvidersFactory(SampleSize).GetProvider(treeType == EnumTreeTypes.BST ? EnumTreeDataProvider.BST : EnumTreeDataProvider.Heap); 21 | 22 | return new BinaryTreeEstimation() 23 | { 24 | Deserialize = _tree.CreateTree(_treeData.Data), 25 | Search = _tree.Search(_treeData.Tree, _treeData.AvgValue), 26 | Insert = _tree.Insert(_treeData.Tree, _treeData.NotFoundValue), 27 | }; 28 | 29 | } 30 | 31 | private static ITree GetTree(EnumTreeTypes treeType) 32 | { 33 | return treeType switch 34 | { 35 | EnumTreeTypes.Heap => new HeapTree(), 36 | EnumTreeTypes.BST => new BinarySearchTree(), 37 | _ => throw new NotImplementedException("Unknown tree type '" + nameof(treeType) + "'"), 38 | }; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /BasicAlgorithms/UI/ArraySearchUI.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays; 2 | using BasicAlgorithms.Arrays.DataProviders.Models; 3 | using BasicAlgorithms.Arrays.SearchAlgorithms.Models; 4 | using System; 5 | 6 | namespace BasicAlgorithms.UI; 7 | 8 | public class ArraySearchUI : Grid 9 | { 10 | 11 | private readonly ArraySearchFactory _searchFactory; 12 | public ArraySearchUI(ArraySearchFactory searchFactory, int tableWith) : base(tableWith) 13 | { 14 | _searchFactory = searchFactory; 15 | } 16 | 17 | public void Print() 18 | { 19 | PrintLine(); 20 | PrintRow("Ticks needed to search an item in " + _searchFactory.SampleSize + " integers"); 21 | PrintLine(); 22 | 23 | PrintLine(); 24 | PrintRow("", "Linear", "Jump", "Binary", "Interpolation"); 25 | PrintLine(); 26 | 27 | PrintRow("Sorted, uniform distribution array"); 28 | PrintLine(); 29 | PrintGrid( 30 | _searchFactory.Estimate(EnumArraysSearchAlgorithms.Linear, EnumArrayDataProviders.SortedAndUniform), 31 | _searchFactory.Estimate(EnumArraysSearchAlgorithms.Jump, EnumArrayDataProviders.SortedAndUniform), 32 | _searchFactory.Estimate(EnumArraysSearchAlgorithms.Binary, EnumArrayDataProviders.SortedAndUniform), 33 | _searchFactory.Estimate(EnumArraysSearchAlgorithms.Interpolation, EnumArrayDataProviders.SortedAndUniform) 34 | ); 35 | 36 | PrintRow("Sorted, not evenly distributed array"); 37 | PrintLine(); 38 | PrintGrid( 39 | _searchFactory.Estimate(EnumArraysSearchAlgorithms.Linear, EnumArrayDataProviders.Sorted), 40 | _searchFactory.Estimate(EnumArraysSearchAlgorithms.Jump, EnumArrayDataProviders.Sorted), 41 | _searchFactory.Estimate(EnumArraysSearchAlgorithms.Binary, EnumArrayDataProviders.Sorted), 42 | _searchFactory.Estimate(EnumArraysSearchAlgorithms.Interpolation, EnumArrayDataProviders.Sorted) 43 | ); 44 | 45 | PrintRow("Unsorted (random) array"); 46 | PrintLine(); 47 | PrintGrid( 48 | _searchFactory.Estimate(EnumArraysSearchAlgorithms.Linear, EnumArrayDataProviders.Unsorted), 49 | _searchFactory.Estimate(EnumArraysSearchAlgorithms.Jump, EnumArrayDataProviders.Unsorted), 50 | _searchFactory.Estimate(EnumArraysSearchAlgorithms.Binary, EnumArrayDataProviders.Unsorted), 51 | _searchFactory.Estimate(EnumArraysSearchAlgorithms.Interpolation, EnumArrayDataProviders.Unsorted) 52 | ); 53 | 54 | var color = Console.ForegroundColor; 55 | Console.ForegroundColor = ConsoleColor.Blue; 56 | Console.Write("Target found, "); 57 | Console.ForegroundColor = ConsoleColor.Red; 58 | Console.Write("Target not found!"); 59 | Console.WriteLine(); 60 | Console.ForegroundColor = color; 61 | } 62 | 63 | private void PrintGrid(SearchResults l, SearchResults j, SearchResults b, SearchResults i) 64 | { 65 | 66 | PrintRow("Min", 67 | (l.MinValue.PositionFound != null ? ConsoleColor.Blue : ConsoleColor.Red) + "|" + l.MinValue.Ticks.ToString("00000000"), 68 | (j.MinValue.PositionFound != null ? ConsoleColor.Blue : ConsoleColor.Red) + "|" + j.MinValue.Ticks.ToString("00000000"), 69 | (b.MinValue.PositionFound != null ? ConsoleColor.Blue : ConsoleColor.Red) + "|" + b.MinValue.Ticks.ToString("00000000"), 70 | (i.MinValue.PositionFound != null ? ConsoleColor.Blue : ConsoleColor.Red) + "|" + i.MinValue.Ticks.ToString("00000000") 71 | ); 72 | PrintRow("Avg", 73 | (l.AvgValue.PositionFound != null ? ConsoleColor.Blue : ConsoleColor.Red) + "|" + l.AvgValue.Ticks.ToString("00000000"), 74 | (j.AvgValue.PositionFound != null ? ConsoleColor.Blue : ConsoleColor.Red) + "|" + j.AvgValue.Ticks.ToString("00000000"), 75 | (b.AvgValue.PositionFound != null ? ConsoleColor.Blue : ConsoleColor.Red) + "|" + b.AvgValue.Ticks.ToString("00000000"), 76 | (i.AvgValue.PositionFound != null ? ConsoleColor.Blue : ConsoleColor.Red) + "|" + i.AvgValue.Ticks.ToString("00000000") 77 | ); 78 | PrintRow("Max", 79 | (l.MaxValue.PositionFound != null ? ConsoleColor.Blue : ConsoleColor.Red) + "|" + l.MaxValue.Ticks.ToString("00000000"), 80 | (j.MaxValue.PositionFound != null ? ConsoleColor.Blue : ConsoleColor.Red) + "|" + j.MaxValue.Ticks.ToString("00000000"), 81 | (b.MaxValue.PositionFound != null ? ConsoleColor.Blue : ConsoleColor.Red) + "|" + b.MaxValue.Ticks.ToString("00000000"), 82 | (i.MaxValue.PositionFound != null ? ConsoleColor.Blue : ConsoleColor.Red) + "|" + i.MaxValue.Ticks.ToString("00000000") 83 | ); 84 | PrintRow("NotFound", 85 | (l.NotFoundValue.PositionFound != null ? ConsoleColor.Blue : ConsoleColor.Red) + "|" + l.NotFoundValue.Ticks.ToString("00000000"), 86 | (j.NotFoundValue.PositionFound != null ? ConsoleColor.Blue : ConsoleColor.Red) + "|" + j.NotFoundValue.Ticks.ToString("00000000"), 87 | (b.NotFoundValue.PositionFound != null ? ConsoleColor.Blue : ConsoleColor.Red) + "|" + b.NotFoundValue.Ticks.ToString("00000000"), 88 | (i.NotFoundValue.PositionFound != null ? ConsoleColor.Blue : ConsoleColor.Red) + "|" + i.NotFoundValue.Ticks.ToString("00000000") 89 | ); 90 | PrintLine(); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /BasicAlgorithms/UI/ArraySortUI.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Arrays; 2 | using BasicAlgorithms.Arrays.DataProviders.Models; 3 | using BasicAlgorithms.Arrays.SortingAlgorithms.Models; 4 | 5 | namespace BasicAlgorithms.UI; 6 | 7 | public class ArraySortUI : Grid 8 | { 9 | 10 | private readonly ArraySortFactory _sortFactory; 11 | public ArraySortUI(ArraySortFactory sortFactory, int tableWith) : base(tableWith) 12 | { 13 | _sortFactory = sortFactory; 14 | } 15 | 16 | public void Print() 17 | { 18 | PrintLine(); 19 | 20 | PrintRow("Ticks needed to sort " + _sortFactory.SampleSize + " integers"); 21 | PrintLine(); 22 | 23 | PrintLine(); 24 | PrintRow("", "Random", "Reversed", "Sorted"); 25 | PrintLine(); 26 | 27 | PrintGrid("Insertion", 28 | _sortFactory.Estimate(EnumArraySortAlgorithms.Insertion, EnumArrayDataProviders.Unsorted), 29 | _sortFactory.Estimate(EnumArraySortAlgorithms.Insertion, EnumArrayDataProviders.ReverseSorted), 30 | _sortFactory.Estimate(EnumArraySortAlgorithms.Insertion, EnumArrayDataProviders.Sorted) 31 | ); 32 | PrintGrid("Selection", 33 | _sortFactory.Estimate(EnumArraySortAlgorithms.Selection, EnumArrayDataProviders.Unsorted), 34 | _sortFactory.Estimate(EnumArraySortAlgorithms.Selection, EnumArrayDataProviders.ReverseSorted), 35 | _sortFactory.Estimate(EnumArraySortAlgorithms.Selection, EnumArrayDataProviders.Sorted) 36 | ); 37 | PrintGrid("Bubble", 38 | _sortFactory.Estimate(EnumArraySortAlgorithms.Bubble, EnumArrayDataProviders.Unsorted), 39 | _sortFactory.Estimate(EnumArraySortAlgorithms.Bubble, EnumArrayDataProviders.ReverseSorted), 40 | _sortFactory.Estimate(EnumArraySortAlgorithms.Bubble, EnumArrayDataProviders.Sorted) 41 | ); 42 | PrintGrid("Heap", 43 | _sortFactory.Estimate(EnumArraySortAlgorithms.Heap, EnumArrayDataProviders.Unsorted), 44 | _sortFactory.Estimate(EnumArraySortAlgorithms.Heap, EnumArrayDataProviders.ReverseSorted), 45 | _sortFactory.Estimate(EnumArraySortAlgorithms.Heap, EnumArrayDataProviders.Sorted) 46 | ); 47 | PrintGrid("Merge", 48 | _sortFactory.Estimate(EnumArraySortAlgorithms.Merge, EnumArrayDataProviders.Unsorted), 49 | _sortFactory.Estimate(EnumArraySortAlgorithms.Merge, EnumArrayDataProviders.ReverseSorted), 50 | _sortFactory.Estimate(EnumArraySortAlgorithms.Merge, EnumArrayDataProviders.Sorted) 51 | ); 52 | PrintGrid("Quick", 53 | _sortFactory.Estimate(EnumArraySortAlgorithms.Quick, EnumArrayDataProviders.Unsorted), 54 | _sortFactory.Estimate(EnumArraySortAlgorithms.Quick, EnumArrayDataProviders.ReverseSorted), 55 | _sortFactory.Estimate(EnumArraySortAlgorithms.Quick, EnumArrayDataProviders.Sorted) 56 | ); 57 | } 58 | 59 | private void PrintGrid(string title, SortResults u, SortResults r, SortResults s) 60 | { 61 | PrintRow(title, 62 | u.Ticks.ToString("00000000"), 63 | r.Ticks.ToString("00000000"), 64 | s.Ticks.ToString("00000000") 65 | ); 66 | PrintLine(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /BasicAlgorithms/UI/Grid.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BasicAlgorithms.UI; 4 | 5 | public class Grid 6 | { 7 | private readonly int _tableWidth = 90; 8 | private readonly ConsoleColor DefaultColor = Console.ForegroundColor; 9 | 10 | public Grid(int tableWith) 11 | { 12 | _tableWidth = tableWith; 13 | } 14 | 15 | public void PrintLine() 16 | { 17 | Console.WriteLine(new string('-', _tableWidth)); 18 | } 19 | 20 | public void PrintRow(params string[] columns) 21 | { 22 | int width = (_tableWidth - columns.Length) / columns.Length; 23 | Console.Write("|"); 24 | 25 | foreach (string column in columns) 26 | { 27 | var color = DefaultColor; 28 | var text = column; 29 | if (column.Contains('|')) 30 | { 31 | color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), column.Split("|")[0]); 32 | text = column.Split("|")[1]; 33 | } 34 | Console.ForegroundColor = color; 35 | Console.Write(AlignCentre(text, width)); 36 | Console.ForegroundColor = DefaultColor; 37 | Console.Write("|"); 38 | } 39 | 40 | Console.WriteLine(); 41 | } 42 | 43 | private static string AlignCentre(string text, int width) 44 | { 45 | text = text.Length > width ? string.Concat(text.AsSpan(0, width - 3), "...") : text; 46 | 47 | if (string.IsNullOrEmpty(text)) 48 | { 49 | return new string(' ', width); 50 | } 51 | else 52 | { 53 | return text.PadRight(width - (width - text.Length) / 2).PadLeft(width); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /BasicAlgorithms/UI/TraversalUI.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees; 2 | using BasicAlgorithms.Trees.TreeAlgorithms.Models; 3 | using System; 4 | 5 | namespace BasicAlgorithms.UI; 6 | 7 | public class TraversalUI : Grid 8 | { 9 | 10 | private readonly TraversalFactory _traversalFactory; 11 | public TraversalUI(TraversalFactory traversalFactory, int tableWith) : base(tableWith) 12 | { 13 | _traversalFactory = traversalFactory; 14 | } 15 | 16 | public void Print() 17 | { 18 | PrintLine(); 19 | 20 | PrintRow("Traversal Algorithms Differences for the following tree"); 21 | PrintLine(); 22 | 23 | var traversal = _traversalFactory.Traverse(EnumTreeTypes.BST); 24 | 25 | PrintTree(traversal.Tree, ""); 26 | PrintLine(); 27 | 28 | PrintRow("Breadth First", 29 | string.Join(" ", traversal.BreadthFirstResult) 30 | ); 31 | PrintRow("In Order", 32 | string.Join(" ", traversal.InOrderResult) 33 | ); 34 | PrintRow("Post Order", 35 | string.Join(" ", traversal.PostOrderResult) 36 | ); 37 | PrintRow("Pre Order", 38 | string.Join(" ", traversal.PreOrderResult) 39 | ); 40 | } 41 | 42 | public void PrintTree(BinaryTree tree, string indent) 43 | { 44 | Console.Write(indent); 45 | Console.Write("^-"); 46 | indent += " "; 47 | 48 | if (tree == null) 49 | { 50 | Console.WriteLine("[ ]"); 51 | return; 52 | } 53 | else 54 | { 55 | Console.WriteLine("[" + tree.Data + "]"); 56 | } 57 | 58 | if (tree.LeftNode != null || tree.RightNode != null) 59 | { 60 | PrintTree(tree.LeftNode, indent); 61 | PrintTree(tree.RightNode, indent); 62 | } 63 | 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /BasicAlgorithms/UI/TreeUI.cs: -------------------------------------------------------------------------------- 1 | using BasicAlgorithms.Trees; 2 | using BasicAlgorithms.Trees.TreeAlgorithms.Models; 3 | 4 | namespace BasicAlgorithms.UI; 5 | 6 | public class TreeUI : Grid 7 | { 8 | 9 | private readonly TreeFactory _treeFactory; 10 | public TreeUI(TreeFactory treeFactory, int tableWith) : base(tableWith) 11 | { 12 | _treeFactory = treeFactory; 13 | } 14 | 15 | public void Print() 16 | { 17 | PrintLine(); 18 | 19 | PrintRow("Ticks needed for a tree of " + _treeFactory.SampleSize + " integers"); 20 | PrintLine(); 21 | 22 | PrintLine(); 23 | PrintRow("", "Creation", "Search", "Insert"); 24 | PrintLine(); 25 | 26 | PrintGrid("Heap", 27 | _treeFactory.Estimate(EnumTreeTypes.Heap) 28 | ); 29 | PrintGrid("BST", 30 | _treeFactory.Estimate(EnumTreeTypes.BST) 31 | ); 32 | } 33 | 34 | private void PrintGrid(string title, BinaryTreeEstimation r) 35 | { 36 | 37 | PrintRow(title, 38 | r.Deserialize.Ticks.ToString("00000000"), 39 | r.Search.Ticks.ToString("00000000"), 40 | r.Insert.Ticks.ToString("00000000") 41 | ); 42 | PrintLine(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![.NET](https://github.com/georgekosmidis/BasicAlgorithms/actions/workflows/restore-build-test.yml/badge.svg)](https://github.com/georgekosmidis/BasicAlgorithms/actions/workflows/restore-build-test.yml) 2 | 3 | Basic Algorithms Comparison in C# 4 | ================================= 5 | Tests and compares the time the following algorithms need to perform their intended job 6 | 7 | Array Search 8 | ------------ 9 | 1. Linear Search 10 | 2. Jump Search 11 | 3. Binary Search 12 | 4. Interpolation Search 13 | 14 | Array Sorting 15 | ------------- 16 | 1. Insertion Sort 17 | 2. Selection Sort 18 | 3. Bubble Sort 19 | 4. Heap Sort 20 | 5. Merge Sort 21 | 6. Quick Sort 22 | 23 | Trees 24 | ----- 25 | 1. Heap Tree 26 | 2. BST Tree 27 | 28 | Traversals 29 | ---------- 30 | 1. Breadth First Traversal 31 | 2. InOrder Traversal 32 | 3. PostOrder Traversal 33 | 4. PreOrder Traversal 34 | 35 | Comparison for Search Algorithms 36 | -------------------------------- 37 | 38 | 39 | Comparison for Sort Algorithms 40 | ------------------------------ 41 | 42 | 43 | Comparison for Trees 44 | -------------------- 45 | 46 | 47 | Comparison for Traversals 48 | ------------------------- 49 | 50 | 51 | 52 | Various Problems 53 | ---------------- 54 | 1. Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. 55 | 2. Given a Matrix of size M x N. Your task is to print the matrix K times left rotated. 56 | 3. Given two integers ‘L’ and ‘R’, write a program that finds the count of numbers having prime number of set bits in their binary representation in the range [L, R]. 57 | 4. Problem described at https://practice.geeksforgeeks.org/problems/find-k-th-character-in-string/0 58 | 5. Problem described at https://practice.geeksforgeeks.org/problems/maximum-tip-calculator/0 59 | 6. Problem described at https://practice.geeksforgeeks.org/problems/check-if-array-contains-contiguous-integers-with-duplicates-allowed/0 60 | 7. Problem described at https://practice.geeksforgeeks.org/problems/pairs-with-positive-negative-values/0 61 | -------------------------------------------------------------------------------- /README/search_results.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/georgekosmidis/BasicAlgorithms/e3e8fe8bbbf0278d050fe93d20421faad3fd1049/README/search_results.png -------------------------------------------------------------------------------- /README/sort_results.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/georgekosmidis/BasicAlgorithms/e3e8fe8bbbf0278d050fe93d20421faad3fd1049/README/sort_results.png -------------------------------------------------------------------------------- /README/traversal_results.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/georgekosmidis/BasicAlgorithms/e3e8fe8bbbf0278d050fe93d20421faad3fd1049/README/traversal_results.png -------------------------------------------------------------------------------- /README/tree_results.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/georgekosmidis/BasicAlgorithms/e3e8fe8bbbf0278d050fe93d20421faad3fd1049/README/tree_results.png --------------------------------------------------------------------------------