Java几种排序算法:
package sort; public class Sorter { /** * 冒泡排序 * * @param a * 待排序数组 * @return 排序后的数组 */ public static int[] bubbleSort(int[] a) { System.out.println("冒泡排序:"); int in = 0; int out = 0; int length = a.length; for (out = length - 1; out > 1; out--) { for (in = 0; in < out; in++) { if (a[in] > a[in + 1]) { int temp = a[in]; a[in] = a[in + 1]; a[in + 1] = temp; } } } return a; } /** * 选择排序 * * @param a * 待排序数组 * @return 排序后的数组 */ public static int[] selectSort(int[] a) { System.out.println("选择排序:"); int in = 0; int min = 0; int out = 0; int length = a.length; for (out = 0; out < length - 1; out++) { min = out; for (in = out + 1; in < length; in++) { if (a[min] > a[in]) { min = in; } } int temp = a[out]; a[out] = a[min]; a[min] = temp; } return a; } /** * 插入排序 * * @param a * 待排序数组 * @return 排序后的数组 */ public static int[] insertSort(int[] a) { System.out.println("插入排序:"); int in = 0; int out = 0; int length = a.length; for (out = 1; out < length; out++) { in = out; int temp = a[out]; while (in > 0 && a[in - 1] > temp) { a[in] = a[in - 1]; in--; } a[in] = temp; } return a; } /** * 归并排序 * * @param a * 待排序数组 * @return 排序后的数组 */ public static int[] mergeSort(int[] a) { int[] workSpace = new int[a.length]; recMergeSort(a, workSpace, 0, a.length - 1); return a; } /** * 归并排序的递归方法 * * @param source * 待排序数组 * @param workSpace * 缓存区 * @param lowerBound * 左边界 * @param upperBound * 右边界 */ private static void recMergeSort(int[] source, int[] workSpace, int lowerBound, int upperBound) { // TODO Auto-generated method stub if (lowerBound == upperBound) { return; } else { int mid = (lowerBound + upperBound) / 2; System.out.println("排序前半部分:" + lowerBound + "-" + mid); recMergeSort(source, workSpace, lowerBound, mid); System.out.println("排序后半部分:" + (mid + 1) + "-" + upperBound); recMergeSort(source, workSpace, mid + 1, upperBound); merge(source, workSpace, lowerBound, mid + 1, upperBound); } } /** * 归并两个数组 * * @param source * 待排序数组 * @param workSpcae * 缓存区 * @param lowerPtr * 左边界 * @param highPtr * 中间位置 * @param upperBound * 右边界 */ private static void merge(int[] source, int[] workSpcae, int lowerPtr, int highPtr, int upperBound) { // TODO Auto-generated method stub int j = 0; int lowerBound = lowerPtr; int mid = highPtr - 1; int n = upperBound - lowerBound + 1; System.out.println("开始归并:lowerBound=" + lowerPtr + ";mid=" + mid + ";upperBound=" + upperBound); while (lowerPtr <= mid && highPtr <= upperBound) { if (source[lowerPtr] < source[highPtr]) { workSpcae[j++] = source[lowerPtr++]; } else { workSpcae[j++] = source[highPtr++]; } } while (lowerPtr <= mid) { workSpcae[j++] = source[lowerPtr++]; } while (highPtr <= upperBound) { workSpcae[j++] = source[highPtr++]; } System.out.print("归并后:"); for (j = 0; j < n; j++) { source[lowerBound + j] = workSpcae[j]; System.out.print(source[lowerBound + j] + ","); } System.out.println(); } /** * 希尔排序 * * @param a * 待排序数组 * @return 排序后的数组 */ public static void shellSort(int a[]) { System.out.println("希尔排序:"); int in; int out; int h = 1; int temp; int nItems = a.length; /** 初始增量 */ while (h <= nItems / 3) { h = h * 3 + 1; } while (h > 0) { for (out = h; out < nItems; out++) { temp = a[out]; in = out; while (in > h - 1 && temp <= a[in - h]) { a[in] = a[in - h]; in -= h; } a[in] = temp; } h = (h - 1) / 3; } } /** * 快速排序-三数据项取中值划分法 * * @param a * 待排序数组 */ public static int[] quickSort(int[] a) { System.out.println("快速排序-三数据项取中值划分法:"); recQuickSort(a, 0, a.length - 1); return a; } private static void recQuickSort(int[] a, int left, int right) { // TODO Auto-generated method stub int size = right - left + 1; if (size <= 3) { manualSort(a, left, right); } else { int median = mediaOf3(a, left, right); int partion = partitionIt(a, left, right, median); recQuickSort(a, left, partion - 1); recQuickSort(a, partion + 1, right); } } private static int partitionIt(int[] a, int left, int right, int median) { // TODO Auto-generated method stub int leftPtr = left; int rightPtr = right - 1; while (true) { while (a[++leftPtr] < median) ; while (a[--rightPtr] > median) ; if (leftPtr >= rightPtr) { break; } else { int temp = a[leftPtr]; a[leftPtr] = a[rightPtr]; a[rightPtr] = temp; } } int temp = a[leftPtr]; a[leftPtr] = a[right - 1]; a[right - 1] = temp; return leftPtr; } private static int mediaOf3(int[] a, int left, int right) { // TODO Auto-generated method stub int mid = (left + right) / 2; if (a[left] > a[mid]) { int temp = a[left]; a[left] = a[mid]; a[mid] = temp; } if (a[left] > a[right]) { int temp = a[left]; a[left] = a[right]; a[right] = temp; } if (a[mid] > a[right]) { int temp = a[mid]; a[mid] = a[right]; a[right] = temp; } int temp = a[mid]; a[mid] = a[right - 1]; a[right - 1] = temp; return a[right - 1]; } private static void manualSort(int[] a, int left, int right) { // TODO Auto-generated method stub int size = right - left + 1; if (size <= 1) { return; } else if (size == 2) { if (a[left] > a[right]) { int temp = a[left]; a[left] = a[right]; a[right] = temp; } return; } else if (size == 3) { int mid = right - 1; if (a[left] > a[mid]) { int temp = a[left]; a[left] = a[mid]; a[mid] = temp; } if (a[left] > a[right]) { int temp = a[left]; a[left] = a[mid]; a[right] = temp; } if (a[mid] > a[right]) { int temp = a[mid]; a[mid] = a[right]; a[right] = temp; } } } }
测试代码:
import java.util.Random; import sort.Sorter; public class StringTest { private static int[] intSeq; public static final int SIZE = 8; public static void main(String args[]) { intSeq = createIntSeq(SIZE); for (int i = 0; i < SIZE; i++) { System.out.print(intSeq[i] + ","); } long startTime = System.currentTimeMillis(); System.out.println(); Sorter.mergeSort(intSeq); // Sorter.bubbleSort(intSeq); // Sorter.insertSort(intSeq); // Sorter.selectSort(intSeq); // Sorter.shellSort(intSeq); // Sorter.quickSort(intSeq); long timeTaken = System.currentTimeMillis() - startTime; for (int i = 0; i < SIZE; i++) { System.out.print(intSeq[i] + ","); } System.out.println(); System.out.println("排序耗时(毫秒):" + timeTaken); } private static int[] createIntSeq(int size) { Random r = new Random(); intSeq = new int[size]; for (int i = 0; i < size; i++) { intSeq[i] = r.nextInt(size); } return intSeq; } }
归并排序的输出测试:
6,1,2,7,3,7,4,1, 排序前半部分:0-3 排序前半部分:0-1 排序前半部分:0-0 排序后半部分:1-1 开始归并:lowerBound=0;mid=0;upperBound=1 归并后:1,6, 排序后半部分:2-3 排序前半部分:2-2 排序后半部分:3-3 开始归并:lowerBound=2;mid=2;upperBound=3 归并后:2,7, 开始归并:lowerBound=0;mid=1;upperBound=3 归并后:1,2,6,7, 排序后半部分:4-7 排序前半部分:4-5 排序前半部分:4-4 排序后半部分:5-5 开始归并:lowerBound=4;mid=4;upperBound=5 归并后:3,7, 排序后半部分:6-7 排序前半部分:6-6 排序后半部分:7-7 开始归并:lowerBound=6;mid=6;upperBound=7 归并后:1,4, 开始归并:lowerBound=4;mid=5;upperBound=7 归并后:1,3,4,7, 开始归并:lowerBound=0;mid=3;upperBound=7 归并后:1,1,2,3,4,6,7,7, 1,1,2,3,4,6,7,7, 排序耗时(毫秒):4
各大排序算法的比较从严蔚敏版数据结构一书中截图:
转载于:https://blog.51cto.com/mwpmym/1240372