标签:
1、源代码
/** * 交换排序-2.快速排序 * * @param array * 数组 * @param start * 开始序号 * @param end * 结束序号 */ public static void quickSort(int[] array, int start, int end) { if (start < end) { int key = array[start]; int i = start; for (int j = start + 1; j <= end; j++) { if (array[j] < key) { if (i + 1 != j) { // 将array[j] 与 array[i + 1]交换 array[j] = array[j] ^ array[i + 1]; array[i + 1] = array[i + 1] ^ array[j]; array[j] = array[j] ^ array[i + 1]; } i++; } } array[start] = array[i]; array[i] = key; System.out.println("键Key:" + key + ",结果:" + Arrays.toString(array)); quickSort(array, start, i - 1); quickSort(array, i + 1, end); } }
2 测试代码
@Test public void testQuickSort2() { int[] array = { 1, 8, 3, 5, 4, 6, 2, 7, 9 }; int[] expectResult = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Sort.quickSort(array, 0, 8); Assert.assertArrayEquals(expectResult, array); }
3 结果
键Key:1,结果:[1, 8, 3, 5, 4, 6, 2, 7, 9]
键Key:8,结果:[1, 7, 3, 5, 4, 6, 2, 8, 9]
键Key:7,结果:[1, 2, 3, 5, 4, 6, 7, 8, 9]
键Key:2,结果:[1, 2, 3, 5, 4, 6, 7, 8, 9]
键Key:3,结果:[1, 2, 3, 5, 4, 6, 7, 8, 9]
键Key:5,结果:[1, 2, 3, 4, 5, 6, 7, 8, 9]
标签:
原文地址:http://www.cnblogs.com/tianxue/p/4186625.html