排序算法
1.选择排序
/** * 选择排序。 * * @author daniel * @date 2013-12-1 下午8:35:23 * @since 0.1 * @version 0.1 */ public class SelectionSort { public static void sel_sort(int x[], int n) { for (; n > 1; n--) {//基于数的个数进行循环,每次减少一个数 int i_max = 0;//i_max用于保存最大元素的下标,首先假设第0个元素最大 for (int i = 1; i < n; i++) {//循环找前n个数中的最大元素 if (x[i] > x[i_max]) i_max = i;//修改i_max的值,使其一直为最大元素的下标 } //交换x[i_max]和x[n-1]的值 int temp = x[i_max]; x[i_max] = x[n - 1]; x[n - 1] = temp; } } /** * @param args */ public static void main(String[] args) { int a[] = { 8, 3, 4, 3, 2, 5, 6, 7, 3, 5, 5, 5, 9 }; System.out.println("before sort"); for (int each : a) { System.out.print(each + " "); } sel_sort(a, a.length); System.out.println("\nafter sort"); for (int each : a) { System.out.print(each + " "); } } }
2.快速排序
/** * 快速排序。 * * @author daniel * @date 2013-12-1 下午10:28:12 * @since 0.1 * @version 0.1 */ public class QuickSort { static void quick_sort(int x[], int first, int last) { if (first < last) { int split_point = split(x, first, last); quick_sort(x, first, split_point - 1); quick_sort(x, split_point + 1, last); } } static int split(int x[], int first, int last) { int split_point, pivot; pivot = x[first]; split_point = first; for (int unknown = first + 1; unknown <= last; unknown++) { if (x[unknown] < pivot) { split_point++; // 交换x[split_point]与x[unknown]的值 int t = x[split_point]; x[split_point] = x[unknown]; x[unknown] = t; } } // 交换x[first]和x[split_point]的值 x[first] = x[split_point]; x[split_point] = pivot; return split_point; } /** * @param args */ public static void main(String[] args) { int a[] = { 8, 3, 4, 3, 2, 5, 6, 7, 3, 5, 5, 5, 9 }; System.out.println("before sort"); for (int each : a) { System.out.print(each + " "); } quick_sort(a, 0, a.length - 1); System.out.println("\nafter sort"); for (int each : a) { System.out.print(each + " "); } } }