From f7835f49b9b9f46f5dbec8691bca8ef7e5a776fb Mon Sep 17 00:00:00 2001 From: ldrf Date: Sat, 17 Oct 2020 20:37:52 -0300 Subject: [PATCH 1/5] Add MergeSort algorith with Java langague --- day34/Java/MergeSort.java | 75 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 day34/Java/MergeSort.java diff --git a/day34/Java/MergeSort.java b/day34/Java/MergeSort.java new file mode 100644 index 00000000..5706a314 --- /dev/null +++ b/day34/Java/MergeSort.java @@ -0,0 +1,75 @@ +import java.util.Arrays; + +public class MergeSort { + + public static void main(String[] args) { + int[] arrayToOrder = new int[]{1, 5, 2, 7, 3, 4, 8, 9, 6}; + System.out.println("Array before sorting"); + System.out.println(Arrays.toString(arrayToOrder)); + int startIndex = 0; + int highIdex = arrayToOrder.length - 1; + mergeSort(arrayToOrder, startIndex, highIdex); + System.out.println("Array after sorting"); + System.out.println(Arrays.toString(arrayToOrder)); + } + + private static void mergeSort(int[] arrayToOrder, int lowIndex, + int highIndex) { + if (lowIndex < highIndex) { + int firstMidIndex = (lowIndex + highIndex) / 2; + mergeSort(arrayToOrder, lowIndex, firstMidIndex); + int secondMidIndex = firstMidIndex + 1; + mergeSort(arrayToOrder, secondMidIndex, highIndex); + merge(arrayToOrder, lowIndex, firstMidIndex, highIndex); + } + } + + private static void merge(int[] arrayToOrder, int lowIndex, int midIndex, + int highIndex) { + int leftSubArraySize = (midIndex - lowIndex) + 1; + int rightSubArraySize = highIndex - midIndex; + int[] leftArray = getArray(arrayToOrder, leftSubArraySize, lowIndex); + int[] rightArray = getArray(arrayToOrder, rightSubArraySize, + midIndex + 1); + int leftCounter = 0; + int rightCounter = 0; + while ((leftCounter < leftSubArraySize) + && (rightCounter < rightSubArraySize)) { + if (leftArray[leftCounter] <= rightArray[rightCounter]) { + arrayToOrder[lowIndex] = leftArray[leftCounter]; + ++leftCounter; + } else { + arrayToOrder[lowIndex] = rightArray[rightCounter]; + ++rightCounter; + } + ++lowIndex; + + } + + lowIndex = remainingElements(arrayToOrder, lowIndex, leftSubArraySize, + leftArray, leftCounter); + remainingElements(arrayToOrder, lowIndex, rightSubArraySize, rightArray, + rightCounter); + + } + + private static int[] getArray(int[] arrayToOrder, int arraySideSize, + int startIndex) { + int[] sideArray = new int[arraySideSize]; + for (int i = 0; i < sideArray.length; i++) { + sideArray[i] = arrayToOrder[startIndex + i]; + } + return sideArray; + } + + private static int remainingElements(int[] arrayToOrder, int startIndex, + int subArraySizeSide, int[] sideArray, int sideCounter) { + while (sideCounter < subArraySizeSide) { + arrayToOrder[startIndex] = sideArray[sideCounter]; + ++startIndex; + ++sideCounter; + } + return startIndex; + } + +} From dbf4d537f1cffed3f2f86aee9b2ab52375080917 Mon Sep 17 00:00:00 2001 From: ldrf Date: Sat, 17 Oct 2020 21:37:36 -0300 Subject: [PATCH 2/5] Add @ldrf as a contributor --- .all-contributorsrc | 10 ++++++++++ CONTRIBUTORS.md | 1 + 2 files changed, 11 insertions(+) diff --git a/.all-contributorsrc b/.all-contributorsrc index 4342ac5d..9e1cbe0b 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -328,6 +328,16 @@ "doc", "code" ] + }, + { + "login": "ldrf", + "name": "ldrf", + "avatar_url": "https://avatars0.githubusercontent.com/u/38339210?v=4", + "profile": "https://github.com/ldrf", + "contributions": [ + "doc", + "code" + ] } ], "commitConvention": "none" diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 39699d08..d52cdc4b 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -49,6 +49,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
GAURAV KUMAR

📖 💻
wboccard

📖 💻
d-l-mcbride

📖 💻 +
ldrf

