Created
November 27, 2014 04:47
-
-
Save drakestone/64fa32344ab9c139a423 to your computer and use it in GitHub Desktop.
This file contains 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
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
https://www.facebook.com/photo.php?fbid=10205462329147142&set=gm.733999690010059&type=1&theater
ㅎㅎ