标签:style blog io color ar os java sp for
import java.util.Arrays; public class QuickSort { public static void quicksort(int[] array){ quicksort(array, 0, array.length); } public static void quicksort(int[] array, int low, int high) { if (low < high) { int pivot = partition(array, low, high); quicksort(array, low, pivot - 1); quicksort(array, pivot + 1, high); } } public static void swap(int[] array, int i, int j) { if (i == j) return; int temp = array[i]; array[i] = array[j]; array[j] = temp; } public static int partition(int[] array, int low, int high) { // 当前划分位置 int partition_position = low; // 中心轴 int pivot = array[partition_position]; for (int i = low + 1; i < high; i++) { if (array[i] < pivot) { partition_position++; swap(array, partition_position, i); } } swap(array, partition_position, low); return partition_position; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] data = new int[20]; for(int i=0;i<20;i++){ data[i]=(int)(Math.random()*100); } quicksort(data); System.out.print(Arrays.toString(data)); } }
执行结果:
[3, 10, 13, 3, 25, 27, 36, 40, 19, 43, 45, 50, 44, 54, 50, 58, 62, 63, 68, 94] -----重复时有问题
标签:style blog io color ar os java sp for
原文地址:http://www.cnblogs.com/maple42/p/4080258.html