简单排序二--冒泡排序、选择排序、插入排序练习

package com.wei;

public class SimpleArrayTool
{

	/*------------------------/
	--函数功能:交换两个数值
	--返回值  :true,成功交换
	--参数:array整形数组;
	--      one,two数组下标
	--------------------------*/
	public static boolean swap(final int[] array, final int one, final int two)
	{
		if(array == null)       return false;
		if(array.length == 1)   return false;
		
		int TempVaule = array[one];
		array[one] = array[two];
		array[two] = TempVaule;
		return true;
	}
	
	/*------------------------/
	--函数功能:显示数组的元素
	--返回值  :空
	--参数:array整形数组;
	--------------------------*/
	public static void showArray(final int[] array) 
	{
	
		for (int elems : array)
		{
			System.out.print(elems+" ");
		}
		System.out.println();
	}	

	/*------------------------------/
	--函数功能:寻找数组的最小值下标
	--返回值  :int--最小值下标>=0
	--参数:array整形数组;
	--      from,to数组起始位置下标
	-------------------------------*/
	public static int findMin(final int[] array,final int from,final int to) 
	{
		if(array==null) return -1;
	
		int len = array.length;
		if( len == 1)   return  0;	
		if( to>len||to<from||from<0 )  return -1; 
		if(to==from) return to ;

		int MinIndex = from;
		for(int i=from; i<to; i++)
		{
			if(array[i] < array[MinIndex] ) 
				MinIndex = i;
		}
			
		return MinIndex;
	}
	
	/*------------------------------/
	--函数功能:冒泡排序
	--返回值  :int数组
	--参数:array整形数组;
	-------------------------------*/
	public static int[] bubbleSort(int[] array) throws Exception 
	 {  
	        // 输入控制  
	        if (array == null)  
	            throw new Exception("input sortedArrayay cannot be null");
	        int len = array.length;
	        if (len == 1)  
	            return array; // if have only one element  
	  
	        // bubble从左向右(in)到out比较;out的右边是已经排好序的  
	        for(int out=len-1; out>0; out--)
	        {
	        	for(int in=0; in<out; in++)
	        	{
	        		if(array[in]>array[in+1])
	        			swap(array, in, in+1);
	        	}
	        }
	        return array;  
	 }
	
	/*------------------------------/
	--函数功能:选择排序
	--返回值  :int数组
	--参数:array整形数组;
	-------------------------------*/
	public static int[] selectSort(int[] array) throws Exception 
	 {  
	        // 输入控制  
	        if (array == null)  
	            throw new Exception("input sortedArrayay cannot be null");
	        int len = array.length;
	        if (len == 1)  
	            return array; // if have only one element  
	  
	        // out的右边是已经排好序的  
	        int min = 0;
	        for(int out=0; out<len-1; out++)
	        {
	        	min=findMin(array,out+1,len);
	        	if(min>-1 && array[out]>array[min])
	        		swap(array, out, min);
	        }
	        return array;  
	 }
	
	/*------------------------------/
	--函数功能:插入排序
	--返回值  :int数组
	--参数:array整形数组;
	-------------------------------*/
	public static int[] insertSort(int[] array) throws Exception 
	 {  
	        // 输入控制  
	        if (array == null)  
	            throw new Exception("input sortedArrayay cannot be null");
	        int len = array.length;
	        if (len == 1)  
	            return array; // if have only one element  
	        
	        int in=0;
	        for(int out=1; out<len; out++)
	        {
	        	int temp =array[out];
	        	for(in=out; in>0 && array[in-1]>temp; in--)
	        	{
	        		array[in] = array[in-1];
	        	}
	        	array[in] =temp; 
	        }
	        return array;  
	 }
}

测试端:

package com.wei;

public class Client
{
	public static void main(String[] args) throws Exception
	{
		int[] a ={3,49,9,82,78,0,5,9,7,2,-1};
		System.out.print("原始数组数据:");
		SimpleArrayTool.showArray(a);
		//数据交换测试
		System.out.print("数据交换测试:");
		if( SimpleArrayTool.swap(a, 0, 1) )
		{
			System.out.println("Swap is success!");
			System.out.print("交换后的数组数据:");
			SimpleArrayTool.showArray(a);
		}
		else
		{
			System.out.println("Swap is not success!");
		}
		
		//寻找最小值的下标
		System.out.println("--------------寻找最小值的下标---------------");
		int Mindex=SimpleArrayTool.findMin(a, 0, a.length);
		if(Mindex>-1)
		{
			System.out.println("Max value is:"+a[Mindex]+" \n下标:index="+Mindex);
		}
		//冒泡测试
		System.out.println("---------------冒泡排序测试-----------------");
		SimpleArrayTool.bubbleSort(a);
		System.out.print("冒泡排序后的数据:");
		SimpleArrayTool.showArray(a);
		
		System.out.println("---------------选择排序测试-----------------");
		SimpleArrayTool.selectSort(a);
		System.out.print("选择排序后的数据:");
		SimpleArrayTool.showArray(a);	
		
		System.out.println("---------------插入排序测试-----------------");
		SimpleArrayTool.insertSort(a);
		System.out.print("插入排序后的数据:");
		SimpleArrayTool.showArray(a);
	}
}
结果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值