Skip to content

Commit

Permalink
added interrupted bubble sort
Browse files Browse the repository at this point in the history
  • Loading branch information
fmt-Println-MKO committed Jul 10, 2015
1 parent 3b7de1d commit 77cd8b6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/de/sunbits/codeeval/interruptedbubblesort/Main.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
5 changes: 5 additions & 0 deletions src/interruptedBubbleSort.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 77cd8b6

Please sign in to comment.