20192319 2020-2021-1《数据结构及面向对象程序设计》实验七报告

实验七报告

  • 课程:《程序设计与数据结构》
  • 班级: 1923
  • 姓名: 李歆韵
  • 学号:20192319
  • 实验教师:王志强
  • 实验日期:2020年11月18日
  • 必修/选修: 必修

1.实验内容

  1. 定义一个Searching和Sorting类,并在类中实现linearSearch,SelectionSort方法,最后完成测试。
    要求不少于10个测试用例,提交测试用例设计情况(正常,异常,边界,正序,逆序),用例数据中要包含自己学号的后四位
    提交运行结果图。
    重构你的代码

  2. 把Sorting.java Searching.java放入 cn.edu.besti.cs1823.(姓名首字母+四位学号) 包中(例如:cn.edu.besti.cs1823.G2301)
    把测试代码放test包中
    重新编译,运行代码,提交编译,运行的截图(IDEA,命令行两种)

  3. 参考http://www.cnblogs.com/maybe2030/p/4715035.html ,学习各种查找算法并在Searching中补充查找算法并测试
    提交运行结果截图

  4. 补充实现课上讲过的排序方法:希尔排序,堆排序,二叉树排序等(至少3个)
    测试实现的算法(正常,异常,边界)
    提交运行结果截图(如果编写多个排序算法,即使其中三个排序程序有瑕疵,也可以酌情得满分)

  5. 编写Android程序对实现各种查找与排序算法进行测试
    提交运行结果截图
    推送代码到码云(选做,加分)

2.实验过程与结果

  1. 定义一个Searching和Sorting类,并在类中实现linearSearch,SelectionSort方法,最后完成测试。
    要求不少于10个测试用例,提交测试用例设计情况(正常,异常,边界,正序,逆序),用例数据中要包含自己学号的后四位
    代码:
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 String linearSearch(){
        for (int i=listSize-1;;i--){
            if (searchElement==List[i]){
                locate=i;
                return "Element founded: " + "List[" + i + "]";
            }
            else if (i==0){
                if (searchElement==List[i]){
                    locate=i;
                    return "Element founded: " + List[i];
                }
                else
                return "Not founded.";
            }
        }
    }
}
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 = 0,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);
    }
}

  1. 把Sorting.java Searching.java放入 cn.edu.besti.cs1823.(姓名首字母+四位学号) 包中(例如:cn.edu.besti.cs1823.G2301)
    把测试代码放test包中
    重新编译,运行代码,提交编译,运行的截图(IDEA,命令行两种)
    代码:
import sy7.sy7_2.cn.edu.besti.cs1923.L2319.Searching;
import sy7.sy7_2.cn.edu.besti.cs1923.L2319.Sorting;
import junit.framework.TestCase;

public class test extends TestCase{

    @org.junit.Test
    public void testSearching(){
        int a1[]={11,2,1,1,1,1,1,1,1,1,1,2319};
        int a2[]={12,1,2,1,1,1,1,1,1,1,1,2319};
        int a3[]={13,1,1,2,1,1,1,1,1,1,1,2319};
        int a4[]={14,1,1,1,2,1,1,1,1,1,1,2319};
        int a5[]={15,1,1,1,1,2,1,1,1,1,1,2319};
        int a6[]={16,1,1,1,1,1,2,1,1,1,1,2319};
        int a7[]={17,2,1,1,1,1,1,2,1,1,1,2319};
        int a8[]={18,1,2,1,1,1,1,1,2,1,1,2319};
        int a9[]={19,1,1,2,1,1,1,1,1,2,1,2319};
        int a10[]={20,1,1,1,2,1,1,1,1,1,2,2319};

        assertEquals(new Searching(a1,2).linearSearch(),"Element found: " + "List[" + 1 + "]");
        assertEquals(new Searching(a1,11).linearSearch(),"Element found: " + "List[" + 0 + "]");
        assertEquals(new Searching(a1,2319).linearSearch(),"Element found: " + "List[" + 11 + "]");
        assertEquals(new Searching(a1,404).linearSearch(),"Not found.");

        assertEquals(new Searching(a2,2).linearSearch(),"Element found: " + "List[" + 2 + "]");
        assertEquals(new Searching(a2,12).linearSearch(),"Element found: " + "List[" + 0 + "]");
        assertEquals(new Searching(a2,2319).linearSearch(),"Element found: " + "List[" + 11 + "]");
        assertEquals(new Searching(a2,404).linearSearch(),"Not found.");

        assertEquals(new Searching(a3,2).linearSearch(),"Element found: " + "List[" + 3 + "]");
        assertEquals(new Searching(a3,13).linearSearch(),"Element found: " + "List[" + 0 + "]");
        assertEquals(new Searching(a3,2319).linearSearch(),"Element found: " + "List[" + 11 + "]");
        assertEquals(new Searching(a3,404).linearSearch(),"Not found.");

        assertEquals(new Searching(a4,2).linearSearch(),"Element found: " + "List[" + 4 + "]");
        assertEquals(new Searching(a4,14).linearSearch(),"Element found: " + "List[" + 0 + "]");
        assertEquals(new Searching(a4,2319).linearSearch(),"Element found: " + "List[" + 11 + "]");
        assertEquals(new Searching(a4,404).linearSearch(),"Not found.");

        assertEquals(new Searching(a5,2).linearSearch(),"Element found: " + "List[" + 5 + "]");
        assertEquals(new Searching(a5,15).linearSearch(),"Element found: " + "List[" + 0 + "]");
        assertEquals(new Searching(a5,2319).linearSearch(),"Element found: " + "List[" + 11 + "]");
        assertEquals(new Searching(a5,404).linearSearch(),"Not found.");

        assertEquals(new Searching(a6,2).linearSearch(),"Element found: " + "List[" + 6 + "]");
        assertEquals(new Searching(a6,16).linearSearch(),"Element found: " + "List[" + 0 + "]");
        assertEquals(new Searching(a6,2319).linearSearch(),"Element found: " + "List[" + 11 + "]");
        assertEquals(new Searching(a6,404).linearSearch(),"Not found.");

        assertEquals(new Searching(a7,2).linearSearch(),"Element found: " + "List[" + 7 + "]");
        assertEquals(new Searching(a7,17).linearSearch(),"Element found: " + "List[" + 0 + "]");
        assertEquals(new Searching(a7,2319).linearSearch(),"Element found: " + "List[" + 11 + "]");
        assertEquals(new Searching(a7,404).linearSearch(),"Not found.");

        assertEquals(new Searching(a8,2).linearSearch(),"Element found: " + "List[" + 8 + "]");
        assertEquals(new Searching(a8,18).linearSearch(),"Element found: " + "List[" + 0 + "]");
        assertEquals(new Searching(a8,2319).linearSearch(),"Element found: " + "List[" + 11 + "]");
        assertEquals(new Searching(a8,404).linearSearch(),"Not found.");

        assertEquals(new Searching(a9,2).linearSearch(),"Element found: " + "List[" + 9 + "]");
        assertEquals(new Searching(a9,19).linearSearch(),"Element found: " + "List[" + 0 + "]");
        assertEquals(new Searching(a9,2319).linearSearch(),"Element found: " + "List[" + 11 + "]");
        assertEquals(new Searching(a9,404).linearSearch(),"Not found.");

        assertEquals(new Searching(a10,2).linearSearch(),"Element found: " + "List[" + 10 + "]");
        assertEquals(new Searching(a10,20).linearSearch(),"Element found: " + "List[" + 0 + "]");
        assertEquals(new Searching(a10,2319).linearSearch(),"Element found: " + "List[" + 11 + "]");
        assertEquals(new Searching(a10,404).linearSearch(),"Not found.");
        System.out.println("Searching test passed.");
    }

    @org.junit.Test
    public void testSorting(){
        int a1[] = {1,4,5,2319,8};
        int a2[] = {4,5,99,2,2319};
        int a3[] = {2319,2318};
        int a4[] = {4,5,0,2319,1,2};
        int a5[] = {25,18,33,65,1,2319};
        int a6[] = {44,22,2319,8,47,54};
        int a7[] = {27,33,55,23,2319};
        int a8[] = {28,19,48,33,2319};
        int a9[] = {32,55,76,45,2319};
        int a10[] = {45,67,55,23,2319,24};
        assertEquals("[1, 4, 5, 8, 2319]",new Sorting(a1).selectionSort());
        assertEquals("[2, 4, 5, 99, 2319]",new Sorting(a2).selectionSort());
        assertEquals("[2318, 2319]",new Sorting(a3).selectionSort());
        assertEquals("[0, 1, 2, 4, 5, 2319]",new Sorting(a4).selectionSort());
        assertEquals("[1, 18, 25, 33, 65, 2319]",new Sorting(a5).selectionSort());
        assertEquals("[8, 22, 44, 47, 54, 2319]",new Sorting(a6).selectionSort());
        assertEquals("[23, 27, 33, 55, 2319]",new Sorting(a7).selectionSort());
        assertEquals("[19, 28, 33, 48, 2319]",new Sorting(a8).selectionSort());
        assertEquals("[32, 45, 55, 76, 2319]",new Sorting(a9).selectionSort());
        assertEquals("[23, 24, 45, 55, 67, 2319]",new Sorting(a10).selectionSort());
        System.out.println("Sorting test passed.");
    }
}



  1. 参考http://www.cnblogs.com/maybe2030/p/4715035.html ,学习各种查找算法并在Searching中补充查找算法并测试
    提交运行结果截图

  2. 补充实现课上讲过的排序方法:希尔排序,堆排序,二叉树排序等(至少3个)
    测试实现的算法(正常,异常,边界)
    提交运行结果截图(如果编写多个排序算法,即使其中三个排序程序有瑕疵,也可以酌情得满分)

3.其它

这次的实验涉及到多种查找和排序的方法,不同的方法有不同的逻辑和效率,当我们需要进行更高层次的编程工作的时候,就需要锻炼逻辑思维能力,掌握较为复杂但是能够提高程序性能的算法。

参考资料

posted on 2020-11-27 20:18  节奏阿莱士  阅读(99)  评论(0编辑  收藏  举报

导航