排序算法

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];
				else
					break;
			}
			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();
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值