# 20202328马文瑞实验报告七

定义一个SearchingSorting类，并在类中实现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());//边界        System.out.println(new Searching(a1,999).linearSearch());//异常,输出0        System.out.println();        System.out.println(new Searching(a2,4).linearSearch());//正常        System.out.println(new Searching(a2,9).linearSearch());//边界        System.out.println(new Searching(a2,2328).linearSearch());//边界        System.out.println(new Searching(a2,999).linearSearch());//异常        System.out.println();        System.out.println(new Searching(a3,243).linearSearch());//正常        System.out.println(new Searching(a3,6).linearSearch());//边界        System.out.println(new Searching(a3,2328).linearSearch());//边界        System.out.println(new Searching(a3,999).linearSearch());//异常        System.out.println();        System.out.println(new Searching(a4,4).linearSearch());//正常        System.out.println(new Searching(a4,1).linearSearch());//边界        System.out.println(new Searching(a4,2328).linearSearch());//边界        System.out.println(new Searching(a4,999).linearSearch());//异常        System.out.println();        System.out.println(new Searching(a5,4).linearSearch());//正常        System.out.println(new Searching(a5,24).linearSearch());//边界        System.out.println(new Searching(a5,2328).linearSearch());//边界        System.out.println(new Searching(a5,999).linearSearch());//异常        System.out.println();        System.out.println(new Searching(a6,4).linearSearch());//正常        System.out.println(new Searching(a6,12).linearSearch());//边界        System.out.println(new Searching(a6,2328).linearSearch());//边界        System.out.println(new Searching(a6,999).linearSearch());//异常        System.out.println();        System.out.println(new Searching(a7,4).linearSearch());//正常        System.out.println(new Searching(a7,3245).linearSearch());//边界        System.out.println(new Searching(a7,2328).linearSearch());//边界        System.out.println(new Searching(a7,999).linearSearch());//异常        System.out.println();        System.out.println(new Searching(a8,23).linearSearch());//正常        System.out.println(new Searching(a8,43).linearSearch());//边界        System.out.println(new Searching(a8,2328).linearSearch());//边界        System.out.println(new Searching(a8,999).linearSearch());//异常        System.out.println();        System.out.println(new Searching(a9,4).linearSearch());//正常        System.out.println(new Searching(a9,65).linearSearch());//边界        System.out.println(new Searching(a9,2328).linearSearch());//边界        System.out.println(new Searching(a9,999).linearSearch());//异常        System.out.println();        System.out.println(new Searching(a10,4).linearSearch());//正常        System.out.println(new Searching(a10,46).linearSearch());//边界        System.out.println(new Searching(a10,2328).linearSearch());//边界        System.out.println(new Searching(a10,999).linearSearch());//异常    }}

（二） sorting

import java.util.Arrays;public class Sorting {    private int List[];    private int size;    public Sorting(int[] list) {        List = list;size=List.length;}    public String selectionSort(){        int temp,tempx;        for (int i=0;i<size-1;i++){            temp=i;            for (int j=i+1;j<size;j++){                if (List[temp]>List[j]){                    temp=j;}            }            if (temp == 0) {                continue;}            else {                tempx=List[i];List[i]=List[temp];List[temp]=tempx;}        }        return Arrays.toString(List) ;}}

SortTest

public class SortTest {    private static int a1[]={1,2,3,4,5,6,7,8,9,2328};//正序private static int a2[]={2328,9,8,7,6,5,4,3,2,1};//逆序private static int a3[]={23,8,445,6,5,45,3,2,98,2328};    private static int a4[]={6,8,7,243,5,4,3,2,43,2328};    private static int a5[]={24,8,7,6,5,4,3,2,76,2328};    private static int a6[]={12,8,7,233,5,4,3,2,43,2328};    private static int a7[]={3245,9,8,34,6,5,4,3,2,12,2328};    private static int a8[]={43,8,7,6,5,23,3,2,24,2328};    private static int a9[]={65,8,7,56,5,4,3,2,234,2328};    private static int a10[]={46,8,7,32,5,4,3,2,2,2328};    public static void main(String[] args) {        System.out.println(new Sorting(a1).selectionSort());System.out.println(new Sorting(a2).selectionSort() );System.out.println(new Sorting(a3).selectionSort() );System.out.println(new Sorting(a4).selectionSort() );System.out.println(new Sorting(a5).selectionSort() );System.out.println(new Sorting(a6).selectionSort() );System.out.println(new Sorting(a7).selectionSort() );System.out.println(new Sorting(a8).selectionSort() );System.out.println(new Sorting(a9).selectionSort() );System.out.println(new Sorting(a10).selectionSort() );}}

重构你的代码

Sorting.java Searching.java放入 cn.edu.besti.cs2023.（姓名首字母+四位学号） 包中（例如：cn.edu.besti.cs1823.G2301

把测试代码放test包中

重新编译，运行代码，提交编译，运行的截图（IDEA，命令行两种）

参考http://www.cnblogs.com/maybe2030/p/4715035.html ，学习各种查找算法并在Searching中补充查找算法并测试

提交运行结果截图

import junit.framework.TestCase;
public class Test extends TestCase {
Searching a = new Searching();
Sorting b = new Sorting();
@org.junit.Test
public void testSearching()
{
int[] t1 = {19,14,23,1,68,20,84,27,55,11,10,2328};
assertEquals(false,a.linearSearch(t1,9,12));            //正常
assertEquals(true,a.linearSearch(t1,1,12));             //正常
assertEquals("Abnormal",a.linearSearch(t1,1,18));       //异常
assertEquals(true,a.linearSearch(t1,2328,12));            //边界
int[] t2 = {5,2,7,43,21,65,2328};
assertEquals(true,a.linearSearch(t2,2,7));
assertEquals(false,a.linearSearch(t2,42314,7));
assertEquals("Abnormal",a.linearSearch(t2,1,18));
assertEquals(true,a.linearSearch(t2,5,7));
int[] t3 = {1,24,5,5,2,2,45,2328};
assertEquals(true,a.linearSearch(t3,24,8));
assertEquals(false,a.linearSearch(t3,0,8));
assertEquals("Abnormal",a.linearSearch(t3,1,18));
assertEquals(true,a.linearSearch(t3,2328,8));
int[] t4 = {2,423,42,435,32,45,4,43,2328};
assertEquals(true,a.linearSearch(t4,4,9));
assertEquals(false,a.linearSearch(t4,123,9));
assertEquals("Abnormal",a.linearSearch(t4,1,18));
assertEquals(true,a.linearSearch(t4,2328,9));
int[] t5 = {967,65,5687,56,332,46,2328};
assertEquals(true,a.linearSearch(t5,332,7));
assertEquals(false,a.linearSearch(t5,12,7));
assertEquals("Abnormal",a.linearSearch(t5,1,18));
assertEquals(true,a.linearSearch(t5,967,7));
}
@org.junit.Test
public void testSorting(){
Comparable[] t6 = {1,2,4,67,3,2328};
b.selectionSort(t6);
assertEquals("1 2 3 4 67 2328 ",b.print(t6));
Comparable[] t7 = {1,4,8,2,9,2328};
b.selectionSort(t7);
assertEquals("1 2 4 8 9 2328 ",b.print(t7));
Comparable[] t8 = {1,2,6,8,9,2328};
b.selectionSort(t8);
assertEquals("1 2 6 8 9 2306 ",b.print(t8));
Comparable[] t9 = {30,27,24,13,2306};
b.selectionSort(t9);
assertEquals("13 24 27 30 2328 ",b.print(t9));
Comparable[] t10 = {99,98,92,2328};
b.selectionSort(t10);
assertEquals("92 98 99 2328 ",b.print(t10));
Comparable[] t11 = {20,23,28};
b.selectionSort(t11);
assertEquals("28 20 23 ",b.print(t11));
}
}

posted @ 2021-11-14 23:05  20202328马文瑞  阅读(47)  评论(0编辑  收藏  举报