- public class Quicksort {
- private static int[] S = new int[] { 10, 12, 13, 7, 6, 5, 4, 3, 2, 1 };
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int i = 0;
- int j = S.length - 1;
- part(i, j, S);
- print();
- }
- /**
- * 返回调整后key的位置
- *
- * @param i
- * @param j
- * @param key
- * @return
- */
- private static void part(int l, int r, int A[]) {
- if (l < r) {
- int i = l, j = r;
- int key = A[i];
- while (i < j) {
- while (i < j && A[j] > key) {
- j--;
- }
- if (i < j) {
- A[i++] = A[j];
- }
- while (A[i] < key && i < j) {
- i++;
- }
- if (i < j) {
- A[j--] = A[i];
- }
- }
- A[i] = key;
- if (i - 1 - l > 0) {
- part(l, i - 1, A);
- }
- if (r - i - 1 > 0) {
- part(i + 1, r, A);
- }
- }
- }
- private static void print() {
- for (int j = 0; j < S.length; j++) {
- System.out.print(" " + S[j]);
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0804201512146.html
来源: http://www.codesnippet.cn/detail/0804201512146.html