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

课程:《程序设计与数据结构》
班级: 2023
姓名: 邬昱初
学号:20202321
实验教师:王志强
实验日期:2021年11月12日
必修/选修: 必修

一、实验内容

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

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

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

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

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

二、实验过程及结果

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

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

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

Searching类代码如下

package cn.edu.besti.cs2023.W2321;

public class Searching_Maru {

/*线性查找*/
public int linearSearch(int[] data, int target)/*有哨兵类*/ {
    int j = 1;
    data[0] = target;
    for (int i = data.length - 1; data[i] != target; i--) {
        j = i;
    }
    return j - 1;
}

public boolean linearSearchNone(int[] data, int target)/*无哨兵类*/ {
    boolean result = false;
    for (int i = 0; i < data.length; i++) {
        if (data[i] == target) {
            result = true;
        }
    }
    return result;
}

/*二分查找*/
public boolean binarySearch(int[] data, int target) {
    int low = 0, mid, high = data.length - 1;
    while (low <= high) {
        mid = (low + high) / 2;
        if (data[mid] == target) {
            return true;
        } else if (data[mid] > target) {
            high = mid - 1;
        } else
            low = mid + 1;
    }
    return false;
}

/*插值查找*/
public boolean insertionSearch(int[] data, int target, int low, int high) {
    int mid = low + (target - data[low]) / (data[high] - data[low]) * (high - low);
    if (target<low||target>high)
        return false;
    if (data[mid] == target) {
        return true;
    } else if (data[mid] > target) {
        if(mid>=low&&mid<=high)
        return insertionSearch(data, target, low, mid - 1);
        else
            return false;
    } else
        if(mid>=low&&mid<=high)
        return insertionSearch(data, target, mid + 1, high);
        else
            return false;

}

/*斐波那契查找*/
public int Fibonacci(int n) {
    if (n == 0) {
        return 0;
    }
    if (n == 1) {
        return 1;
    }
    return Fibonacci(n - 1) + Fibonacci(n - 2);
}
public boolean fibonacciSearch(int[] data, int target){
    int n=data.length-1;
    int low = 1, high = n, 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] > target) {
            high = mid - 1;
            k = k - 1;
        } else if (temp[mid] < target) {
            low = mid + 1;
            k = k - 2;
        } else {
            if (mid <= n) {
                return true;
            } else {
                return false;
            }
        }
    }
    return false;

}

}

树表查找代码如下

package cn.edu.besti.cs2023.W2321;

public class BST_Maru {
public BSTNode_Maru root;
public BST_Maru(){
this.root=null;
}
public void insertBST(int data){
if (root==null){
root=new BSTNode_Maru();
root.key=data;
}
else {
insertbst(this.root,data);
}

}
public void insertbst(BSTNode_Maru node,int key){
    if(node==null||node.key==key){
        return;
    }else if (node.key>key){
        if (node.leftChild==null){
            BSTNode_Maru temp = new BSTNode_Maru();
            temp.key = key;
            node.leftChild = temp;
           // node.leftChild.key=key;
        }else {
            insertbst(node.leftChild,key);
        }

    }else {
        if (node.rightChild==null){
            BSTNode_Maru temp = new BSTNode_Maru();
            temp.key = key;
            node.rightChild = temp;
            // node.rightChild.key=key;
        }else{
            insertbst(node.rightChild,key);
        }
    }
}
public BSTNode_Maru BSTSearch(int key){
    if (this.root==null){
        return null;
    }else {
        if (this.root.key==key){
            return root;
        }
        else {
            return this.bstSearch(root,key);
        }
    }

}
public BSTNode_Maru bstSearch(BSTNode_Maru node,int key){
    if (node.key==key){
        return  node;
    }
    else if (node.key>key){
        if (node.leftChild!=null){
            return bstSearch(node.leftChild,key);
        }else {
            return null;
        }
    }else {
        if (node.rightChild!=null){
            return bstSearch(node.rightChild,key);
        }else {
            return null;
        }
    }
}
public boolean isBSTfound(int[] data,int target){
    BSTNode_Maru node=new BSTNode_Maru();
    BST_Maru Data=new BST_Maru();
    int temp;
    for (int i=0;i<data.length;i++){
        temp=data[i];
        Data.insertBST(temp);
    }
    node= Data.BSTSearch(target);
    if(node!=null)
        return true;
    return false;
}

class BSTNode_Maru {
public int key;
public BSTNode_Maru leftChild;
public BSTNode_Maru rightChild;
public BSTNode_Maru(){
this.leftChild=null;
this.rightChild=null;
}
}
}

