Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.io.FileReader;
- import java.io.BufferedReader;
- import java.io.File;
- class Lab1 {
- public static void sort(int[] a) {
- int temp;
- boolean repeatCheck = true;
- do {
- repeatCheck = false;
- for (int i = 0; i < a.length - 1; i++) {
- temp = a[i + 1];
- if (a[i] > a[i + 1]) {
- a[i + 2] = a[i];
- a[i] = temp;
- repeatCheck = true;
- continue;
- }
- }
- } while (repeatCheck);
- }
- public static void main(String[] args) throws Exception {
- String filePath = args[0];
- File textFile = new File(filePath);
- if (args.length == 0) {
- System.out.println("ingen filnamn specifierat");
- }
- else if (!textFile.isFile()) {
- System.out.println("filen hittades ej");
- }
- else {
- FileReader file = new FileReader(filePath);
- BufferedReader reader = new BufferedReader(file);
- String numString = "";
- String currentLine = reader.readLine();
- while (currentLine != null) {
- numString += currentLine;
- currentLine = reader.readLine();
- }
- String[] strArr = numString.split(" ");
- int[] numArr = new int[strArr.length];
- for (int i = 0; i < strArr.length; i++) {
- numArr[i] = Integer.parseInt(strArr[i]);
- }
- sort(numArr);
- System.out.println("Sorted array: " + Arrays.toString(numArr));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement