冒泡、选择、插入排序

冒泡排序,如下:

 

public class BubbleSort 
{
	public static void main(String[] args)
	{
		int[] array = new int[]{29, 23, 56, 40, 12, 63, 89, 7, 96, 38};
		
		System.out.println("Before sort:");
		Display(array);
		
		Sort(array);
		
		System.out.println("After sort:");
		Display(array);
	}
	
	public static void Sort(int[] array)
	{
		int temp = 0;
		for(int i=array.length-1; i>0; i--)
		{
			for(int j=0; j<i;j++)
			{
				if(array[j] > array[j+1])
				{
					temp = array[j];
					array[j] = array[j+1];
					array[j+1] = temp;
				}
			}
		}
	}
	
	public static void Display(int[] array)
	{
		for(int i=0; i<array.length; i++)
			System.out.print(array[i] + "  ");
		System.out.println();
	}
}

//*****************
//时间复杂度为:O(N*N)
//需要比较的次数:N*(N-1)/2
//需要交换的次数:N*(N-1)/2
//*****************

/**
Before sort:
29  23  56  40  12  63  89  7  96  38  
After sort:
7  12  23  29  38  40  56  63  89  96  
*/

 

选择排序,如下:

 

public class SelectSort 
{
	public static void main(String[] args)
	{
		int[] array = new int[]{29, 23, 56, 40, 12, 63, 89, 7, 96, 38};
		
		System.out.println("Before sort:");
		Display(array);
		
		Sort(array);
		
		System.out.println("After sort:");
		Display(array);
	}
	
	public static void Sort(int[] array)
	{
		int min = 0;
		int temp = 0;
		
		for(int i=0; i<array.length-1; i++)
		{
			min = i;
			
			for(int j=i+1; j<array.length; j++)
			{
				if(array[j] < array[min])
					min = j;
			}
			
			temp = array[i];
			array[i] = array[min];
			array[min] = temp;
		}
	}
	
	public static void Display(int[] array)
	{
		for(int i=0; i<array.length; i++)
			System.out.print(array[i] + "   ");
		System.out.println();
	}
}


//*****************
//时间复杂度为:O(N*N)
//需要比较的次数:N*(N-1)/2
//需要交换的次数:N-1
//*****************

/**
Before sort:
29  23  56  40  12  63  89  7  96  38  
After sort:
7  12  23  29  38  40  56  63  89  96  
*/

 

 

插入排序,如下:

 

public class InsertSort 
{
	public static void main(String[] args)
	{
		int[] array = new int[]{29, 23, 56, 40, 12, 63, 89, 7, 96, 38};
		
		System.out.println("Before sort:");
		Display(array);
		
		Sort(array);
		
		System.out.println("After sort:");
		Display(array);
	}
	
	public static void Sort(int[] array)
	{
		int index = 0;
		int temp = 0;
		
		for(int i=1; i<array.length; i++)
		{
			index = i;
			temp = array[i];
			
			while(index > 0 && array[index-1] > temp)
			{
				array[index] = array[index-1];
				index--;
			}

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


//*****************
//时间复杂度为:O(N*N)
//需要比较的次数:N*(N-1)/4
//需要交换的次数:N-1
//*****************

/**
Before sort:
29  23  56  40  12  63  89  7  96  38  
After sort:
7  12  23  29  38  40  56  63  89  96  
*/

 

 

小结一下:

冒泡排序、选择排序、插入排序的时间复杂度为:O(N*N),且都是稳定的。
选择排序的比较次数与冒泡排序的比较次数一样,都为:N*(N-1)/2,但交换次数比其少的多(N-1次和N*(N-1)/2次)。
插入排序的比较次数比冒泡排序快一倍,比选择排序略快。
总之,相对来说,插入排序较快。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值