Last active
December 27, 2015 00:49
-
-
Save piggybox/7240429 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
package com.sky; | |
import java.io.*; | |
public class MinMax { | |
public static void main(String[] args) { | |
int min = 0; | |
int max = 0; | |
long before = System.currentTimeMillis(); | |
File file = new File("data"); // integers inside are ranged from -5000000 to 5000000 | |
BufferedReader reader = null; | |
try { | |
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); | |
String line = reader.readLine(); | |
while (line != null) { | |
int value = Integer.parseInt(line); | |
if (value > max) max = value; | |
if (value < min) min = value; | |
line = reader.readLine(); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (reader != null) reader.close(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
long after = System.currentTimeMillis(); | |
System.out.println("run took " + (after - before) / 1000 + " s"); | |
System.out.format("min: %d\n", min); | |
System.out.format("max: %d", max); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment