Created
October 8, 2015 01:26
-
-
Save twinkfrag/67c8ddd51a562d3013ab to your computer and use it in GitHub Desktop.
アルゴ演習15-3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by @twinkfrag on 2015/09/26. | |
*/ | |
import java.nio.file.*; | |
import java.util.*; | |
import java.io.*; | |
import java.util.stream.*; | |
/** | |
* アルゴ演習15の課題に関して、解説とJava8などでの改良点 | |
* 問題に対する解答として間違っている可能性もあるので注意 | |
* <p> | |
* 3週目 | |
*/ | |
public class Algo15_ex3 { | |
public static void main(String[] args) { | |
TestSort.main(args); | |
} | |
} | |
abstract class SortableBase { | |
protected int[] array; | |
public SortableBase(String filename) { | |
try (Stream<String> st = Files.lines(Paths.get(filename))) { | |
array = st.mapToInt(Integer::parseInt).toArray(); | |
} catch (IOException | NumberFormatException e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
} | |
public abstract void sort(); | |
public void output(String filename) { | |
List<String> outList = Arrays.stream(array).<String>mapToObj(String::valueOf).collect(Collectors.toList()); | |
try { | |
Files.write(Paths.get(filename), outList); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
class BubbleSort extends SortableBase { | |
public BubbleSort(String filename) { | |
super(filename); | |
} | |
@Override | |
public void sort() { | |
for (int i = 0; i < array.length; i++) { | |
for (int j = array.length - 1; j > i; j--) { | |
if (array[j] < array[i]) { | |
int temp = array[i]; | |
array[i] = array[j]; | |
array[j] = temp; | |
} | |
} | |
} | |
} | |
} | |
class SelectionSort extends SortableBase { | |
public SelectionSort(String filename) { | |
super(filename); | |
} | |
@Override | |
public void sort() { | |
for (int i = 0; i < array.length; i++) { | |
int minIndex = i; | |
for (int j = i + 1; j < array.length; j++) { | |
if (array[j] < array[minIndex]) minIndex = j; | |
} | |
int temp = array[i]; | |
array[i] = array[minIndex]; | |
array[minIndex] = temp; | |
} | |
} | |
} | |
class InsertionSort extends SortableBase { | |
public InsertionSort(String filename) { | |
super(filename); | |
} | |
@Override | |
public void sort() { | |
for (int i = 1; i < array.length; i++) { | |
for (int j = i; j > 0; j--) { | |
if (array[j - 1] < array[j]) break; | |
int temp = array[j - 1]; | |
array[j - 1] = array[j]; | |
array[j] = temp; | |
} | |
} | |
} | |
} | |
class TestSort { | |
public static void main(String[] args) { | |
long start, stop; | |
String file1 = "rand2.txt"; | |
String file2 = "sorted2.txt"; | |
String file3 = "reverse2.txt"; | |
String file4 = "same2.txt"; | |
List<String> files = Arrays.asList(file1, file2, file3, file4); | |
files.forEach(filename -> { | |
List<SortableBase> sorts = Arrays.asList( | |
new BubbleSort(filename), | |
new SelectionSort(filename), | |
new InsertionSort(filename) | |
); | |
System.out.println(String.format("%sのソート", filename)); | |
sorts.forEach(sort -> { | |
long f = System.currentTimeMillis(); | |
sort.sort(); | |
long e = System.currentTimeMillis(); | |
System.out.println(String.format("%s : %d [ms]", sort.getClass().getName(), e - f)); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment