package com.wirthy.spring.boot.distributed.sort; public class QuickSort { public static void main(String[] args) { int[] array = {30, 10, 80, 70, 5, 20, 40, 90, 1, 50}; quickSort(array, 0, array.length - 1); } public static void printArray(int[] array){ for (int i = 0; i < array.length; i++) { System.out.printf("%4d", array[i]); } System.out.println(); } public static void quickSort(int[] array, int low, int high){ if(low < high){ int pi = partition(array, low, high); printArray(array); quickSort(array, low, pi-1); quickSort(array, pi+1, high); } } public static int partition(int[] array, int low, int high){ int pivot = array[high]; int lowIndex = low - 1; for (int j = low; j<= high - 1; j++) { if(array[j] < pivot){ lowIndex++; int temp = array[lowIndex]; array[lowIndex] = array[j]; array[j] = temp; } } int temp = array[lowIndex + 1]; array[lowIndex + 1] = array[high]; array[high] = temp; return lowIndex + 1; } } 运行结果: 30 10 5 20 40 1 50 90 70 80 1 10 5 20 40 30 50 90 70 80 1 10 5 20 30 40 50 90 70 80 1 10 5 20 30 40 50 90 70 80 1 5 10 20 30 40 50 90 70 80 1 5 10 20 30 40 50 70 80 90
转载于:https://my.oschina.net/aspiretruth/blog/2986465