直接贴源代码:
package com.java.fmd; import java.util.Arrays; import java.util.Scanner; public class QuickSort { public static int b=1; public static void swap(int[] nums, int i, int j) { if (i == j) { return; } int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; } public static void quickSort(int[] nums, int low, int high) { if (low > high) { return; } int i = low, j = high; while (i < j) { while (i < j && nums[j] >= nums[low]) { j--; } while (i < j && nums[i] <= nums[low]) { i++; } if (i < j) { swap(nums, i, j); } } System.out.println("第"+b+"次排序结果:"); System.out.println(Arrays.toString(nums)); b++; swap(nums, low, i); quickSort(nums, low, i - 1); quickSort(nums, i + 1, high); } public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.println("请输入数组的个数:"); int n=scan.nextInt(); System.out.println("请输入数组:"); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=scan.nextInt(); } System.out.println("排序结果为:"); quickSort(a, 0, a.length - 1); }
}
运行结果: