This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static String bubbleSortString(String input) { | |
if (input == null) { | |
return ""; | |
} | |
final char[] inputArray = input.toCharArray(); | |
boolean hasFinished = false; | |
while (!hasFinished) { | |
hasFinished = true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) |