面试题望求教!
刚走出校门,今天去一家公司面试,笔试方面很顺利,但是上机题我搞不懂什么意思!
1.有一个数组arry[4,15,3,9,7,-1,55,21,9,33]十个数,请对它从小到大排序,然后输出,请注意代码的可读性(程序语言不限),排序算法自己写.
这题我写的代码如下:
/**
* 对一组无序数列进行冒泡排序。
* @author Administrator
*
*/
public class BubbleSort
{
/**
* 主函数入口。
* @param args
*/
public static void main(String args[])
{
int array[] = { 4,15,3,9,7,-1,55,21,9,33};
int result[];
//打印待排序数组。
for (int i = 0; i < array.length; i++)
{
System.out.print(array + " ");
}
System.out.println("");
//进行排序。
result = bubble(array);
//打印排序后数组。
for (int i = 0; i < result.length; i++)
{
System.out.print(result + " ");
}
}
/**
* 冒泡排序。
* @param tmp:待排序数列;
* @return
*/
private static int[] bubble(int tmp[])
{
int temp;
for (int i = tmp.length - 1; i >= 1; i--)
{
for (int j = 0; j < i; j++)
{
if (tmp[j] > tmp[j + 1])
{
//交换位置。
temp = tmp[j];
tmp[j] = tmp[j + 1];
tmp[j + 1] = temp;
}
}
}
return tmp;
}
}
但是第二题我就不是很明白怎么搞了!希望高手们指点一下,并给出个答案,谢了!
2.对排序函数进行单元测试,设计一个用例.
//排序函数
//采用冒泡排序