Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 34, Java #365

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
<td align="center"><a href="https://github.com/gaurav01k3"><img src="https://avatars0.githubusercontent.com/u/67451786?v=4?s=100" width="100px;" alt=""/><br /><sub><b>GAURAV KUMAR</b></sub></a><br /><a href="https://github.com/CodeToExpress/dailycodebase/commits?author=gaurav01k3" title="Documentation">📖</a> <a href="https://github.com/CodeToExpress/dailycodebase/commits?author=gaurav01k3" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/wboccard"><img src="https://avatars3.githubusercontent.com/u/72316984?v=4?s=100" width="100px;" alt=""/><br /><sub><b>wboccard</b></sub></a><br /><a href="https://github.com/CodeToExpress/dailycodebase/commits?author=wboccard" title="Documentation">📖</a> <a href="https://github.com/CodeToExpress/dailycodebase/commits?author=wboccard" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/d-l-mcbride"><img src="https://avatars3.githubusercontent.com/u/46550732?v=4?s=100" width="100px;" alt=""/><br /><sub><b>d-l-mcbride</b></sub></a><br /><a href="https://github.com/CodeToExpress/dailycodebase/commits?author=d-l-mcbride" title="Documentation">📖</a> <a href="https://github.com/CodeToExpress/dailycodebase/commits?author=d-l-mcbride" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/ldrf"><img src="https://avatars0.githubusercontent.com/u/38339210?v=4?s=100" width="100px;" alt=""/><br /><sub><b>ldrf</b></sub></a><br /><a href="https://github.com/CodeToExpress/dailycodebase/commits?author=ldrf" title="Documentation">📖</a> <a href="https://github.com/CodeToExpress/dailycodebase/commits?author=ldrf" title="Code">💻</a></td>
</tr>
</table>

Expand Down
80 changes: 80 additions & 0 deletions day34/Java/MergeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* @author Lucas Freire(ldrf)
* @date 16/10/2020
*/

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;
}

}
85 changes: 85 additions & 0 deletions day34/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,88 @@ int main(){
return 0;
}
```

### [Java Implementation](./Java/MergeSort.Java)
``` java
/**
* @author Lucas Freire(ldrf)
* @date 16/10/2020
*/

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;
}

}

```