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 42d4fd6

Browse files
Implemented recursive algorithms concept in MergeSort
1 parent e9729e5 commit 42d4fd6

File tree

1 file changed

+33
-11
lines changed
  • src/main/java/br/com/zevolution/algorithms/sorting/mergesort

1 file changed

+33
-11
lines changed

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.Arrays;
44

5+
import br.com.zevolution.algorithms.sorting.insertionsort.Product;
6+
57
public class MergeSort {
68

79
public static void main(String[] args) {
@@ -14,29 +16,49 @@ public static void main(String[] args) {
1416
new Product("product5", 8),
1517
new Product("product4", 6),
1618
new Product("product1", 3),
17-
new Product("product8", 10),
19+
new Product("product9", 10),
20+
new Product("product8", 9.7),
1821
new Product("product7", 9.3)
1922
};
2023

21-
mergeSort(products, 0, 1, 2);
22-
mergeSort(products, 2, 3, 4);
23-
mergeSort(products, 4, 5, 6);
24-
mergeSort(products, 6, 7, 8);
25-
mergeSort(products, 0, 4, 8);
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);
2630
// array = mergeSort(array, 0, 2, 4);
2731

28-
System.out.println(Arrays.toString(products));
32+
System.out.println(Arrays.toString(sorted));
33+
34+
}
35+
36+
public static Product[] sort(Product[] products, int length) {
37+
Product[] array = products.clone();
38+
mergeSort(array, 0, length);
39+
return array;
40+
}
41+
42+
private static void mergeSort(Product[] products, int start, int end) {
43+
int length = end - start;
2944

45+
if (length > 1) {
46+
int middle = (start + end) >> 1; // as well as (start + end) / 2
47+
48+
mergeSort(products, start, middle);
49+
mergeSort(products, middle, end);
50+
mergeSort(products, start, middle, end);
51+
}
3052
}
3153

32-
private static void mergeSort(Product[] products, int low, int medium, int high) {
54+
private static void mergeSort(Product[] products, int low, int middle, int high) {
3355
Product[] array = new Product[high-low];
3456

3557
int current = 0;
3658
int left = low;
37-
int right = medium;
59+
int right = middle;
3860

39-
while (left < medium && right < high) {
61+
while (left < middle && right < high) {
4062
if (products[left].getPrice() < products[right].getPrice()) {
4163
array[current] = products[left];
4264
left++;
@@ -47,7 +69,7 @@ private static void mergeSort(Product[] products, int low, int medium, int high)
4769
current++;
4870
}
4971

50-
while (left < medium) {
72+
while (left < middle) {
5173
array[current] = products[left];
5274
left++;
5375
current++;

0 commit comments

Comments
(0)

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