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

课程:《程序设计与数据结构》
班级: 1923
姓名: 陈宇帆
学号:20192313
实验教师:王志强
实验日期:2020年11月19日
必修/选修: 必修

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个测试用例,提交测试用例设计情况(正常,异常,边界,正序,逆序),用例数据中要包含自己学号的后四位

提交运行结果图。

  • Searching代码
package Search;

public class Searching {

    public static Comparable linearSearch(Comparable[] data,Comparable target){

        Comparable result = null;
        int index = 0;
        while(result == null && index < data.length){
            if(data[index].compareTo(target)==0)
                result = data[index];
            index++;
        }

        return result;

    }

}

Sorting代码

package Search;

public class Sorting {
    public static void selectionSort(Comparable[] data) {
        int min;
        for(int index=0;index<data.length-1;index++){
            min=index;
            for(int scan=index+1;scan<data.length;scan++)
                if(data[scan].compareTo(data[min])<0)
                    min=scan;
            swap(data,min,index);
        }
    }
    private static void swap(Comparable[] data,int index1,int index2){
        Comparable temp=data[index1];
        data[index1]=data[index2];
        data[index2]=temp;
    }

}

Contact代码

package Search;
public class Contact implements Comparable
{

    private String firstName, lastName, phone;


    public Contact (String first, String last, String telephone)
    {

        firstName = first;
        lastName = last;

        phone = telephone;

    }


    public String toString ()
    {
        return lastName + ", " + firstName + ":  " + phone;
    }


    public int compareTo (Object other)
    {

        int result;
        result = phone.compareTo(((Contact)other).phone);
        return result;
    }

Test代码

package Search;

public class Test {
    public static void main(String[] args) {
        Contact[] a = new Contact[5];
        Contact[] b = new Contact[5];

        a[0] = new Contact("a", "b", "2303");
        a[1] = new Contact("c", "d", "2019");
        a[2] = new Contact("e", "f", "2000");
        a[3] = new Contact("g", "h", "1999");
        a[4] = new Contact("i", "j", "1923");
        b[0] = new Contact("k", "l", "123");
        b[1] = new Contact("m", "n", "234");
        b[2] = new Contact("o", "p", "345");
        b[3] = new Contact("q", "r", "456");
        b[4] = new Contact("s", "t", "567");
        Contact target1 = new Contact("", "", "123");//小边界
        Contact target2 = new Contact("", "", "2303");//大边界
        Contact target3 = new Contact("", "", "1923");//正常
        Contact target4 = new Contact("", "", "234");//正常
        Contact target5 = new Contact("", "", "345");//正常
        Contact target6 = new Contact("", "", "0010");//异常
        Contact target7 = new Contact("", "", "999");//异常


        Contact found[] = new Contact[10];
        found[0] = (Contact) Searching.linearSearch(b, target1);
        found[1] = (Contact) Searching.linearSearch(a, target2);
        found[2] = (Contact) Searching.linearSearch(a, target3);
        found[3] = (Contact) Searching.linearSearch(b, target4);
        found[4] = (Contact) Searching.linearSearch(b, target5);
        found[5] = (Contact) Searching.linearSearch(b, target6);
        found[6] = (Contact) Searching.linearSearch(a, target7);
        found[7] = (Contact) Searching.linearSearch(b, target7);
        found[8] = (Contact) Searching.linearSearch(a, target6);
        found[9] = (Contact) Searching.linearSearch(a, target1);
        for (int i = 1; i <= 10; i++) {
            System.out.println("Test" + i + ":");
            if (found[i - 1] == null)
                System.out.println("Can't found it!");
            else
                System.out.println("Found:  " + found[i - 1]);
        }

        Sorting.selectionSort(a);//倒序
        Sorting.selectionSort(b);//正序

        System.out.println("Test11:");
        for (Comparable play : a)
            System.out.println(play);
        System.out.println("Test12:");
        for (Comparable play : b)
            System.out.println(play);
    }
}

运行截图
20192313 2020-2021-1 《数据结构与面向对象程序设计》实验七报告
20192313 2020-2021-1 《数据结构与面向对象程序设计》实验七报告
20192313 2020-2021-1 《数据结构与面向对象程序设计》实验七报告
20192313 2020-2021-1 《数据结构与面向对象程序设计》实验七报告
20192313 2020-2021-1 《数据结构与面向对象程序设计》实验七报告

2.重构你的代码,把Sorting.java Searching.java放入 cn.edu.besti.cs1823.(姓名首字母+四位学号) 包中(例如:cn.edu.besti.cs1823.G2301)

把测试代码放test包中

重新编译,运行代码,提交编译,运行的截图(IDEA,命令行两种)

实验代码
Search包代码A

package Search;

import cn.edu.besti.cs1923.cyf2313.B;
import cn.edu.besti.cs1923.cyf2313.C;

public class A{
    public static void main(String[] args) {
        Contact[] a = new Contact[5];
        Contact[] b = new Contact[5];

        a[0] = new Contact("a", "b", "2303");
        a[1] = new Contact("c", "d", "2019");
        a[2] = new Contact("e", "f", "2000");
        a[3] = new Contact("g", "h", "1999");
        a[4] = new Contact("i", "j", "1923");
        b[0] = new Contact("k", "l", "123");
        b[1] = new Contact("m", "n", "234");
        b[2] = new Contact("o", "p", "345");
        b[3] = new Contact("q", "r", "456");
        b[4] = new Contact("s", "t", "567");
        Contact target1 = new Contact("", "", "123");//小边界
        Contact target2 = new Contact("", "", "2303");//大边界
        Contact target3 = new Contact("", "", "1923");//正常
        Contact target4 = new Contact("", "", "234");//正常
        Contact target5 = new Contact("", "", "345");//正常
        Contact target6 = new Contact("", "", "0010");//异常
        Contact target7 = new Contact("", "", "999");//异常


        Contact found[] = new Contact[10];
        found[0] = (Contact) B.linearSearch(b, target1);
        found[1] = (Contact) B.linearSearch(a, target2);
        found[2] = (Contact) B.linearSearch(a, target3);
        found[3] = (Contact) B.linearSearch(b, target4);
        found[4] = (Contact) B.linearSearch(b, target5);
        found[5] = (Contact) B.linearSearch(b, target6);
        found[6] = (Contact) B.linearSearch(a, target7);
        found[7] = (Contact) B.linearSearch(b, target7);
        found[8] = (Contact) B.linearSearch(a, target6);
        found[9] = (Contact) B.linearSearch(a, target1);
        for (int i = 1; i <= 10; i++) {
            System.out.println("Test" + i + ":");
            if (found[i - 1] == null)
                System.out.println("Can't found it!");
            else
                System.out.println("Found:  " + found[i - 1]);
        }

        C.selectionSort(a);//倒序
        C.selectionSort(b);//正序

        System.out.println("Test11:");
        for (Comparable play : a)
            System.out.println(play);
        System.out.println("Test12:");
        for (Comparable play : b)
            System.out.println(play);
    }
}

cn.edu.besti.cs1923.cyf2313包代码B 代码C

package cn.edu.besti.cs1923.cyf2313;

public class B {
    public static Comparable linearSearch(Comparable[] data,Comparable target){
        Comparable result = null;
        int index = 0;
        while(result == null && index < data.length){
            if(data[index].compareTo(target)==0)
                result = data[index];
            index++;
        }
        return result;
    }

}
package cn.edu.besti.cs1923.cyf2313;

public class C {
    public static void selectionSort(Comparable[] data) {
        int min;
        for(int index=0;index<data.length-1;index++){
            min=index;
            for(int scan=index+1;scan<data.length;scan++)
                if(data[scan].compareTo(data[min])<0)
                    min=scan;
            swap(data,min,index);
        }
    }
    private static void swap(Comparable[] data,int index1,int index2){
        Comparable temp=data[index1];
        data[index1]=data[index2];
        data[index2]=temp;
    }

}

运行截图
20192313 2020-2021-1 《数据结构与面向对象程序设计》实验七报告
20192313 2020-2021-1 《数据结构与面向对象程序设计》实验七报告
20192313 2020-2021-1 《数据结构与面向对象程序设计》实验七报告
20192313 2020-2021-1 《数据结构与面向对象程序设计》实验七报告

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

提交运行结果截图

Searching代码

package Task7_3;

public class Searching {
    public static Comparable LinearSearch(Comparable[] data,Comparable target)
    {
        Comparable result=null;
        int index=0;

        while(result==null&&index<data.length)
        {
            if(data[index].compareTo(target)==0)
                result=data[index];
            index++;
        }
        return result;
    }

    //二分查找
    public static Comparable binarySearch(Comparable[] data,Comparable target)
    {
        Comparable result=null;
        int first=0,last=data.length,mid;

        while(result==null&&first<=last)
        {
            mid=(first+last)/2;
            if(data[mid].compareTo(target)==0)
                result=data[mid];
            else
            if(data[mid].compareTo(target)>0)
                last=mid-1;
            else
                first=mid+1;
        }
        return result;
    }

    //顺序查找
    public static<T>
    boolean linearSearch(T[] data,int min,int max,T target)
    {
        int index=min;
        boolean found=false;

        while(!found&&index<=max)
        {
            found=data[index].equals(target);
            index++;
        }
        return found;
    }
    // 插值查找
    public static int InsertionSearch(int[] a, int value, int low, int high) {
        int mid = low + (value - a[low]) / (a[high] - a[low]) * (high - low);
        if (a[mid] == value) {
            return mid;
        }
        if (a[mid] > value) {
            return InsertionSearch(a, value, low, mid - 1);
        } else {
            return InsertionSearch(a, value, mid + 1, high);
        }
    }

    // 斐波那契查找
    public static int Fibonacci(int n) {
        if(n == 0) {
            return 0;
        }
        if(n == 1) {
            return 1;
        }
        return Fibonacci(n - 1) + Fibonacci(n - 2);
    }

    public static int FibonacciSearch(int[] data,int n,int key) {
        int low = 1;
        int high = n;
        int mid;

        int k = 0;
        while (n > Fibonacci(k) - 1) {
            k++;
        }
        int[] temp = new int[Fibonacci(k)];
        System.arraycopy(data, 0, temp, 0, data.length);
        for (int i = n + 1; i <= Fibonacci(k) - 1; i++) {
            temp[i] = temp[n];
        }

        while (low <= high) {
            mid = low + Fibonacci(k - 1) - 1;
            if (temp[mid] > key) {
                high = mid - 1;
                k = k - 1;
            }

            else if (temp[mid] < key) {
                low = mid + 1;
                k = k - 2;
            } else {
                if (mid <= n) {
                    return mid;
                }

                else {
                    return n;
                }
            }
        }
        return 0;
    }

SearchingTest代码

package Task7_3;


import junit.framework.TestCase;

public class SearchingTest extends TestCase {


    Integer[] a = {20, 19, 23, 3, 2303, 20, 11, 22, 26, 45, 11, 19};
    Integer[] b = {13, 14, 15, 11, 19, 20, 3,23,2303,24,47,56};
    Searching searching = new Searching();

    @org.junit.Test
    public void testLinearSearch() {
        assertEquals(2303, searching.LinearSearch(a, 2303));//大边界
        assertEquals(2303, searching.LinearSearch(b, 2303));//大边界
        assertEquals(null, searching.LinearSearch(a, 0));//异常
        assertEquals(null, searching.LinearSearch(b, 0));//异常
        assertEquals(3, searching.LinearSearch(a, 3));//小边界
        assertEquals(3, searching.LinearSearch(b, 3));//小边界
        assertEquals(22, searching.LinearSearch(a, 22));//正常
        assertEquals(11, searching.LinearSearch(b, 11));//正常
        assertEquals(23, searching.LinearSearch(a, 23));//正常
        assertEquals(23, searching.LinearSearch(b, 23));//正常
    }

    @org.junit.Test
    public void testbinarySearch() {
        // 正常测试
        Comparable list[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2303};
        // 元素在所查找的范围内
        assertEquals(2, Searching.binarySearch(list, 2));
        assertEquals(2303, Searching.binarySearch(list, 2303)); //边界测试
        //异常
        assertEquals(null, Searching.binarySearch(list, 0));
    }

