-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergesort.java
More file actions
67 lines (60 loc) · 1.23 KB
/
Mergesort.java
File metadata and controls
67 lines (60 loc) · 1.23 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.Scanner;
public class Mergesort{
public static void main( String args[])
{
int[] numbers = readNumbers();
int count = numbers.length;
numbers = sort(numbers,0,count-1);
for( int i=0 ; i < numbers.length ; i++)
System.out.println(numbers[i]);
}
public static int[] sort( int[] numbers ,int start,int end)
{
if( start == end)
return numbers;
int mid;
mid = (start+end)/2;
int L[], R[] ;
L = sort(numbers,start,mid);
R = sort( numbers, mid+1, end);
int newset[];
newset = Merge(L,R);
return newset;
}
public static int[] Merge(int[] left, int[] right)
{
int i=0,j=0,k;
int count = left.length + right.length;
int ful[] = new int[count];
for( k = 0 ; k< count; k++)
{
if(left[i] <= right[j])
{
ful[k] = left[i];
i++;
}
else
{
ful[k] = right[j];
j++;
}
}
return ful;
}
public static int[] readNumbers()
{
Scanner s = new Scanner(System.in);
int count = s.nextInt();
s.nextLine();
Scanner newS = new Scanner(s.nextLine());
int[] numbers = new int[count];
for (int i=0; i<count;i++)
{
if(newS.hasNextInt())
numbers[i] = newS.nextInt();
else
System.out.println("enough numbers not given");
}
return numbers;
}
}