java选择排序、冒泡排序和插入排序实现

本文深入探讨了排序算法的基础概念,重点介绍了选择排序、冒泡排序和插入排序三种基本算法的实现原理、优缺点及应用实例。通过详细解释和代码演示,帮助读者理解排序算法的核心思想。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

排序是将一组“无序”的记录序列调整为“有序”的序列的操作。最简单的排序算法有选择排序、冒泡排序和插入排序。

public class popSort {

	public static void printArray(int[] a) {
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}
		System.out.println();
	}

	// 选择法排序
	public static void selectSort(int[] a) {
		if(a ==null || a.length<2){
			return ;
		}
		
		for (int i = 0; i < a.length; i++) {
			int min = i;
			for (int j = i; j < a.length; j++) {
				if (a[min] > a[j]) {
					min = j;
				}
			}

			if (min != i) {
				int tmp = a[i];
				a[i] = a[min];
				a[min] = tmp;
			}
		}
	}

	// 冒泡法排序, 从右到左
	public static void bubbleSort1(int[] a) {
		if(a ==null || a.length<2){
			return ;
		}
		
		for (int i = 0; i < a.length; i++) {
			for (int j = a.length-1; j > i; j--) {
				if (a[j] < a[j - 1]) {
					int tmp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = tmp;
				}
			}
		}
	}
	
	// 冒泡法排序, 从左到右
	public static void bubbleSort2(int[] a) {
		if(a ==null || a.length<2){
			return ;
		}
		
		for (int i = 0; i < a.length; i++) {
			for(int j =0; j<a.length-1;j++){
				if(a[j]>a[j+1]){
					int tmp = a[j];
					a[j] = a[j+1];
					a[j+1] = tmp;
				}
			}
		}
	}
	
	// 冒泡法排序, 从右到左
	public static void advBubbleSort1(int[] a) {
		if(a ==null || a.length<2){
			return ;
		}
		
		boolean exchange = true;
		for (int i = 0; (i < a.length) && exchange; i++) {
			exchange = false;
			for (int j = a.length-1; j > i; j--) {
				if (a[j] < a[j - 1]) {
					int tmp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = tmp;
					exchange = true;
				}
			}
		}
	}
	
	// 冒泡法排序, 从左到右
	public static void advBubbleSort2(int[] a) {
		if(a ==null || a.length<2){
			return ;
		}
		
		boolean exchange = true;
		for (int i = 0; (i < a.length) && exchange; i++) {
			exchange = false;
			for(int j =0; j<a.length-1;j++){
				if(a[j]>a[j+1]){
					int tmp = a[j];
					a[j] = a[j+1];
					a[j+1] = tmp;
					exchange = true;
				}
			}
		}
	}
	
	// 插入排序
	public static void insertSort(int[] a) {
		if(a ==null || a.length<2){
			return ;
		}
		
		for(int i =0; i<a.length;i++){
			int currentValue = a[i];
			int postion = i;
			for(int j = i-1;j>=0;j--){
				if(a[j]>currentValue){
					a[j+1] = a[j];
					postion = j;
				}
				else{
					break;
				}
			}
			
			a[postion] = currentValue;
		}
	}

	public static void main(String[] args) {

		int[] a = { 2, 11, 3, 12, 54, 23, 17, 45, 3, 26, 17, 10, 20, 51, 28 };

		System.out.println("排序前:");
		printArray(a);

		// selectSort(a);
		//bubbleSort1(a);
		//bubbleSort2(a);
		//advBubbleSort1(a);
		//advBubbleSort2(a);
		insertSort(a);

		System.out.println("排序后:");
		printArray(a);

<span style="white-space:pre">	</span>}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值