    @org.junit.Test
    public void testinsertionSearch() {
        int list[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2303};
        // 元素在所查找的范围内
        assertEquals(1, Searching.InsertionSearch(list, 2, 0, 10));
        assertEquals(10, Searching.InsertionSearch(list, 2303, 0, 10)); //边界测试
    }

    @org.junit.Test
    public void testfibonacciSearch() {
        // 正常测试
        int list[] = {2, 23, 42,456, 2303};
        // 元素在所查找的范围内
        assertEquals(3, Searching.FibonacciSearch(list, 4, 456));
        assertEquals(4, Searching.FibonacciSearch(list, 4, 2303)); //边界测试
    }

运行截图
20192313 2020-2021-1 《数据结构与面向对象程序设计》实验七报告
20192313 2020-2021-1 《数据结构与面向对象程序设计》实验七报告
20192313 2020-2021-1 《数据结构与面向对象程序设计》实验七报告

4.补充实现课上讲过的排序方法:希尔排序,堆排序,二叉树排序等(至少3个)

测试实现的算法(正常,异常,边界)

提交运行结果截图

Searching 代码

package Task7_4;


import java.util.Arrays;

public class Searching<T> {

    public void   linear(int[] data, int target)
    {
        int count=0;
        for(int i = 0; i < data.length; i++) {
            if(data[i]==target){ count++;System.out.println("找到目标数的下标:"+i);
            }
        }
        if (count==0)
            System.out.println("没有找到目标数");
        else
            System.out.println("共有"+count+"个");
    }
    public void binarySearch(int[] data,int target)
    {
        int count=0;
        int low,high,mid;
        low=0;
        high=data.length-1;

        while (low<=high)
        {
            mid=(low+high)/2;
            if(data[mid]==target)
            { count++;
                System.out.println("找到目标数的下标:"+mid);
                break;
            }
            else if (data[mid]>target)
            {
                high=mid-1;
            }
            else
                low=mid+1;
        }
        if (count==0)
            System.out.println("没有找到目标数");
        else
            System.out.println("共有"+count+"个");

    }
    public  int binSearch(int srcArray[], int start, int end, int target) {
        int mid = (end - start) / 2 + start;
        if (srcArray[mid] == target) {
            return mid;
        }
        if (start >= end) {
            return -1;
        } else if (target > srcArray[mid]) {
            return binSearch(srcArray, mid + 1, end, target);
        } else if (target < srcArray[mid]) {
            return binSearch(srcArray, start, mid - 1, target);
        }
        return -1;
    }

    public    int insertSearch(int []data,int  left,int right,int target){

        if(left>right || target<data[0] ||target>data[data.length-1]){
            return -1;
        }

        int mid = left +(right - left) * (target - data[left])/ (data[right] -data[left]);
        int midVal =data[mid];
        if(target > midVal){
            return insertSearch(data, mid+1, right, target);

        }else if(target < midVal){
            return insertSearch(data, left, mid-1, target);
        }else {
            return mid;
        }
    }
    public static int[] fib(int []data) {
        int[] f = new int[data.length];
        f[0] = 1;
        f[1] = 1;
        for (int i = 2; i < data.length; i++) {
            f[i] = f[i - 1] + f[i - 2];
        }
        return f;
    }
    public  int fibSearch(int[] a, int target) {
        int low = 0;
        int high = a.length - 1;
        int k = 0;
        int mid = 0;
        int f[] = fib(a);
        while (high > f[k] - 1) {
            k++;
        }
        int[] temp = Arrays.copyOf(a, f[k]);
        for (int i = high + 1; i < temp.length; i++) {
            temp[i] = a[high];
        }
        while (low <= high) {
            mid = low + f[k - 1] - 1;
            if (target < temp[mid]) {
                high = mid - 1;
                k--;
            } else if (target > temp[mid]) {
                low = mid + 1;
                k -= 2;
            } else {
                if (mid <= high) {
                    return mid;
                } else {
                    return high;
                }
            }
        }
        return -1;
    }
}

Sorting 代码

package Task7_4;

public class Sorting {


    public void selectionSort(int[] data)
    {
        int min=0;
        for(int i=0;i<data.length;i++)
        {

            for (int j=i+1;j<data.length;j++)
            {
                if(data[i]>data[j])
                {min=data[j];
                    data[j]=data[i];
                    data[i]=min;}

            }

        }
        for(int i=0;i<data.length;i++)
        {
            System.out.println("test"+"["+i+"]"+": "+data[i]);
        }
    }
    public void insertSort(int []data)
    {
        int temp;
        int j ;
        for(int i=1;i<data.length;i++)
        {
            temp=data[i];//待插入的数
            for ( j=i-1;j>=0&&data[j]>=temp;j--)
            {
                data[j+1]=data[j];
            }
            data[j+1]=temp;

        }
        for(int i=0;i<data.length;i++)
        {
            System.out.println("test"+"["+i+"]"+": "+data[i]);
        }
    }

    public  void shellSort(int[] data)
    {
        int j = 0;
        int temp = 0;
        for (int increment = data.length / 2; increment > 0; increment /= 2)
        {
            for (int i = increment; i < data.length; i++)
            {
                temp = data[i];
                for (j = i; j >= increment&&temp <data[j - increment]; j -= increment)
                {
                    data[j] = data[j - increment];
                }
                data[j] = temp;
            }

        }
        for(int i=0;i<data.length;i++)
        {
            System.out.println("test"+"["+i+"]"+": "+data[i]);
        }
    }

}

SearchingSortTest 代码

package Task7_4;


import java.util.Stack;

public class SearchingSortTest {
    public static void main(String[] args) {

        int []test=new int[10];
        test[0]=1;
        test[1]=10;
        test[2]=1;
        test[3]=8;
        test[4]=8;
        test[5]=6;
        test[6]=2;
        test[7]=3;
        test[8]=0;
        test[9]=4;

        Searching searching=new Searching();
        System.out.println("线性查找: ");
        searching.linear(test, 1);
        Sorting sorting=new Sorting();
        sorting.selectionSort(test);
        searching.binarySearch(test, 2);
        System.out.println("二分查找: ");
        System.out.println("找到目标数的下标:"+searching.binSearch(test, 0, test.length-1, 1));
        System.out.println("插值查找: ");
        System.out.println("找到目标数的下标:"+searching.insertSearch(test, 0, test.length-1, 1));
        System.out.println("斐波那契查找: ");
        System.out.println("找到目标数的下标:"+ searching.fibSearch(test,3));

        /*Sorting sorting=new Sorting();*/
        System.out.println("希尔排序: ");
        sorting.shellSort(test);
        /*Sorting sorting=new Sorting();*/
        System.out.println("直接插入排序: ");
        sorting.insertSort(test);

    }
}

运行截图
20192313 2020-2021-1 《数据结构与面向对象程序设计》实验七报告
20192313 2020-2021-1 《数据结构与面向对象程序设计》实验七报告
20192313 2020-2021-1 《数据结构与面向对象程序设计》实验七报告
20192313 2020-2021-1 《数据结构与面向对象程序设计》实验七报告
20192313 2020-2021-1 《数据结构与面向对象程序设计》实验七报告

5.编写Android程序对实现各种查找与排序算法进行测试

提交运行结果截图

推送代码到码云

(未选做)

实验中遇到的问题和解决方法

实验还是挺顺畅的,没啥问题。

实验感悟

java学习可以这样比喻,看上去平平无奇,简单容易,一旦亲身实践起来会发现山重水复,一山放过一山拦。但也不是没有解决办法,因为java学习并不是一个人的学习,你只是在走别人走过的路,在觉得无路可走的时候,不妨问问过来人,你的学长学姐,你的老师,你的同学,你会很快发现新的办法,也就是柳暗花明。但同时,却不能轻易忽视动手锻炼的重要性,这绝不局限于老师课程要求的实验,平时还要勤加练习,敲敲课本代码就很关键,多练,多问,多总结,学习Java才能得心应手。

上一篇:Smart Bus Bar Monitoring System/智能母线监测解决方案


下一篇:避坑之Simulink的Contact forces安装