简单排序算法

本文介绍了三种简单的排序算法:冒泡排序、选择排序和插入排序。详细解析了每种算法的实现思路、具体实现代码及效率分析。这三种排序算法虽然时间复杂度较高,但在特定场景下仍有其应用价值。

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

        简单的排序算法包括 冒泡排序、选择排序、插入排序。

        1. 冒泡排序

            思路:从队列的开头开始,对相邻的两个数比较大小,较大的数移动到后一位。经过一轮遍历后,最后一个数字即队列的最大值。重复这一过程(已确定的值除外),即可完成排序。

            实现:

               

package com.test;

/**
 * 冒泡排序
 * @author xurongsheng
 * @date 2016年8月12日 下午3:05:02
 *
 */
public class BubbleSort {

	private long[] target;
	
	public BubbleSort() {
		
	}
	
	public BubbleSort(int maxLength) {
		target = new long[maxLength];
	}
	
	public void setTargetValue(int index,long value){
		target[index] = value;
	}
	
	public long getTargetValue(int index){
		return target[index];
	}
	
	/**
	 * 冒泡排序
	 * 比较次数 N*(N-1)/2,交换次数 N²/4
	 * 时间复杂度 O(N²)
	 * @author xurongsheng
	 * @date 2016年8月12日 上午11:11:56
	 */
	public void bubbleSort(){
		for (int i = (target.length - 1); i > 1 ; i--) {
			for (int j = 0; j < i; j++) {
				if(target[j] > target[j+1]){
					swap(j,j+1);
				}
			}
		}
	}
	
	private void swap(int fromIndex,int toIndex){
		long from = target[fromIndex];
		long to = target[toIndex];
		target[fromIndex] = to;
		target[toIndex] = from;
	}
	
	public String printTargetValue(){
		StringBuffer sb = new StringBuffer();
		for (long l : target) {
			sb.append(l).append(" ");
		}
		return sb.toString();
	}
	
	public static void main(String[] args) {
		BubbleSort bs = new BubbleSort(6);
		bs.setTargetValue(0, 1);
		bs.setTargetValue(1, 3);
		bs.setTargetValue(2, 5);
		bs.setTargetValue(3, 7);
		bs.setTargetValue(4, 9);
		bs.setTargetValue(5, 12);
		
		bs.bubbleSort();
		
		System.out.println(bs.printTargetValue());
		
	}
	
}

            效率:

                    冒泡排序的比较: 在第一轮遍历时执行了 N-1 次,第二轮 N--2 次,以此类推。即: (N-1)+(N-2)+(N-3)+... ...+1 = N*(N-1)/2

                    冒泡排序的交换: 只有在需要时才交换,大概有一半的数据需要交换。也就是N*(N-1)/4

                    比较和交换的时间复杂度都是 O(N²)


        2. 选择排序

            思路: 基本思路与冒泡排序相同,从队列开头开始,对相邻两个数进行比较,但不做位移。记录下较大值的下标,在一轮遍历结束后,将最大值交换至队尾。重复这一过程即可。

            实现:

               
package com.test;

/**
 * 选择排序
 * @author xurongsheng
 * @date 2016年8月12日 下午3:05:15
 *
 */
public class SelectSort {
	
	private long[] target;
	
	public SelectSort() {
		
	}
	
	public SelectSort(int maxLength) {
		target = new long[maxLength];
	}
	
	public void setTargetValue(int index,long value){
		target[index] = value;
	}
	
	public long getTargetValue(int index){
		return target[index];
	}
	
	/**
	 * 选择排序
	 * 比较次数 N*(N-1)/2,交换次数 小于N
	 * 时间复杂度 O(N²)
	 * @author xurongsheng
	 * @date 2016年8月12日 下午2:47:55
	 */
	public void selectSort(){
		for (int i = 0; i < target.length; i++) {
			int index = i;
			for (int j = (i+1); j < target.length; j++) {
				if(target[index] > target[j]){
					index = j;
				}
			}
			swap(i,index);
		}
	}
	
	private void swap(int fromIndex,int toIndex){
		long from = target[fromIndex];
		long to = target[toIndex];
		target[fromIndex] = to;
		target[toIndex] = from;
	}
	
	public String printTargetValue(){
		StringBuffer sb = new StringBuffer();
		for (long l : target) {
			sb.append(l).append(" ");
		}
		return sb.toString();
	}
	
	public static void main(String[] args) {
		SelectSort ss = new SelectSort(6);
		ss.setTargetValue(0, 134);
		ss.setTargetValue(1, 32);
		ss.setTargetValue(2, 568);
		ss.setTargetValue(3, 73);
		ss.setTargetValue(4, 955);
		ss.setTargetValue(5, 122);
		ss.selectSort();
		
		System.out.println(ss.printTargetValue());
	}

}
            效率:选择排序的比较: 与冒泡排序相同 N*(N-1)/2
                        选择排序的交换: 只在明确了位置后才交换 即 N


        3. 插入排序
            思路: 将左侧局部区域定义为已排序好的。将局部区域的下一个数标记为待定位的数字,将之与左侧已排好数字,由大往小比对,值小时,换位,直到值大时,即可确定位置。 如此再将下一个数字标记为待定位数字,重复以上过程。
            实现:
               
package com.test;

/**
 * 插入排序
 * @author xurongsheng
 * @date 2016年8月12日 下午3:05:25
 *
 */
public class InsertSort {

	private long[] target;
	
	public InsertSort(){
		
	}
	
	public InsertSort(int maxLength){
		target = new long[maxLength];
	}
	
	public void setTargetValue(int index,long value){
		target[index] = value;
	}
	
	public long getTargetValue(int index){
		return target[index];
	}
	
	/**
	 *  插入排序
	 *  比较次数: N*(N-1)/4
	 *  时间复杂度O(N²)
	 *  
	 * @author xurongsheng
	 * @date 2016年8月12日 下午3:38:31
	 */
	public void insertSort(){
		
		for (int i = 1; i < target.length; i++) {
			long temp = target[i];
			int flag = i;
			while(flag > 0 && target[flag-1] >= temp){
				target[flag] = target[flag-1];
				--flag;
			}
			target[flag] = temp;
		}
	}
	
	public String printTargetValue(){
		StringBuffer sb = new StringBuffer();
		for (long l : target) {
			sb.append(l).append(" ");
		}
		return sb.toString();
	}
	
	public static void main(String[] args) {
		InsertSort is = new InsertSort(6);
		is.setTargetValue(0, 134);
		is.setTargetValue(1, 32);
		is.setTargetValue(2, 568);
		is.setTargetValue(3, 73);
		is.setTargetValue(4, 955);
		is.setTargetValue(5, 122);
		is.insertSort();
		
		System.out.println(is.printTargetValue());
	}
	
}
            效率:插入排序的比较:在第一轮遍历时最多执行 1次比较,第二轮 2次,一次类推。即: 1+2+3+... ...+N-1 = N*(N-1)/2  但是在 发现插入点之前大概只有一半数据需要比较。也就是 N*(N-1)/4
                         插入排序的时间复杂度 也是O(N²) . 但在 二次排序的场景中 效率很高
                        
                        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值