20202328马文瑞实验报告七
课程:《程序设计与数据结构》
班级: 2023
姓名: 马文瑞
学号:20202328
实验教师:王志强
实验日期:2021年11月14日
必修/选修: 必修
定义一个Searching和Sorting类,并在类中实现linearSearch,SelectionSort方法,最后完成测试。
要求不少于10个测试用例,提交测试用例设计情况(正常,异常,边界,正序,逆序),用例数据中要包含自己学号的后四位
提交运行结果图。
(一)searching
代码:
package com.company;
public class Searching {
private int List[];
private int searchElement;
private int listSize;
private int locate;
public Searching(int[] list, int searchElement)
{
List = list;
this.searchElement = searchElement;
listSize= List.length;
}
public int linearSearch(){
for (int i=listSize-1;;i--){
if (searchElement==List[i]){
locate=i;
return List[i];
}
else if (i==0){
return 0;
}
}
}
}
SearchTest
import com.company.Searching;
public class SearchTest {
public static void main(String[] args) {
int a1[]={1,2,3,4,5,6,7,8,9,2328};
int a2[]={9,8,7,6,5,4,3,2,1,2328};
int a3[]={23,8,445,6,5,45,3,2,98,2328};
int a4[]={6,8,7,243,5,4,3,2,43,2328};
int a5[]={24,8,7,6,5,4,3,2,76,2328};
int a6[]={12,8,7,233,5,4,3,2,43,2328};
int a7[]={3245,9,8,34,6,5,4,3,2,12,2328};
int a8[]={43,8,7,6,5,23,3,2,24,2328};
int a9[]={65,8,7,56,5,4,3,2,234,2328};
int a10[]={46,8,7,32,5,4,3,2,2,2328};
System.out.println("线性查找:");
System.out.println(new Searching(a1,4).linearSearch());//正常
System.out.println(new Searching(a1,1).linearSearch());//边界
System.out.println(new Searching(a1,2328).linearSearch());//边界