Created
May 15, 2020 01:00
-
-
Save Meghatronics/170731d0b7d652148cdb975ac16d4f87 to your computer and use it in GitHub Desktop.
Day 14 30 Days Of Code. Insertion Sort algorithm. Emmanuel.1aliyu@gmail.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void main() { | |
| List<double> unsortedList = [2, 5, 3, 4, 7.2, 1, 3, 10, 200, 54, 21, 46, 8]; | |
| List sortedList = sort(unsortedList); | |
| print(sortedList); | |
| } | |
| ///Sorts a list of doubles [unsortedArray] in ascending order. | |
| /// | |
| ///Takes an unordered list of doubles [unsortedArray] and returns a list of its elements, | |
| ///arranged in ascending order. | |
| /// | |
| /// Uses INSERTION SORT Algorithm. | |
| /// | |
| /// Maintains input list length. Does not remove repitions. | |
| /// ``` | |
| /// sortedList = sort([4,3,3,5,2]); | |
| /// sortedList == [2,3,3,4,5] | |
| /// ``` | |
| List<double> sort(List<double> unsortedArray) { | |
| ///The length of the input list. | |
| int listLength; | |
| int checkLevel, currentCheck; | |
| double key; | |
| listLength = unsortedArray.length; | |
| for (checkLevel = 1; checkLevel < listLength; checkLevel++) { | |
| currentCheck = checkLevel; | |
| while (currentCheck > 0 && | |
| unsortedArray[currentCheck - 1] > unsortedArray[currentCheck]) { | |
| key = unsortedArray[currentCheck]; | |
| unsortedArray[currentCheck] = unsortedArray[currentCheck - 1]; | |
| unsortedArray[currentCheck - 1] = key; | |
| currentCheck--; | |
| } | |
| } | |
| return unsortedArray; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment