Skip to content

Instantly share code, notes, and snippets.

Teginst
,Haaaeeeeffhiijllmmmnnoorsstuwyy
[0, 1, 1, 3, 5, 6, 6, 6, 8, 9]
[0, 1, 1, 3, 5, 6, 6, 6, 8, 9]
[The, brown, fox, jumps, over, quick, lazy, do, the]
// character sort of a String
System.out.println(bubbleSortString("Testing"));
System.out.println(bubbleSortString("Hello my name is jeff, what is your name"));
// List<Integer> sort
Integer[] phoneNumber = new Integer[] {8,0,1,6,6,1,5,9,3,6};
List<Integer> listOne = Arrays.asList(phoneNumber);
System.out.println(bubbleSortIntegerList(listOne));
// Generic sort (Integer example)
private static <T extends Comparable<T>> List<T> bubbleSortGeneric(List<T> input) {
if (input == null) {
return new ArrayList<T>();
}
boolean hasFinished = false;
while (!hasFinished) {
hasFinished = true;
for (int i = 0; i < input.size() - 1; i++) {
T first = input.get(i);
private static List<Integer> bubbleSortIntegerList(List<Integer> input) {
if (input == null) {
return Collections.emptyList();
}
boolean hasFinished = false;
while (!hasFinished) {
hasFinished = true; // set to a clean state until a change is found
for (int i = 0; i < input.size()-1; i++) {
final Integer first = input.get(i);
private static String bubbleSortString(String input) {
if (input == null) {
return "";
}
final char[] inputArray = input.toCharArray();
boolean hasFinished = false;
while (!hasFinished) {
hasFinished = true;
/**
* Simple runnable which attempts to process 10 records, with a 1 second wait in between each record. However, if it
* is interrupted, then it stops sleeping and gracefully completes.
*/
private static class InterruptableRunnable implements Runnable {
private static Logger LOGGER = Logger.getLogger(InterruptableRunnable.class);
private final int numToProcess;
/**
* Runnable which cannot be easily interrupted, since it does not handle the InterruptedException properly.
*/
private static class NonInterruptableRunnable implements Runnable {
private static Logger LOGGER = Logger.getLogger(NonInterruptableRunnable.class);
private final int numToProcess;
private final long sleepDuration;
@jmartin127
jmartin127 / BadHandlingOfThreadInterruptedException.java
Last active August 29, 2015 14:02
Bad handling of InterruptedException
try {
Thread.sleep(milliSecondsToSleep);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
// Do NOT just print the stack trace here, this is BAD practice
e.printStackTrace();
}
@jmartin127
jmartin127 / SleepingRunnable.java
Created June 14, 2014 23:49
Simple runnable which sleeps for 30 seconds
/**
* Simple runnable which attempts to sleep for 30 seconds. However, if it is interrupted, then it stops sleeping and
* gracefully completes.
*/
private static class SleepingRunnable implements Runnable {
private static Logger LOGGER = Logger.getLogger(SleepingRunnable.class);
/*
* (non-Javadoc)