Last active
September 22, 2016 15:44
-
-
Save perforb/7587643f300431b1573315710162b788 to your computer and use it in GitHub Desktop.
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
[g, j, f, v, m, n, h, b, t, s, u, z, w, p, y, d, k, r, i, c, e, x, o, l, q, a] | |
[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z] |
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
package jp.co.serialgames.pitacchi.tool; | |
import java.util.Arrays; | |
import java.util.concurrent.ThreadLocalRandom; | |
public class QuickSort { | |
private static char[] getAlphabets() { | |
int i = 0; | |
char[] alphabets = new char[26]; | |
for (char alphabet = 'a'; alphabet <= 'z'; alphabet++) { | |
alphabets[i] = alphabet; | |
i++; | |
} | |
return alphabets; | |
} | |
private static void shuffle(char[] alphabets) { | |
ThreadLocalRandom random = ThreadLocalRandom.current(); | |
for (int i = alphabets.length - 1; i > 0; i--) { | |
int index = random.nextInt(i + 1); | |
char temp = alphabets[index]; | |
alphabets[index] = alphabets[i]; | |
alphabets[i] = temp; | |
} | |
} | |
public static void qsort(int bottom, int top, char[] alphabets) { | |
if (bottom >= top) { | |
return; | |
} | |
int left, right; | |
char pivot = alphabets[bottom]; | |
for (left = bottom, right = top; left < right;) { | |
while (left <= right && alphabets[left] <= pivot) { | |
left++; | |
} | |
while (left <= right && alphabets[right] > pivot) { | |
right--; | |
} | |
if (left < right) { | |
char temp = alphabets[left]; | |
alphabets[left] = alphabets[right]; | |
alphabets[right] = temp; | |
} | |
} | |
alphabets[bottom] = alphabets[right]; | |
alphabets[right] = pivot; | |
qsort(bottom, right - 1, alphabets); | |
qsort(right + 1, top, alphabets); | |
} | |
public static void main(String[] args) { | |
char[] alphabets = getAlphabets(); | |
shuffle(alphabets); | |
System.out.println(Arrays.toString(alphabets)); | |
qsort(0, alphabets.length - 1, alphabets); | |
System.out.println(Arrays.toString(alphabets)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment