package test import java util Arrays Created by GrabetsB on 24 02 2015

 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
62
63
64
package test;
import java.util.Arrays;
/**
* Created by GrabetsB on 24.02.2015.
*/
public class Summ
{
public static int summ1=0;
public static void mergeSort(int[] a, int low, int high) {
if (low+1 < high) {
int mid = (low + high) >>> 1;
mergeSort(a, low, mid);
mergeSort(a, mid, high);
int size = high - low;
int[] b = new int[size];
int i = low;
int j = mid;
for (int k = 0; k < size; k++) {
if (j >= high || i < mid && a[i] < a[j]) {
b[k] = a[i++];
} else {
b[k] = a[j++];
}
}
System.arraycopy(b, 0, a, low, size);
}
}
public static void summ(int[] a){
//System.out.println("-----");
if (a.length>1){
int mid = (0+a.length)>>>1;
int[] b = new int[mid];
int[] c = new int[a.length-mid];
System.arraycopy(a, 0, b, 0, mid);
System.arraycopy(a, mid, c, 0, a.length-mid);
//test
/*System.out.println(Arrays.toString(b));
System.out.println(Arrays.toString(c));*/
summ(b);
summ(c);
if (b.length==1){
summ1+= b[0];
}
if (c.length==1){
summ1+=c[0];
}
}
}
public static void main(String[] args)
{
int[] array = new int[]{2,-1,3,24,5,7234,234,0,-2501};
//mergeSort(array,0,array.length);
System.out.println(Arrays.toString(array));
summ(array);
System.out.println(summ1);
}
}