分块查找代码如下

package cn.edu.besti.cs2023.W2321;

public class Block_Maru {
private int data[];
private int length;
public boolean BlockSearch(Block_Maru bm,int[][] blockTable,int bmlength,int btlength,int target){
int low=0,high=btlength-1,mid;
BlockTable bt=bm.new BlockTable();
bt.createBlockTable(blockTable);
while (low<=high){
mid=(low+high)/2;
if (bt.node[mid].top>=target){
high=mid-1;
}
else
low=mid+1;
}
if (low<btlength){
int i=bt.node[low].start;
while (i<=bt.node[low].start+bmlength/btlength-1&&bm.data[i]!=target){
i++;
}
if (i<=bt.node[low].start+bmlength/btlength-1){
return true;
}else
return false;

    }
    return false;
}
public void createBlock(int[] data){
    this.data=new int[data.length];
    for (int i=0;i<data.length;i++){
        this.data[i]=data[i];
        this.length++;
    }
}
public class Node{
    public int top;
    public int start;
}
public class BlockTable{
    public Node[] node;
    public int length=0;
    public void createBlockTable(int data[][]){
        this.node=new  Node[data.length];
        for (int i=0;i<data.length;i++){
            node[i]=new Node();
            node[i].top=data[i][0];
            node[i].start=data[i][1];
            this.length++;
        }
    }
}

}

哈希表查找代码如下

package cn.edu.besti.cs2023.W2321;

public class HashSearch_Maru {
public boolean hashSearch(int[] data,int target){
int prime=0; //除留余数法
for(int i= data.length;i>0;i--){
if(isPrime(i)){
if (prime0){
prime=i;
}
}
}
Node[] hashtable= createHashTable(data,prime);
// 查找对应值
int key=target%prime;
Node cur=hashtable[key];
while (cur!=null&&cur.key!=key){
cur=cur.next;
}
if (cur
null){
return false;
}else {
return true;
}
}
public Node[] createHashTable(int[] data,int allNode){
Node[] hashtable=new Node[allNode];
int key;
for(int i=0;i<data.length;i++){
Node node=new Node();
node.key=data[i];
key=data[i]%allNode;
if(hashtable[key]null){
hashtable[key]=node;
}else { //链地址法进行处理
Node Next=hashtable[key];
while (Next!=null&&Next.next!=null){
Next =Next.next;
}
Next.next=node;
}
}
return hashtable;
}
public boolean isPrime(int index){
for(int i=2;i<index;i++){
if (index%i
0){
return false;
}
}
return true;
}

}
class Node{

int key;
Node next;

}

Test类代码如下

