Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 77838fb

Browse files
Conclusion of the custom implementation of MergeSort
1 parent 42d4fd6 commit 77838fb

File tree

3 files changed

+21
-57
lines changed

3 files changed

+21
-57
lines changed

‎src/main/java/br/com/zevolution/algorithms/sorting/mergesort/MergeSort.java

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,29 @@
11
package br.com.zevolution.algorithms.sorting.mergesort;
22

3-
import java.util.Arrays;
3+
import br.com.zevolution.algorithms.sorting.Product;
4+
import br.com.zevolution.algorithms.sorting.Sort;
45

5-
importbr.com.zevolution.algorithms.sorting.insertionsort.Product;
6+
publicclassMergeSortimplementsSort {
67

7-
public class MergeSort {
8-
9-
public static void main(String[] args) {
10-
11-
// Unsorted array
12-
Product[] products = {
13-
new Product("product3", 5),
14-
new Product("product2", 4),
15-
new Product("product6", 9),
16-
new Product("product5", 8),
17-
new Product("product4", 6),
18-
new Product("product1", 3),
19-
new Product("product9", 10),
20-
new Product("product8", 9.7),
21-
new Product("product7", 9.3)
22-
};
23-
24-
// mergeSort(products, 0, 1, 2);
25-
// mergeSort(products, 2, 3, 4);
26-
// mergeSort(products, 4, 5, 6);
27-
// mergeSort(products, 6, 7, 8);
28-
// mergeSort(products, 0, 4, 8);
29-
Product[] sorted = sort(products, products.length);
30-
// array = mergeSort(array, 0, 2, 4);
31-
32-
System.out.println(Arrays.toString(sorted));
33-
34-
}
35-
36-
public static Product[] sort(Product[] products, int length) {
8+
public Product[] sort(Product[] products, int length) {
379
Product[] array = products.clone();
3810
mergeSort(array, 0, length);
3911
return array;
4012
}
4113

42-
private staticvoid mergeSort(Product[] products, int start, int end) {
14+
private void mergeSort(Product[] products, int start, int end) {
4315
int length = end - start;
4416

4517
if (length > 1) {
46-
int middle = (start + end) >> 1;// as well as (start + end) / 2
18+
int middle = (start + end) >> 1;
4719

4820
mergeSort(products, start, middle);
4921
mergeSort(products, middle, end);
5022
mergeSort(products, start, middle, end);
5123
}
5224
}
5325

54-
private staticvoid mergeSort(Product[] products, int low, int middle, int high) {
26+
private void mergeSort(Product[] products, int low, int middle, int high) {
5527
Product[] array = new Product[high-low];
5628

5729
int current = 0;

‎src/main/java/br/com/zevolution/algorithms/sorting/mergesort/Product.java

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package br.com.zevolution.algorithms.sorting.mergesort;
2+
3+
import static br.com.zevolution.algorithms.sorting.SortTester.testSort;
4+
5+
import org.junit.Test;
6+
7+
public class MergeSortTest {
8+
9+
@Test
10+
public void should_Sort_Array() {
11+
testSort(new MergeSort());
12+
}
13+
14+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /