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

Appearance settings
This repository was archived by the owner on Oct 29, 2020. It is now read-only.

Latest commit

 

History

History
History
84 lines (77 loc) · 1.38 KB

File metadata and controls

84 lines (77 loc) · 1.38 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
76
77
78
79
80
81
82
83
84
import java.util.*;
class MergeSort
{
private void merge(int[] a,int low,int high)
{
int mid=(low+high)/2;
int i=low,j=mid+1;
int[] c = new int[high-low+1];
int k=0;
while(i<=mid && j<=high)
{
if(a[i]<=a[j])
{
c[k]=a[i];
i++;
k++;
}
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-low];
}
}
public void mergesort(int[] a,int low, int high)
{
if(low>=high)
return;
else
{
int mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high);
}
}
public void print(int[] a,int n)
{
int i=0;
for(i=0;i<n;i++)
System.out.print(a[i]+" ");
}
public static void main(String[] args)
{
Scanner inputSize = new Scanner(System.in);
System.out.println("Enter number of elements:");
int size = inputSize.nextInt();
Scanner inputElements = new Scanner(System.in);
int[] a = new int[size];
for (int i = 0; i < size; i++) {
System.out.println("Enter the element " + (i + 1) + ":");
a[i] = inputElements.nextInt();
}
inputSize.close();
inputElements.close();
MergeSort mergeSort = new MergeSort();
mergeSort.mergesort(a,0,size-1);
mergeSort.print(a,size);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.