package Searching_and_Sorting;
import cn.edu.besti.cs2023.W2321.Block_Maru;
import cn.edu.besti.cs2023.W2321.HashSearch_Maru;
import cn.edu.besti.cs2023.W2321.Searching_Maru;
import cn.edu.besti.cs2023.W2321.BST_Maru;
import junit.framework.TestCase;
import org.junit.Test;
public class SearchTest extends TestCase{
Searching_Maru data1=new Searching_Maru();
/线性查找/
int[] array1={5,8,13,9,20,20,23,21,11,11,10};
@Test
public void testlinearSearch()throws Exception{
assertEquals(true,data1.linearSearchNone(array1,5));//边界
assertEquals(true,data1.linearSearchNone(array1,13));//正常
assertEquals(false,data1.linearSearchNone(array1,2321));//异常
}
/二分查找/
int[] array2={2,3,5,11,15,20,21,23} ;//按顺序排序
@Test
public void testbinarySearch()throws Exception{
assertEquals(true,data1.binarySearch(array2,2));//边界
assertEquals(true,data1.binarySearch(array2,20));//正常
assertEquals(false,data1.binarySearch(array2,2321));//异常
}
/插值查找/
@Test
public void testinsertionSearch()throws Exception{
int[] array3=new int[100];
for (int i=0;i<array3.length-1;i++){
array3[i]=i+1;
}
assertEquals(true,data1.insertionSearch(array3,2,1,10));//边界
assertEquals(true,data1.insertionSearch(array3,20,10,30));//正常
assertEquals(false,data1.insertionSearch(array3,2321,1,99));//异常
}

/*斐波那契查找*/
int[] array4={3,20,23,42,86,2321};
@Test
public void testFibonacciSearch()throws Exception{
    assertEquals(true,data1.fibonacciSearch(array4,2321));//边界
    assertEquals(true,data1.fibonacciSearch(array4,23));//正常
    assertEquals(false,data1.fibonacciSearch(array4,-1));//异常
}
/*树表查找*/
BST_Maru data2=new BST_Maru();
int[] array5={5,7,88,67,11,2321,13,114,2021,42};
@Test
public void testBST()throws Exception{
   assertEquals(true,data2.isBSTfound(array5,42));//边界
   assertEquals(true,data2.isBSTfound(array5,2321));//正常
   assertEquals(false,data2.isBSTfound(array5,-1));//异常
}
/*分块查找*/
Block_Maru data3=new Block_Maru();
int[] array6={4, 42, 36, 27, 86, 2321, 6657, 123321, 1, 33, 9, 58,11,12};
int[][] array6Table={{42,0},{123321,4},{58,9}};
@Test
public void testBlockSearch()throws Exception{
    data3.createBlock(array6);
    assertEquals(true,data3.BlockSearch(data3,array6Table,array6.length,array6Table.length,4));//边界
    assertEquals(true,data3.BlockSearch(data3,array6Table,array6.length,array6Table.length,2321));//正常
    assertEquals(false,data3.BlockSearch(data3,array6Table,array6.length,array6Table.length,100));//异常
}

/*哈希查找*/
HashSearch_Maru data4=new HashSearch_Maru();
int[] array7={23,21,2321,20,11,12,17,34,2020,2023};
@Test
public void testHashSearch()throws Exception{
    assertEquals(true,data4.hashSearch(array7,2023));//边界
    assertEquals(true,data4.hashSearch(array7,2321));//正常
    assertEquals(false,data4.hashSearch(array7,100));//异常

}

}

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

Sorting代码如下

package cn.edu.besti.cs2023.W2321;
public class newSorting_Maru {
public int[] 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;}

        }

    }
    return data;

}
public int[] 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;

    }
    return data;

}

public  int[] 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;
        }

    }
    return data;

}

}

测试结果为

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

  1. 实验过程中遇到的问题和解决过程
  • 问题1:出现写的类不能引用的情况

  • 问题解决方案:主要是对static的认识还不够清晰,而不同位置的class的用法也不同,经过对其和代码调整勉强实现了运行。

  • 问题2:哈希查找实现时链地址法出现next为null的情况

  • 问题解决方案:debug调试失败后对代码进行了逐条检查无果,第二天再次查询时发现可能是while里的判定条件出现问题,于是进行修改后运行成功。

4.实验体会
-对基础知识认识更加清晰,对查找与排序了解增多,尤其在哈希与树表两方面的知识得到丰富。

参考资料

posted on 2021-11-14 16:02  MaruForever  阅读(28)  评论(0编辑  收藏  举报

导航