Skip to content

Instantly share code, notes, and snippets.

@drakestone
Created November 27, 2014 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drakestone/64fa32344ab9c139a423 to your computer and use it in GitHub Desktop.
Save drakestone/64fa32344ab9c139a423 to your computer and use it in GitHub Desktop.
import java.util.List;
import java.util.ArrayList;
public class SortingString {
public static void main(String args[]) {
String s = "typewriter";
final long start = System.currentTimeMillis();
for (int i = 0; i < Integer.MAX_VALUE; i++) {
s = shuffle(s);
if (s.equals("eeiprrttwy")) {
System.out.println(s);
break;
}
if (i == Integer.MAX_VALUE - 1)
i = 0;
}
final long end = System.currentTimeMillis();
System.out.println("It takes " + (end - start) + " milliseconds.");
}
private static String shuffle(String input) {
List<Character> characters = new ArrayList<Character>();
for(char c:input.toCharArray()){
characters.add(c);
}
StringBuilder output = new StringBuilder(input.length());
while (characters.size() != 0) {
int randPicker = (int)(Math.random()*characters.size());
output.append(characters.remove(randPicker));
}
return output.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment