排序算法

来源:互联网 发布:条码软件系统 编辑:程序博客网 时间:2024/06/05 05:00
public class Sort {/** * 1 冒泡排序(交换排序) 稳定 时间复杂度o(n^2),空间复杂度O(1) *  * @param a *            待排序数组 */public void bubbleSort(int a[]) {for (int i = 0; i < a.length; i++)for (int j = 0; j < a.length - 1; j++)if (a[j] > a[j + 1]) {int temp = a[j];a[j] = a[j + 1];a[j + 1] = temp;}}/** * 带排序边界的冒泡排序 稳定 时间复杂度o(n^2),空间复杂度O(1) *  * @param a */public void bubbleSort2(int a[]) {int boundary = a.length - 1;int nextBoundary = boundary;for (int i = 0; i < a.length; i++) {boundary = nextBoundary;for (int j = 0; j < boundary; j++)if (a[j] > a[j + 1]) {int temp = a[j];a[j] = a[j + 1];a[j + 1] = temp;nextBoundary = j;}}}/** * 2 直接插入排序(插入排序) 稳定 时间复杂度o(n^2),空间复杂度O(1) *  * @param a *            待排序数组 */public void insertSort(int a[]) {for (int i = 0; i < a.length; i++) {int j;int temp = a[i];// a[i]与i之前的元素比较,确定a[i]的位置for (j = i - 1; j >= 0; j--) {if (temp < a[j])a[j + 1] = a[j];elsebreak;}if (j != (i - 1))a[j + 1] = temp;}}/** * 3 直接选择排序(选择排序) 稳定 时间复杂度o(n^2),空间复杂度O(1) *  * @param a *            待排序数组 */public void selectSort(int a[]) {int minIndex, temp;for (int i = 0; i < a.length; i++) {minIndex = i;temp = a[i];// 从a[i]到a[a.length-1]中找一个最小的和a[i]交换位置for (int j = i; j < a.length; j++) {if (a[j] < a[minIndex])minIndex = j;}a[i] = a[minIndex];a[minIndex] = temp;}}/** * 4 快速排序(交换排序) *  * @param a *            带排序数组 */public void quickSort(int a[], int start, int end) {if (a == null)return;if (!(start < end))return;int privot = partition(a, start, end);quickSort(a, start, privot - 1);quickSort(a, privot + 1, end);}public int partition(int a[], int i, int j) {int temp;while (i < j) {while (i < j && a[j] > a[i])j--;if (i < j) {temp = a[i];a[i] = a[j];a[j] = temp;i++;}while (i < j && a[i] < a[j])i++;if (i < j) {temp = a[i];a[i] = a[j];a[j] = temp;j--;}}return i;}/** * 5 shell排序(插入排序) *  * @param a *            待排序数组 */public void shellSort(int a[],int step) {}/** * 6 堆排序(选择排序) *  * @param a *            待排序数组 */public void heapSort(int a[]) {}/** * 7二路归并排序(归并排序) *  * @param a *            待排序数组 */public void mergeSort(int a[]) {}public void print(int a[]) {for (int i : a) {System.out.print(i + " ");}System.out.println();}public static void main(String[] args) {System.out.println("1 冒泡排序");Sort s = new Sort();int a[] = new int[] { 3, 28, 6, 4, 5, 9, 8, 2, 1 };System.out.print("排序前:");s.print(a);s.bubbleSort(a);System.out.print("排序后:");s.print(a);System.out.println();System.out.println("1.1 带边界的冒泡排序");a = new int[] { 3, 28, 6, 4, 5, 9, 8, 2, 1 };System.out.print("排序前:");s.print(a);s.bubbleSort2(a);System.out.print("排序后:");s.print(a);System.out.println();System.out.println("2直接插入排序");a = new int[] { 3, 28, 6, 4, 5, 9, 8, 2, 1 };System.out.print("排序前:");s.print(a);s.insertSort(a);System.out.print("排序后:");s.print(a);System.out.println();System.out.println("3直接选择排序");a = new int[] { 3, 28, 6, 4, 5, 9, 8, 2, 1 };System.out.print("排序前:");s.print(a);s.selectSort(a);System.out.print("排序后:");s.print(a);System.out.println();System.out.println("4快速排序");a = new int[] { 3, 28, 6, 4, 5, 9, 8, 2, 1 };System.out.print("排序前:");s.print(a);s.quickSort(a, 0,a.length-1);System.out.print("排序后:");s.print(a);System.out.println();}}