20202310肖衍豪《数据结构与面向对象程序设计》实验七报告

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

课程:《程序设计与数据结构》

班级: 2023
姓名:肖衍豪
学号:20202310
实验教师:王志强
实验日期:2021年11月4日
必修/选修: 必修

 一、实验内容

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

Sorting类代码

 

public static class Adam

{

    int ID ;

    int val ;

    String name ;

    Adam(int ID , String name , int val)

    {

    this.ID = ID ;

    this.name = name ;

    this.val = val ;

      }

}

Collections.sort(list, new Comparator<Object>()

{    

    public int compare(Object a , Object b)

    {

        Adam student1 = (Adam)a ;

        Adam student2 = (Adam)b ;

        return student1.ID - student2.ID ;

    }

}

  

Sorting测试代码:

public class AdamTest

 {

    public static void main(String[] args) {

        int a[] = new int[]{20,20,23,10,20,21,22,11,10,06};

        int b[] = a;

        Sorting c = new Sorting();

        c.SelectionSort1(a);

        for(int i=0;i<a.length;i++)

{

        System.out.printf(String.valueOf(a[i])+" ");}

        System.out.println("");

        c.SelectionSort2(b);

        for(int i=0;i<a.length;i++)

{

        System.out.printf(String.valueOf(b[i])+" ");

}

    }

}

  

测试结果:

 

 

Search类代码:

public static int binarySearch2(int[] array, int value) {

    int low = 0;

    int high = array.length - 1;

    while (low <= high) {

        int middle = low + ((high - low) >> 1);

        if (value == array[middle]) {

            return middle;

        }

        if (value > array[middle]) {

            low = middle + 1;

        }

        if (value < array[middle]) {

            high = middle - 1;

        }

    }

    return -1;

}

  

测试代码:

public class SearchingTest

{

    public static void main(String[] args) {

        int a[]=new int[]{20,20,23,10,20,21,22,11,06,03,68,54};

        Searching b = new Searching();

        b.linSearch(a,23);        b.linSearch(a,21);

        b.linSearch(a,11);

        b.linSearch(a,20);

        b.linSearch(a,68);

        b.linSearch(a,06);

        b.linSearch(a,3030);

        b.linSearch(a,2435);

    }

}

  

测试结果:

 

 

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

1.顺序查找

int SequenceSearch(int a[], int value, int n)

{

    int i;

    for(i=0; i<n; i++)

        if(a[i]==value)

            return i;

    return -1;

}

  

2.二分查找(折半查找)

int BinarySearch1(int a[], int value, int n)

{

    int low, high, mid;

    low = 0;

    high = n-1;

    while(low<=high)

    {

        mid = (low+high)/2;

        if(a[mid]==value)

            return mid;

        if(a[mid]>value)

            high = mid-1;

        if(a[mid]<value)

            low = mid+1;

    }

    return -1;

}

  

3.插值查找

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);

    if(a[mid]<value)

        return InsertionSearch(a, value, mid+1, high);

}

  

4.斐波那契查找

 

#include "stdafx.h"

#include <memory>

#include  <iostream>

using namespace std;

 

const int max_size=20;//斐波那契数组的长度

 

/*构造一个斐波那契数组*/

void Fibonacci(int * F)

{

    F[0]=0;

    F[1]=1;

    for(int i=2;i<max_size;++i)

        F[i]=F[i-1]+F[i-2];

}

 

/*定义斐波那契查找法*/  

int FibonacciSearch(int *a, int n, int key)  //a为要查找的数组,n为要查找的数组长度,key为要查找的关键字

{

  int low=0;

  int high=n-1;

  

  int F[max_size];

  Fibonacci(F);//构造一个斐波那契数组F

 

  int k=0;

  while(n>F[k]-1)//计算n位于斐波那契数列的位置

      ++k;

 

  int  * temp;//将数组a扩展到F[k]-1的长度

  temp=new int [F[k]-1];

  memcpy(temp,a,n*sizeof(int));

 

  for(int i=n;i<F[k]-1;++i)

     temp[i]=a[n-1];

  

  while(low<=high)

  {

    int mid=low+F[k-1]-1;

    if(key<temp[mid])

    {

      high=mid-1;

      k-=1;

    }

    else if(key>temp[mid])

    {

     low=mid+1;

     k-=2;

    }

    else

    {

       if(mid<n)

           return mid; //若相等则说明mid即为查找到的位置

       else

           return n-1; //若mid>=n则说明是扩展的数值,返回n-1

    }

  }  

  delete [] temp;

  return -1;

}

 

int main()

{

    int a[] = {0,16,24,35,47,59,62,73,88,99};

    int key=100;

    int index=FibonacciSearch(a,sizeof(a)/sizeof(int),key);

    cout<<key<<" is located at:"<<index;

    return 0;

}

  

 

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

1选择排序

package sort;

 public class SelectSort {

public static void main(String[] args) {

int[] array = {52,63,14,59,68,35,8,67,45,99};

System.out.println("原数组:");

for (int i : array) {

System.out.print(i+" ");

}

System.out.println();

selectSort(array);

System.out.println("排序后:");

for (int i : array) {

System.out.print(i+" ");

}

}

 

public static void selectSort(int[] arr){

for(int i = 0; i < arr.length-1; i++){

int min = i;

for(int j = i+1; j <arr.length ;j++){

if(arr[j]<arr[min]){

min = j;

}

}

if(min!=i){

swap(arr, i, min);

}

}

}

public static void swap(int[] arr,int a,int b){

int temp = arr[a];

arr[a] = arr[b];

arr[b] = temp;

}

}

  

2).冒泡排序

package 冒泡排序;

import java.util.Arrays;

public class BubbleSort1 {

public static void BubbleSort(int[] arr)

 {

        boolean flag = true;

        while(flag)

{

            int temp;            

for(int i=0;i<arr.length-1;i++)

{

                for(int j=0;j<arr.length-i-1;j++){

                    if(arr[j+1]<arr[j])

{

                        temp = arr[j];

                        arr[j] = arr[j+1];

                        arr[j+1] = temp;

                        flag = true;

                    }

                }

                if(!flag){

                    break;

                }

            }

        }

    }

    public static void main(String[] args) {

        int arr[] = new int[]{1,6,2,2,5};

        BubbleSort.BubbleSort(arr);

        System.out.println(Arrays.toString(arr));

    }

}

  

3.插入排序

public class Insert

{

public static void main(String[] args)

{

int[] ins = {2,3,5,1,23,6,78,34};

int[] ins2 = sort(ins);

for(int in: ins2){

System.out.println(in);

}

}

 

public static int[] sort(int[] ins){

 

for(int i=1; i<ins.length; i++){

for(int j=i; j>0; j--){

if(ins[j]<ins[j-1]){

int temp = ins[j-1];

ins[j-1] = ins[j];

ins[j] = temp;

}

}

}

return ins;

}

}

  

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

三、 实验过程中遇到的问题和解决过程

 

 

 

 

 

 

 

 

posted @ 2021-11-14 22:20  20202310肖衍豪  阅读(44)  评论(0编辑  收藏  举报