diff --git a/src/de/sunbits/codeeval/interruptedbubblesort/Main.java b/src/de/sunbits/codeeval/interruptedbubblesort/Main.java new file mode 100644 index 0000000..8f2cd1b --- /dev/null +++ b/src/de/sunbits/codeeval/interruptedbubblesort/Main.java @@ -0,0 +1,62 @@ +package de.sunbits.codeeval.interruptedbubblesort; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.regex.Pattern; + +public class Main { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + final Pattern patRounds = Pattern.compile("\\|"); + final Pattern patSpace = Pattern.compile(" "); + + try { + final BufferedReader inputStream = new BufferedReader(new FileReader(args[0])); + String line; + while ((line = inputStream.readLine()) != null) { + String[] lData = patRounds.split(line, 0); + long rounds = Long.valueOf(lData[1].trim()); + String[] data = patSpace.split(lData[0], 0); + + if (rounds > data.length) { + rounds = data.length; + } + int[] ar = new int[data.length]; + + for (int j = 0; j < rounds; j++) { + for (int i = 0; i < ar.length - 1; i++) { + int v1; + int v2; + if (j == 0 && i == 0) { + v1 = Integer.valueOf(data[i]); + v2 = Integer.valueOf(data[i + 1]); + } else if (j == 0 && i > 0) { + v1 = ar[i]; + v2 = Integer.valueOf(data[i + 1]); + } else { + v1 = ar[i]; + v2 = ar[i + 1]; + } + if (v1 < v2) { + ar[i] = v1; + ar[i + 1] = v2; + } else { + ar[i] = v2; + ar[i + 1] = v1; + } + } + } + for (int i = 0; i < ar.length; i++) { + System.out.print(ar[i] + " "); + } + System.out.println(); + } + } catch (java.io.IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/interruptedBubbleSort.txt b/src/interruptedBubbleSort.txt new file mode 100644 index 0000000..05e77d8 --- /dev/null +++ b/src/interruptedBubbleSort.txt @@ -0,0 +1,5 @@ +36 47 78 28 20 79 87 16 8 45 72 69 81 66 60 8 3 86 90 90 | 1 +40 69 52 42 24 16 66 | 2 +54 46 0 34 15 48 47 53 25 18 50 5 21 76 62 48 74 1 43 74 78 29 | 6 +48 51 5 61 18 | 2 +59 68 55 31 73 4 1 25 26 19 60 0 | 1000000000000 \ No newline at end of file