高速排序算法原理:见上图描写叙述。
代码例如以下:
package com.huan; import java.util.Arrays; import java.util.Random; public class QuickSort { public static void main(String[] args) { int[] data = new int[10]; for (int i = 0; i < 10; i++){ data[i] = new Random().nextInt(1000); } System.out.println(Arrays.toString(data)); quicksort(data, 0, data.length - 1); System.out.println(Arrays.toString(data)); } public static void quicksort(int[] data, int i, int j){ if(i < j){ int m = i, n = j; int k = data[i]; while(i < j){ while (i < j && data[j] > k){ j--; } data[i] = data[j]; while (i < j && data[i] < k){ i++; } data[j] = data[i]; } data[i] = k; quicksort(data, m, i-1); quicksort(data, i+1, n); } } }
时间: 2024-10-08 06:03:45