From 42a78f47b67abe184d9ff0afecf152860ab5dafc Mon Sep 17 00:00:00 2001 From: Anthony Berg Date: Fri, 4 Sep 2020 02:07:08 +0100 Subject: [PATCH] Merge sort didnt add lol --- Assets/Scripts/monopoly/MergeSort.cs | 71 ++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Assets/Scripts/monopoly/MergeSort.cs diff --git a/Assets/Scripts/monopoly/MergeSort.cs b/Assets/Scripts/monopoly/MergeSort.cs new file mode 100644 index 0000000..1a88fa2 --- /dev/null +++ b/Assets/Scripts/monopoly/MergeSort.cs @@ -0,0 +1,71 @@ +using System.Collections.Generic; +using System.Linq; + +public static class MergeMethod +{ + public static List MergeSort(List unsorted) + { + + if (unsorted.Count <= 1) //Checks if the list is longer than 1 to do a merge sort + { + return unsorted; //Stops the function if the length is 1 or less + } + + int middle = unsorted.Count / 2; //Does an integer division of 2 + + List left = new List(); //Creates a list for the left items in the list. + List right = new List(); //Creates a list for the right items in the list. + + + for (int i = 0; i < middle; i++) //Adds the left half of the unsorted list to the left list. + { + left.Add(unsorted[i]); + } + + for (int i = middle; i < unsorted.Count; i++) //Adds the rest of the unsorted list to the right list. + { + right.Add(unsorted[i]); + } + + //Uses recursion to get to return early. + left = MergeSort(left); + right = MergeSort(right); + + //Merges the lists. + return Merge(left, right); + } + + private static List Merge(List left, List right) + { + List sorted = new List(); //Creates the list with the sort. + + while (left.Count > 0 || right.Count > 0) //While operates as the left and right lists aren't empty. + { + if (left.Count > 0 && right.Count > 0) //Checks if none of the lists are empty. + { + if (left.First().property_id <= right.First().property_id) //Checks if the left one is smaller than the right one. + { + sorted.Add(left.First()); + left.Remove(left.First()); + } + else //If the right one is smaller than the left one then... + { + sorted.Add(right.First()); + right.Remove(right.First()); + } + } + else if (left.Count > 0) //Runs if the only list left is the left one. + { + sorted.Add(left.First()); + left.Remove(left.First()); + } + else if (right.Count > 0) //Runs if the only list left is the right one. + { + sorted.Add(right.First()); + right.Remove(right.First()); + } + } + + return sorted; //Returns the sorted list. + } +}