├── .gitignore ├── lib ├── junit-4.12.jar └── hamcrest-core-1.3.jar ├── Searching ├── src │ ├── LinearSearch.kt │ └── Search.kt ├── README.md └── test │ └── LinearSearchTest.kt ├── Sorting ├── src │ ├── Sort.kt │ └── SelectionSort.kt ├── README.md └── test │ └── SelectionSortTest.kt ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ -------------------------------------------------------------------------------- /lib/junit-4.12.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/android254/Algorithms/HEAD/lib/junit-4.12.jar -------------------------------------------------------------------------------- /lib/hamcrest-core-1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/android254/Algorithms/HEAD/lib/hamcrest-core-1.3.jar -------------------------------------------------------------------------------- /Searching/src/LinearSearch.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Implementation of LinearSearch Algorithm 4 | * 5 | */ 6 | class LinearSearch : Search { 7 | override fun > search(data: Array, element: T): Int { 8 | data.forEachIndexed { index, currentElement -> 9 | if (currentElement == element) return index 10 | } 11 | return -1 12 | } 13 | } -------------------------------------------------------------------------------- /Searching/src/Search.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Provides Generic Interface for Search Algorithms 4 | * 5 | **/ 6 | interface Search { 7 | 8 | /** 9 | * 10 | * @param data the array which the specified element will be looked for 11 | * @param element the item to be looked for 12 | * @return index of element ,if multiple will return first index 13 | * 14 | */ 15 | fun > search(data: Array, element: T): Int 16 | } -------------------------------------------------------------------------------- /Sorting/src/Sort.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Provides Generic Interface for Sorting Algorithms 4 | * 5 | */ 6 | interface Sort { 7 | 8 | /** 9 | * 10 | * @param unsorted an array of items which are to be sorted in a specified order 11 | * @return a sorted array of items from the unsorted list 12 | * 13 | */ 14 | fun > sort(unsorted: Array): Array 15 | 16 | /** 17 | * 18 | * Provides an alternative to working with arrays 19 | * 20 | * @param unsorted a list of items which are to be sorted in a specified order 21 | * @return a sorted list of items from the unsorted list 22 | * 23 | */ 24 | fun > sort(unsorted: MutableList): List 25 | } -------------------------------------------------------------------------------- /Searching/README.md: -------------------------------------------------------------------------------- 1 | ## Searching Algorithms 2 | 3 | Searching Algorithms are used to find or retrieve an element from where it is stored in a given data structure. 4 | This can be either __Sequential__ or __Interval__ based. 5 | 6 | 1. **Linear Search** 7 | 8 | ![Linear Search Gif](https://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif) 9 | 10 | This is a sequential search,it works by comparing the input in which you are searching against all elements in the data structure 11 | starting from the first element to the last and will stop if it encounters a match midway. 12 | 13 | 14 | __Best Case Scenario__ : 0(1) - this is when the item is found on the first try 15 | 16 | __Worst Case Scenario__: 0(n) - this can be cases where the input is not in the list or is at the very end of the list. 17 | 18 | __Average Case Scenario__: 0(n) -------------------------------------------------------------------------------- /Searching/test/LinearSearchTest.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | 3 | import org.junit.Assert.* 4 | 5 | class LinearSearchTest { 6 | 7 | @Test 8 | fun search() { 9 | val linearSearch = LinearSearch() 10 | 11 | val numbersArray = arrayOf(1, 3, 4, 5, 56, 4, 5, 6, 7, 84, 3, 3564, 64, 234, 35, 43, 242, 5222, 425, 132, 453, 324, 324, 532, 422) 12 | val characterArray = arrayOf('a', 'b', 'v', 'f', 'r', 'r', 'e', 'q', 'y', 'u', 'y', 'j', 'k', 'l', 'b', 'g', 'y', 'x') 13 | 14 | val noNotFound = linearSearch.search(numbersArray, 2) 15 | val noFound = linearSearch.search(numbersArray, 324) 16 | val charNotFound = linearSearch.search(characterArray, 'o') 17 | val charFound = linearSearch.search(characterArray, 'y') 18 | 19 | assertTrue("That number exists", noNotFound == -1) 20 | assertTrue("That number does not exist", noFound == 21) //index of first instance 21 | assertTrue("That character exists", charNotFound == -1) 22 | assertTrue("That character does not exist", charFound == 8) 23 | } 24 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 android254 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Algorithms🚧 🚧(In-Progress)🚧🚧 2 | 3 | This Repository contains implementation to common or any interesting algorithms with Kotlin and their analysis. 4 | 5 | Each module represents a subset of different types/forms of algorithms 6 | 7 | To Contribute to this repository e.g adding a set of algorithms classified under dynamic programming 8 | - Create the module ```Dynamic Programming``` with ``src`` directory containing the algorithm(s) implementation each with their own class/file and ``test`` directory for tests related to the algorithm. 9 | This follows the default java project structure. 10 | 11 | Or 12 | 13 | - If the module to your algorithm exists add it under src in its own file/class with a corresponding test 14 | 15 | or 16 | 17 | - If your algorithm does not fit in any module or type of algorithm place it under the ```Other``` module with corresponding test 18 | 19 | Checkout [Sorting](https://github.com/Davidodari/Algorithms/tree/sorting-algorithms-selection/Sorting) module for example template 20 | 21 | The documentation will iterate and improve over the course of development and is a chance for first time contributors to get their 22 | hands dirty with open source contributions. 23 | 24 | Thank you for your contribution. -------------------------------------------------------------------------------- /Sorting/README.md: -------------------------------------------------------------------------------- 1 | ## Sorting Algorithms 2 | 3 | Sorting Algorithms are used to put elements in a given set of data in a certain order. 4 | 5 | 1. **Selection Sort** 6 | 7 | ![Selection Sort Gif](https://upload.wikimedia.org/wikipedia/commons/9/94/Selection-Sort-Animation.gif) 8 | 9 | Given a set of data,Selection sort; 10 | 11 | - Selects the smallest/minimum or largest/maximum value in the data set depending on desired order. 12 | - Places the smallest/minimum or largest/maximum at the beginning of the data set ,if it is already at the beginning it will move on to next element 13 | otherwise it will swap the value initially at the starting index with the minimum/maximum value and this will go on for each index till it traverses to the end. 14 | This is achieved by using a temporary variable to swap elements from one index to another. 15 | - Results in two subsets of data ;sorted and unsorted.The unsorted subset is broken down further to smaller subsets as sorting goes on until the whole list is sorted. 16 | 17 | Due to the fact that it has atleast 2 loops one being nested ```n*n```,in terms of analysis; 18 | n being the time taken to execute the code 19 | 20 | __Best Case Scenario__ : 0(n^2) 21 | 22 | __Worst Case Scenario__: 0(n^2) 23 | 24 | __Average Case Scenario__: 0(n^2) -------------------------------------------------------------------------------- /Sorting/src/SelectionSort.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Implementation of selection sort algorithm for arrays and lists 3 | */ 4 | class SelectionSort : Sort { 5 | override fun > sort(unsorted: Array): Array { 6 | //Get length of data set 7 | val arrayLength = unsorted.size 8 | for (i in 0 until arrayLength) { 9 | //Index will be used to find minimum element 10 | var index = i 11 | for (j in i + 1 until arrayLength) { 12 | if (unsorted[j] < unsorted[index]) 13 | //Store position of smallest element 14 | index = j 15 | } 16 | //Swap minimum element with first element in unsorted sublist 17 | val temp = unsorted[index] 18 | unsorted[index] = unsorted[i] 19 | unsorted[i] = temp 20 | } 21 | return unsorted 22 | } 23 | 24 | override fun > sort(unsorted: MutableList): List { 25 | val listLength = unsorted.size 26 | for (i in 0 until listLength) { 27 | var index = i 28 | for (j in i + 1 until listLength) { 29 | if (unsorted[j] < unsorted[index]) 30 | index = j 31 | } 32 | val temp = unsorted[index] 33 | unsorted[index] = unsorted[i] 34 | unsorted[i] = temp 35 | } 36 | return unsorted 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Sorting/test/SelectionSortTest.kt: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | 3 | import org.junit.Assert.* 4 | import org.junit.Before 5 | 6 | class SelectionSortTest { 7 | 8 | lateinit var selectionSort: SelectionSort 9 | 10 | @Before 11 | fun selectionSort() { 12 | selectionSort = SelectionSort() 13 | } 14 | 15 | @Test 16 | fun sortArray() { 17 | //Unsorted 18 | val alphabets = arrayOf('s', 'r', 'e', 'w', 't', 'a', 'r', 'p', 'z', 'b', 'd', 'y', 'z', 'y') 19 | val numbers = arrayOf(1, 3, 78, 32, 1, 23, 0, 2, 33, 2909, 4590, 12, 123, 342, 6) 20 | 21 | //Sorted 22 | val alphabetsSorted = arrayOf('a', 'b', 'd', 'e', 'p', 'r', 'r', 's', 't', 'w', 'y', 'y', 'z', 'z') 23 | val numbersSorted = arrayOf(0, 1, 1, 2, 3, 6, 12, 23, 32, 33, 78, 123, 342, 2909, 4590) 24 | 25 | assertTrue("Characters should follow natural order", selectionSort.sort(alphabets).contentEquals(alphabetsSorted)) 26 | assertTrue("Numbers should follow natural order", selectionSort.sort(numbers).contentEquals(numbersSorted)) 27 | } 28 | 29 | @Test 30 | fun sortList() { 31 | //Unsorted 32 | val namesList = mutableListOf("David", "Dorothy", "Daniel", "Dimitry", "Duncan", "Drogba", "Douglas", "Diplo", "Daenarys", "Dee") 33 | val numberList = mutableListOf(712345623, 123455, 338472, 49821, 3384001, 420480, 20480284, 1024824, 4288402, 9274927, 497294, 4927490) 34 | 35 | val namesListSorted = mutableListOf("Daenarys", "Daniel", "David", "Dee", "Dimitry", "Diplo", "Dorothy", "Douglas", "Drogba", "Duncan") 36 | val numberListSorted = mutableListOf(49821, 123455, 338472, 420480, 497294, 1024824, 3384001, 4288402, 4927490, 9274927, 20480284, 712345623) 37 | 38 | assertTrue("Names should follow natural order", selectionSort.sort(namesList) == namesListSorted) 39 | assertTrue("Numbers should follow natural order", selectionSort.sort(numberList) == numberListSorted) 40 | } 41 | } --------------------------------------------------------------------------------