基础思想不在复述,java的实现如下:
import java.util.Arrays; /** * Created by ykanghe on 12/27/16. */ public class algorithm { /** * 快速排序 * * @param a * @param left * @param right * @return */ public static int quickPostSort(int[] a, int left, int right) { int low = left; int high = right; int value = a[low]; while (low < high) { while (low < high && value <= a[high]) { high--; } a[low] = a[high]; while (low < high && value >= a[low]) { low++; } a[high] = a[low]; } a[low] = value; return low; } public static void quickSort(int[] a, int left, int right) { if (left < right) { int q = quickPostSort(a, left, right); quickSort(a, left, q - 1); quickSort(a, q + 1, right); } } /** * 冒泡排序 * * @param a * @return */ public static void blumSort(int[] a) { if (a.length > 0) { int tmp = 0; for (int i = 0; i < a.length - 1; i++) { for (int j = 0; j < a.length - 1 - i; j++) { if (a[j] > a[j + 1]) { tmp = a[j]; a[j] = a[j + 1]; a[j + 1] = tmp; } } } } } /** * 直接插入排序 * * @param a */ public static void insertSort(int[] a) { for (int i = 1; i < a.length; i++) { int key = a[i]; int j = i - 1; while (j >= 0 && a[j] > key) { a[j + 1] = a[j]; j--; } a[j + 1] = key; } } public static void main(String[] arg) { int[] a = {49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51}; // blumSort(a);//冒泡 // quickSort(a, 0, a.length - 1);//快速 insertSort(a);//插入排序 String s = Arrays.toString(a); System.out.println(s); } }