Skip to content

Instantly share code, notes, and snippets.

@agilepoodle
Created February 19, 2013 15:14
Show Gist options
  • Save agilepoodle/4986753 to your computer and use it in GitHub Desktop.
Save agilepoodle/4986753 to your computer and use it in GitHub Desktop.
Horrible, dirty sort, a response to a challenge ;)
package org.jussimononen;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.junit.Test;
public class FooTest {
private final LinkedList<Integer> result = new LinkedList<Integer>();
private final List<Integer> numbers = Arrays.asList(4, 6, 7, 2, 3, 12, 34, 5, 23, 46, 67, 54, 5, 6, 87, 98, 1);
@Test
public void test() {
for (int i = 0; i < numbers.size(); i++) {
Integer current = numbers.get(i);
boolean added = false;
for (int j = 0; j < result.size(); j++) {
if (result.isEmpty()) {
result.addFirst(current);
added = true;
break;
} else if (current < result.get(j)) {
result.add(j, current);
added = true;
break;
}
}
if (added == false) {
result.addLast(current);
}
}
for (Integer num : result) {
System.out.print(num + ", ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment