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);
}
}
结果: