-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem019.java
More file actions
62 lines (49 loc) · 1.55 KB
/
problem019.java
File metadata and controls
62 lines (49 loc) · 1.55 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
package project_beuler;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class problem019 {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
String[] names = parseFile("problem019.txt");
names = mergesort(names);
System.out.println(names.length);
}
public static String[] mergesort(String[] list) {
if(list.length == 1)
return list;
String[] first = new String[list.length/2];
String[] second = new String[list.length-list.length/2];
for(int i = 0; i < list.length; i++)
if(i < list.length/2)
first[i] = list[i];
else
second[i-list.length/2] = list[i];
return merge(mergesort(first), mergesort(second));
}
public static String[] merge(String[] list1, String[] list2) {
String[] full = new String[list1.length+list2.length];
int x = 0; int y = 0;
for(int i = 0; i < full.length; i++)
if(
(y >= list2.length) ||
(x < list1.length && list1[x].compareTo(list2[y]) <= 0)
)
full[i] = list1[x++];
else
full[i] = list2[y++];
return full;
}
public static void swapString(String[] list, int left, int right) {
String temp = list[left];
list[left] = list[right];
list[right] = temp;
}
public static String[] parseFile(String dir) throws FileNotFoundException {
File file = new File(System.getProperty("user.dir")+"/src/project_beuler/"+dir);
Scanner reader = new Scanner(file);
reader.close();
String contents = reader.nextLine();
return contents.split("(\\\"(,\\\")?)");
}
}