Java排序

Java排序

一、选择排序

/**
 * <p>Title: SelectionSort</p>
 * <p>Description: 选择排序</p>
 */
public class SelectionSort {

	/**
	 * 主方法
	 * <p>Title: main</p>
	 * <p>Description: 主方法</p>
	 * @param args
	 */
	public static void main(String[] args) {
		int[] array = newArray();
		printArray("排序前:", array);
		sortn(array);
		printArray("排序后:", array);
	}
	
	/**
	 * 创建一个数组
	 * <p>Title: newArray</p>
	 * <p>Description: 创建一个数组</p>
	 * @return
	 */
	private static int[] newArray() {
		int[] array = new int[] {5, 7, 1, 3, 9, 2 ,8, 4, 6, 10};
		return array;
	}
	
	/**
	 * 排序
	 * <p>Title: sort</p>
	 * <p>Description: 排序</p>
	 * @param array
	 */
	private static void sort1(int[] array) {
		for (int x = 0; x < array.length - 1; x++) {
			for (int y = x + 1; y < array.length; y++) {
				if (array[x] > array[y]) {
					int temp = array[x];
					array[x] = array[y];
					array[y] = temp;
				}
			}
		}
	}
	
	/**
	 * 排序
	 * <p>Title: sort</p>
	 * <p>Description: 排序</p>
	 * @param array
	 */
	private static void sort2(int[] array) {
		for (int x = array.length - 1; x > 0; x--) {
			for (int y = 0; y < x; y++) {
				if (array[y] > array[y + 1]) {
					int temp = array[y];
					array[y] = array[y + 1];
					array[y + 1] = temp;
				}
			}
		}
	}
	
	/**
	 * 排序
	 * <p>Title: sort</p>
	 * <p>Description: 排序</p>
	 * @param array
	 */
	private static void sort3(int[] array) {
		for (int x = 0; x < array.length - 1; x++) {
			int index = x;
			int num = array[x];
			for (int y = x + 1; y < array.length; y++) {
				if (num > array[y]) {
					index = y;
					num = array[y];
				}
			}
			if (index != x) {
				swap(array, x, index);
			}
		}
	}
	
	/**
	 * 打印数组
	 * <p>Title: printArray</p>
	 * <p>Description: 打印数组</p>
	 * @param str
	 * @param array
	 */
	private static void printArray(String str, int[] array) {
		System.out.print(str);
		for (int x = 0; x < array.length; x++) {
			if (x != array.length -1)
				System.out.print(array[x] + ", ");
			else {
				System.out.println(array[x]);
			}
		}
	}
	
}

二、冒泡排序

/**
 * <p>Title: SelectionSort</p>
 * <p>Description: 选择排序</p>
 */
public class BubboleSort {

	/**
	 * 主方法
	 * <p>Title: main</p>
	 * <p>Description: 主方法</p>
	 * @param args
	 */
	public static void main(String[] args) {
		int[] array = newArray();
		printArray("排序前:", array);
		sort(array);
		printArray("排序后:", array);
	}
	
	/**
	 * 创建一个数组
	 * <p>Title: newArray</p>
	 * <p>Description: 创建一个数组</p>
	 * @return
	 */
	private static int[] newArray() {
		int[] array = new int[] {5, 7, 1, 3, 9, 2 ,8, 4, 6, 10};
		return array;
	}
	
	/**
	 * 排序
	 * <p>Title: sort</p>
	 * <p>Description: 排序</p>
	 * @param array
	 */
	private static void sort1(int[] array) {
		for (int x = 0; x < array.length - 1; x++) {
			for (int y = 0; y < array.length - 1 - x; y++) {
				if (array[y] > array[y + 1]) {
					int temp = array[y];
					array[y] = array[y + 1];
					array[y + 1] = temp;
				}
			}
		}
	}
	
	/**
	 * 排序
	 * <p>Title: sort</p>
	 * <p>Description: 排序</p>
	 * @param array
	 */
	private static void sort2(int[] array) {
		for (int x = array.length - 1; x > 0; x--) {
			for (int y = 0; y < x; y++) {
				if (array[y] > array[y + 1]) {
					int temp = array[y];
					array[y] = array[y + 1];
					array[y + 1] = temp;
				}
			}
		}
	}
	
	/**
	 * 打印数组
	 * <p>Title: printArray</p>
	 * <p>Description: 打印数组</p>
	 * @param str
	 * @param array
	 */
	private static void printArray(String str, int[] array) {
		System.out.print(str);
		for (int x = 0; x < array.length; x++) {
			if (x != array.length -1)
				System.out.print(array[x] + ", ");
			else {
				System.out.println(array[x]);
			}
		}
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值