计算机基础——常见排序算法

冒泡排序

import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;

public class BubbleSort {
	private Integer[] integers;

	// 数组应该是递增的,当递减的时候交换,
	public static void bubbleSort(Integer[] a) {
		int i = a.length - 1;
		int j;
		Integer tmp;
		for (; i > 1; i--) {
			for (j = 0; j < i; j++) {
				// 每次比较相邻的两个
				if (a[j] > a[j + 1]) {
					tmp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = tmp;
				}
			}
		}
	}

	@Before
	public void init() {
		Integer[] integers = { 999, 1, 3, 9, 4, 56, 43, 23, 4, 34, 33, 2, 454, 5 };
		this.integers = integers;
	}

	@Test
	public void insertSortTest() {
		System.out.println(Arrays.toString(integers));
		BubbleSort.bubbleSort(integers);
		System.out.println(Arrays.toString(integers));
	}
}

简单选择排序

public class EasySelectSort {
	private Integer[] integers;

	// 选最小的,和前面的交换
	public static void EasySelectSort(Integer[] a) {
		int min;
		int j;
		Integer tmp;
		for (int i = 0; i < a.length; i++) {
			min = i;
			for (j = i; j < a.length; j++) {
				if (a[j] < a[min]) {
					min = j;
				}
			}
			tmp = a[min];
			a[min] = a[i];
			a[i] = tmp;
		}
	}

	@Before
	public void init() {
		Integer[] integers = { 999, 1, 3, 9, 4, 56, 43, 23, 4, 34, 33, 2, 454, 5 };
		this.integers = integers;
	}

	@Test
	public void insertSortTest() {
		System.out.println(Arrays.toString(integers));
		BubbleSort.bubbleSort(integers);
		System.out.println(Arrays.toString(integers));
	}
}

插入排序

import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;

public class InsertSort {
	/*
	 * 第一个是哨兵
	 */
	// 泛型方法
	private Integer[] integers;

	public static void insertSort(Integer[] t) {
		int k;
		Integer tmp;
		for (int i = 1; i < t.length; i++) {
			k = i;
			// 这个先后位置不能打乱,先判断是否满足条件
			while (k > 0 && t[k] < t[k - 1]) {
				tmp = t[k-1];
				t[k-1] = t[k];
				t[k] = tmp;
				k--;
			}
		}
	}

	public static void insertSortByBinSearch(Integer[] t) {
		int low;
		int high;
		int j;
		for (int i = 1; i < t.length; i++) {
			low = 0;
			high = i;
			int mid;
			// 这个先后位置不能打乱,先判断是否满足条件
			while (low < high) {
				mid = (low + high) >> 1;
				if (t[i] < t[mid])
					high = mid - 1;
				else if (t[i] > t[mid])
					low = mid + 1;
			}
			for (j = i; j >= high + 2; --j)
				t[j] = t[j - 1];// 统一向后移动元素,空出插入位置
			t[high + 1] = t[0];// 插入操作
		}
	}

	@Before
	public void init() {
		// 0, 1, 2, 3, 4, 4, 5, 9, 22, 23, 33, 33, 34, 43, 56, 454, 999
		// 0, 1, 2, 3, 4, 4, 5, 9, 22, 23, 33, 33, 34, 43, 56, 454, 999
		Integer[] integers = { 22, 0, 33, 999, 1, 3, 9, 4, 56, 43, 23, 4, 34, 33, 2, 454, 5 };
		this.integers = integers;
	}

	@Test
	public void insertSortTest() {
		System.out.println(Arrays.toString(integers));
		InsertSort.insertSort(integers);
		System.out.println(Arrays.toString(integers));
	}

}

快排

import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;

public class QuickSort {
	private Integer[] integers;

	public static void quickSort(Integer[] a, int from, int to) {
		if (to - from <= 0)
			return;
		int i = from;
		int j = to;
		int mid = a[from];
		while (i < j) {
			while (i < j && a[j] > mid)
				j--;
			if (i < j) {
				a[i++] = a[j];
			}
			while (i < j && a[i] < mid)
				i++;
			if (i < j) {
				a[j--] = a[i];
			}
		}
		// low==high,i==j
		a[i] = mid;
		quickSort(a, from, j - 1);
		quickSort(a, j + 1, to);
	}

	@Before
	public void init() {
		Integer[] integers = { 999, 1, 3, 9, 1, 56, 7, 77, 1, 88, 1, 4, 56, 43, 23, 4, 34, 33, 2, 454, 5 };
		this.integers = integers;
		// 逆序
		// [999, 454, 56, 43, 34, 33, 23, 9, 5, 4, 4, 3, 2, 1]
		// 正序
		// [1, 2, 3, 4, 4, 5, 9, 23, 33, 34, 43, 56, 454, 999]
	}

	@Test
	public void quickSort() throws Exception {
		QuickSort.quickSort(integers, 0, integers.length - 1);
		System.out.println(Arrays.toString(integers));
	}
}

希尔排序

public class SheelSort {
	private Integer[] integers;

	public static void shellSort(Integer[] a, int shell) {
		int j;
		Integer tmp;
		for (; shell >= 1; shell = shell >> 1) {
			// i=1和0都一样?
			for (int i = 1; i < a.length; i = i + shell) {
				// j - shell >= 0;这句不一样
				for (j = i; j - shell >= 0; j = j - shell) {
					if (a[j] < a[j - shell]) {
						tmp = a[j];
						a[j] = a[j - shell];
						a[j - shell] = tmp;
					}
				}
			}
		}
	}

	@Before
	public void init() {
		Integer[] integers = { 999, 1, 1, 2, 1, 2, 1, 3, 9, 4, 56, 43, 23, 4, 34, 33, 2, 454, 5 };
		this.integers = integers;
		// 逆序
		// [999, 454, 56, 43, 34, 33, 23, 9, 5, 4, 4, 3, 2, 1]
		// 正序
		// [1, 2, 3, 4, 4, 5, 9, 23, 33, 34, 43, 56, 454, 999]
	}

	@Test
	public void shellSort() throws Exception {
		SheelSort.shellSort(integers, 4);
		System.out.println(Arrays.toString(integers));
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值