This repository was archived by the owner on Dec 7, 2024. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
Latest commit
45 lines (40 loc) · 1.11 KB
/
Copy pathMergeSort.java
File metadata and controls
45 lines (40 loc) · 1.11 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
packagecom.ematrix;
importjava.sql.Array;
importjava.util.Arrays;
importjava.util.Random;
publicclassMergeSort {
/* Java program for Merge Sort */
publicvoidmerge(int[] sOne,int[] sTwo,int[]S){
inti=0;
intj=0;
while(i+j<S.length){
if(j==sTwo.length||(i<sOne.length&&(sOne[i]<sTwo[j]))){
S[j+i]=sOne[i++];
}else{
S[j+i]=sTwo[j++];
}
}
}
publicvoidsort(int[] S){
if(S.length<2){
return;
}
intmid= S.length/2;
int[] sOne=Arrays.copyOfRange(S,0,mid);
int[] sTwo=Arrays.copyOfRange(S,mid,S.length);
sort(sOne);
sort(sTwo);
merge(sOne,sTwo,S);
}
publicstaticvoidmain(String[] args){
int[] ary=newint[10000000];
Randomrand =newRandom();
rand.setSeed(927838837377388388L);
for(intx=0;x<10000000;x++)
ary[x]=rand.nextInt(10000000);
MergeSortmergeSort=newMergeSort();
mergeSort.sort(ary);
for(intitem:ary)
System.out.print(item+",");
}
}