-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHM.java
More file actions
61 lines (45 loc) · 1.39 KB
/
HM.java
File metadata and controls
61 lines (45 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.*;
import java.io.*;
public class HM{
public static void main(String[]args){
Scanner in = new Scanner(System.in);
String input = in.nextLine();
String[] characters = input.split("\\+"); //Redex pattern for the + symbol
int[] numbers = new int[characters.length];
for(int i = 0; i < numbers.length; i++){
numbers[i] = Integer.parseInt(characters[i]);
}
//MergeSort for sorting the array
mergeSort(numbers);
String res = "";
for(int i = 0; i < numbers.length-1; i++){
res += numbers[i] + "+";
}
res+=numbers[numbers.length-1];
System.out.println(res);
}
static void mergeSort(int v[]){
mergeSort(v, 0, v.length -1); //method that we will call
}
static void mergeSort(int v[], int start, int end){
if(start < end){
int half = (start + end) / 2;
mergeSort(v, start, half);
mergeSort(v, half +1, end);
naturalMerge(v, start, half, end);
}
}
static void naturalMerge(int v[], int start, int half, int end){
int aux[] = new int[end-start+1];
int a = start, b = half+1, c = 0;
while(a <= half && b <= end){
if(v[a] < v[b]) aux[c++] = v[a++];
else aux[c++] = v[b++];
}
while(a <= half) aux[c++] = v[a++];
while(b <= end) aux[c++] = v[b++];
for(int i=0; i <aux.length; i++){
v[start+i] = aux[i];
}
}
}