📖 💻 From 58b8af03bce7b4ec3065b2baf75cf82bb8837e8e Mon Sep 17 00:00:00 2001 From: ldrf Date: Sat, 17 Oct 2020 21:39:50 -0300 Subject: [PATCH 3/5] Add author and date --- day34/Java/MergeSort.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/day34/Java/MergeSort.java b/day34/Java/MergeSort.java index 5706a314..29a76b01 100644 --- a/day34/Java/MergeSort.java +++ b/day34/Java/MergeSort.java @@ -1,3 +1,8 @@ +/** + * @author Lucas Freire(ldrf) + * @date 16/10/2020 + */ + import java.util.Arrays; public class MergeSort { From fc449a09e7b81715f07d6eed89c616ee0540f996 Mon Sep 17 00:00:00 2001 From: ldrf Date: Sat, 17 Oct 2020 21:40:34 -0300 Subject: [PATCH 4/5] Add Java code in README.md --- day34/README.md | 80 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/day34/README.md b/day34/README.md index 319b371f..dd93e71c 100644 --- a/day34/README.md +++ b/day34/README.md @@ -125,3 +125,83 @@ int main(){ return 0; } ``` + +### [Java Implementation](./Java/MergeSort.Java) +``` java +import java.util.Arrays; + +public class MergeSort { + + public static void main(String[] args) { + int[] arrayToOrder = new int[]{1, 5, 2, 7, 3, 4, 8, 9, 6}; + System.out.println("Array before sorting"); + System.out.println(Arrays.toString(arrayToOrder)); + int startIndex = 0; + int highIdex = arrayToOrder.length - 1; + mergeSort(arrayToOrder, startIndex, highIdex); + System.out.println("Array after sorting"); + System.out.println(Arrays.toString(arrayToOrder)); + } + + private static void mergeSort(int[] arrayToOrder, int lowIndex, + int highIndex) { + if (lowIndex < highIndex) { + int firstMidIndex = (lowIndex + highIndex) / 2; + mergeSort(arrayToOrder, lowIndex, firstMidIndex); + int secondMidIndex = firstMidIndex + 1; + mergeSort(arrayToOrder, secondMidIndex, highIndex); + merge(arrayToOrder, lowIndex, firstMidIndex, highIndex); + } + } + + private static void merge(int[] arrayToOrder, int lowIndex, int midIndex, + int highIndex) { + int leftSubArraySize = (midIndex - lowIndex) + 1; + int rightSubArraySize = highIndex - midIndex; + int[] leftArray = getArray(arrayToOrder, leftSubArraySize, lowIndex); + int[] rightArray = getArray(arrayToOrder, rightSubArraySize, + midIndex + 1); + int leftCounter = 0; + int rightCounter = 0; + while ((leftCounter < leftSubArraySize) + && (rightCounter < rightSubArraySize)) { + if (leftArray[leftCounter] <= rightArray[rightCounter]) { + arrayToOrder[lowIndex] = leftArray[leftCounter]; + ++leftCounter; + } else { + arrayToOrder[lowIndex] = rightArray[rightCounter]; + ++rightCounter; + } + ++lowIndex; + + } + + lowIndex = remainingElements(arrayToOrder, lowIndex, leftSubArraySize, + leftArray, leftCounter); + remainingElements(arrayToOrder, lowIndex, rightSubArraySize, rightArray, + rightCounter); + + } + + private static int[] getArray(int[] arrayToOrder, int arraySideSize, + int startIndex) { + int[] sideArray = new int[arraySideSize]; + for (int i = 0; i < sideArray.length; i++) { + sideArray[i] = arrayToOrder[startIndex + i]; + } + return sideArray; + } + + private static int remainingElements(int[] arrayToOrder, int startIndex, + int subArraySizeSide, int[] sideArray, int sideCounter) { + while (sideCounter < subArraySizeSide) { + arrayToOrder[startIndex] = sideArray[sideCounter]; + ++startIndex; + ++sideCounter; + } + return startIndex; + } + +} + +``` \ No newline at end of file From bdb0cfffe53991fece2ceb1482503be66feac474 Mon Sep 17 00:00:00 2001 From: ldrf Date: Sat, 17 Oct 2020 22:01:46 -0300 Subject: [PATCH 5/5] Add author and date in README.md file --- day34/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/day34/README.md b/day34/README.md index dd93e71c..becba18b 100644 --- a/day34/README.md +++ b/day34/README.md @@ -128,6 +128,11 @@ int main(){ ### [Java Implementation](./Java/MergeSort.Java) ``` java +/** + * @author Lucas Freire(ldrf) + * @date 16/10/2020 + */ + import java.util.Arrays; public class MergeSort {