Last active
December 20, 2018 03:14
-
-
Save takanakahiko/2c39babf838cad24ddd407828157d07d to your computer and use it in GitHub Desktop.
ソート課題
This file contains hidden or 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
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.io.IOException; | |
public class Main { | |
public static void main(String[] args) { | |
long base = System.currentTimeMillis(); | |
int[] array = new int[10000000]; | |
int count = 0; | |
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { | |
String line; | |
for (count = 0; (line = br.readLine()) != null; count++) { | |
array[count] = Integer.parseInt(line); | |
} | |
}catch(IOException ex){ | |
} | |
sort(array, 0 , count-1); | |
for(int i = 0; i < array.length; i++) System.out.println(array[i]); | |
System.err.println("ソート完了 : " + (base - System.currentTimeMillis()) + "ms"); | |
} | |
public static void sort(int[] a, int begin, int end) { | |
if (end <= begin) return; | |
int i = begin; | |
int less = begin; | |
int greater = end; | |
while (i <= greater){ | |
if (a[i] < a[less]) { | |
int tmp = a[i]; | |
a[i] = a[less]; | |
a[less] = tmp; | |
i++; | |
less++; | |
} else if (a[less] < a[i]){ | |
int tmp = a[i]; | |
a[i] = a[greater]; | |
a[greater] = tmp; | |
greater--; | |
} else { | |
i++; | |
} | |
} | |
sort(a, begin, less - 1); | |
sort(a, greater + 1, end); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment