Java三种简单排序算法:冒泡,选择,插入

最近公司项目比较闲,特意回去读了读java数据结构和算法,重温了下几个基本算法,冒泡,选择和插入,并测试了各个排序的速度,测试样本为20000大小的数组,测试结果和代码如下,可以看到冒泡速度最慢,插入最快,选择其次:


/**   
*    
* 项目名称:TestStringUtils   
* 类名称:TestSort   
* 类描述:   测试排序算法
* 	    冒泡最慢,插入最快,选择其次
* 创建人:xuanwugang   
* 创建时间:2018年3月23日 上午9:32:18   
* 修改人:xuanwugang   
* 修改时间:2018年3月23日 上午9:32:18   
* 修改备注:   
* @version    
*    
*/
public class TestSort {
	public static void main(String[] args){
		System.gc();
		Map<String, String> getenv = System.getenv();
		System.out.println(getenv.toString());
	}

        //测试冒泡
	@Test
	public void testPop(){
		int[] a = new int[20000];
		for(int i = 0;i<20000;i++){
			Random r = new Random();
			a[i]=r.nextInt(20000);
		}
		System.out.println(Arrays.toString(a));
		TestSort testSort = new TestSort();
		testSort.popSort(a);
		System.out.println(Arrays.toString(a));
	}
	
	//测试插入
	@Test
	public void testInsert(){
		int[] a = new int[20000];
		for(int i = 0;i<20000;i++){
			Random r = new Random();
			a[i]=r.nextInt(20000);
		}
		System.out.println(Arrays.toString(a));
		TestSort testSort = new TestSort();
		testSort.insertSort(a);
		System.out.println(Arrays.toString(a));
	}
	
	//测试选择
	@Test
	public void testSelect(){
		int[] a = new int[20000];
		for(int i = 0;i<20000;i++){
			Random r = new Random();
			a[i]=r.nextInt(20000);
		}
		System.out.println(Arrays.toString(a));
		TestSort testSort = new TestSort();
		testSort.selectSort(a);
		System.out.println(Arrays.toString(a));
	}


        /** 

	* @Title: 冒泡排序 
	* @Description: 从第一条依次与后面做对比
	* @param @param tosort  
	* @return void  
	* @throws 
	*/
	public void popSort(int[] tosort) {
		for (int i = 0; i < tosort.length; i++) {
			for (int j = i + 1; j < tosort.length; j++) {
				//每判断一次则立马进行交换
				if (tosort[j] < tosort[i]) {
					int temp = tosort[i];
					tosort[i] = tosort[j];
					tosort[j] = temp;
				}
			}
		}
	}
	
	/** 
	* @Title: 选择排序
	* @Description: 先找到最小位置再交换
	* @param @param tosort  
	* @return void  
	* @throws 
	*/
	public void selectSort(int[] tosort){
		for (int i = 0; i < tosort.length; i++) {
			//最小位置记号
			int mark = i;
			for (int j = i + 1; j < tosort.length; j++) {
				//每判断一次则记录最小位置
				if (tosort[j] < tosort[i]) {
					mark = j;
				}
			}
			//找到最小的再交换
			if(i!=mark){
				int temp = tosort[i];
				tosort[i] = tosort[mark];
				tosort[mark] = temp;
			}
		}
	}
	
	/** 
	* @Title: 插入排序 
	* @Description: 依次和左侧元素进行比较,找到合适的位置插入
	* @param @param tosort  
	* @return void  
	* @throws 
	*/
	public void insertSort(int[] tosort){
		for(int i =1;i < tosort.length;i++){
			//记录待插入的标记值
			int markValue = tosort[i];
			//记录该值得最新位置
			int markPosition;
			for(markPosition = i;markPosition > 0&&markValue<tosort[markPosition-1];markPosition--){
				//若左侧值比标记值大,则右移,直到找到能插入的位置
				tosort[markPosition] = tosort[markPosition-1];
			}
			//将该位置的值设为标记值
			tosort[markPosition] = markValue;
		}
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值