快速排序

第一篇随笔就从随性写的快速排序开始吧,欢迎指正bug!

import java.util.Arrays;

public class Test {
	public static void main(String[] args) {
		int[] a = { 6, 5, 4, 7, 8, 9, 4, 3, 1, 2, 4, 0 };
		sort(a, 0, a.length - 1);
		System.out.println(Arrays.toString(a));
	}

	public static void sort(int[] a, int ii, int jj) {
		int i = ii + 1;
		int j = jj;
		while (i < j) {
			while (a[j] > a[ii]) {
				j--;
			}

			while (i <= j && a[i] <= a[ii]) {
				i++;
			}
			if (i < j) {
				int temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
			if (i > j) {
				int temp = a[ii];
				a[ii] = a[j];
				a[j] = temp;
				if ((i - ii) > 1) {
					System.out.println(ii + " " + (j - 1));
					sort(a, ii, j - 1);
				}
				if ((jj - j) > 1) {
					System.out.println((j + 1) + " " + jj);
					sort(a, j + 1, jj);
				}
			}

		}
	}

}

  

 

posted @ 2013-11-08 09:38  雨眠  阅读(179)  评论(0)    收藏  举报