-
Notifications
You must be signed in to change notification settings - Fork 2
/
Mergesort.java
75 lines (63 loc) · 1.82 KB
/
Mergesort.java
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
65
66
67
68
69
70
71
72
73
74
75
package com.programs;
import java.util.*;
public class Mergesort {
static int max = 50000;
public static void mergeSort(int a[],int low,int high) {
int mid;
if(low<high) {
mid = (low+high)/2;
mergeSort(a,low,mid);
mergeSort(a,mid+1,high);
merge(a,low,mid,high);
}
}
public static void merge(int a[],int low,int mid,int high) {
int k=low,j=mid+1,i=low;
int c[] = new int[1000];
while((i<=mid)&&(j<=high)) {
if(a[i]<=a[j]) {
c[k] = a[i];
i++;
}
else {
c[k] = a[j];
j++;
}
k++;
}
while(i<=mid) {
c[k] = a[i];
k++;i++;
}
while(j<=high) {
c[k] = a[j];
k++;j++;
}
for(i=low;i<=high;i++)
a[i] = c[i];
}
public static void main(String args[]) {
int n,i;
System.out.println("Enter the size:");
Scanner in = new Scanner(System.in);
n = in.nextInt();
Random rand = new Random();
int a[] = new int[max];
for(i=0;i<n;i++)
a[i] = rand.nextInt(100);
System.out.println("The random elements are:");
for(i=0;i<n;i++)
System.out.println(a[i]);
try {
long start_time = System.nanoTime();
mergeSort(a,0,n-1);
long end_time = System.nanoTime();
System.out.println("The sorted array:");
for(i=0;i<n;i++)
System.out.println(a[i]);
System.out.println("Time taken: "+(end_time-start_time)+" nanoseconds");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index max reached");
}
}
}