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

Latest commit

 

History

History
History
108 lines (86 loc) · 1.48 KB

File metadata and controls

108 lines (86 loc) · 1.48 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.util.*;
class MergeSort
{
public void sort(int a[], int low, int high)
{
if(low<high)
{
int mid=(low+high)/2;
sort(a,low,mid);
sort(a,mid+1,high);
merge(a,low,mid,high);
}
}
public void merge(int a[], int low, int mid, int high)
{
int h=low, i=low, j=mid+1,k;
int b[]=new int[high+1];
while((h<=mid) && (j<=high))
{
if(a[h]<a[j])
{
b[i]=a[h];
h=h+1;
}
else
{
b[i]=a[j];
j=j+1;
}
i=i+1;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i=i+1;
}
}
if(j>high)
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i=i+1;
}
}
for(k=low;k<=high;k++)
{
a[k]=b[k];
}
}
static void printArray(int a[], int n)
{
for(int i=0;i<n;i++)
{
System.out.print( a[i]+ "\t");
}
}
public static void main(String args[])
{
Scanner scan= new Scanner(System.in);
try{
int low,high;
int n,i;
System.out.println("How many elements do you want in the array?");
n=scan.nextInt();
int a[]= new int[n];
System.out.println("Enter the array elements one by one");
for(i=0;i<n;i++)
{
a[i]=scan.nextInt();
}
System.out.println("Array before sorting is:");
printArray(a, n);
low=0;
high=n-1;
MergeSort obj = new MergeSort();
obj.sort(a, low, high);
System.out.println();
System.out.println("The sorted array is: ");
printArray(a, n);
}catch(Exception e){e.printStackTrace();}
scan.close();
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.