package edu.jlu.hgd.sort; public class QuickSort { public static int partition(int a[], int p, int r) {// 找到分点 int i, j, k; int x = a[r]; i = p - 1; for (j = p; j < r; j++) { if (a[j] < x) { swap(a, j, ++i); } } swap(a, ++i, r); return i; } static void swap(int[] a, int i, int j) { // 交换 int temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void qSort(int a[], int p, int r) { // 主体部分 int mid = partition(a, p, r); if (p < mid - 1) { print(a); qSort(a, p, mid - 1); } if (r > mid + 1) { qSort(a, mid + 1, r); } } public static void print(int a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + ","); } System.out.println(); } public static void main(String[] args) { int a[] = { 29, 2, 3, 1, 7, 5, 9, 4, 8, 15 }; qSort(a, 0, a.length - 1); System.out.println("the result is"); print(a); } } package edu.jlu.hgd.sort; public class ShellSort { public static void shellSort(int a[], int l, int r) { int h; for (h = 1; h < r / 9; h = 3 * h + 1) //构造步长序列 ; System.out.println(h); for (; h > 0; h = h / 3) { for (int i = l + h; i < r; i++) { //进行-排序 int j = i; int temp = a[i]; while (j >= l + h && temp < a[j - h]) { //循环找到插入位置 a[j] = a[j - h]; j = j - h; } a[j] = temp; //在位置j处完成插入 } } } public static void main(String[] args) { int[] a = { 1, 2, 3, 5, 9, 4, 8, 7, 24, 56, 78, 65, 43, 29, 11, 10, 97, 64, 26, 2, 3, 5, 9, 4, 8, 7, 24, 56, 78, 65, 43, 29, 11, 10, 97, 64, 26,2, 3, 5, 9, 4, 8, 7, 24, 56, 78, 65, 43, 29, 11, 10, 97, 64, 26 }; shellSort(a, 0, a.length); for (int i = 0; i < a.length; i++) { System.out.print(a[i]+","); } } }