20192303 2020-2021-1 《数据结构与面向对象程序设计》实验七报告
课程:《程序设计与数据结构》
班级: 1923
姓名: 杨佳宁
学号:20192303
实验教师:王志强
实验日期:2020年11月19日
一、实验内容
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程序对实现各种查找与排序算法进行测试
提交运行结果截图
推送代码到码云(选做,加分)
二、实验过程及结果
(1)定义了Searching和Sorting类,并在类中实现linearSearch,SelectionSort方法。使用测试用例进行检测。
编写Searching.java
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.java
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;
}
}
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;
}
}
编写测试代码
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);
}
}
运行结果截图:
代码截图:
(2)把Sorting.java和Searching.java放入cn.edu.besti.cs1923.yjn2303包中并重新进行测试。
实验二的代码与实验一的代码基本一致,只需在测试代码中导入包即可
Part1:使用IDEA
运行结果截图:
Part2:使用命令行
(3)参考http://www.cnblogs.com/maybe2030/p/4715035.html ,学习各种查找算法并在Searching中补充查找算法并测试
public class Searching2 {
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;
}
}
测试代码
import junit.framework.TestCase;
public class Searching2Test 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};
Searching2 searching = new Searching2();
@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, Searching2.binarySearch(list, 2));
assertEquals(2303, Searching2.binarySearch(list, 2303)); //边界测试
//异常
assertEquals(null, Searching2.binarySearch(list, 0));
}
@org.junit.Test
public void testinsertionSearch() {
int list[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2303};
// 元素在所查找的范围内
assertEquals(1, Searching2.InsertionSearch(list, 2, 0, 10));
assertEquals(10, Searching2.InsertionSearch(list, 2303, 0, 10)); //边界测试
}
@org.junit.Test
public void testfibonacciSearch() {
// 正常测试
int list[] = {2, 23, 42,456, 2303};
// 元素在所查找的范围内
assertEquals(3, Searching2.FibonacciSearch(list, 4, 456));
assertEquals(4, Searching2.FibonacciSearch(list, 4, 2303)); //边界测试
}
}
运行结果截图:
(4)补充排序算法:
public class Sorting3 {
//选择排序
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;
}
public static void ShellSort(int[] data)
{
int m = 0;
int temp = 0;
// 每次将步长缩短为原来的一半
for (int gap = data.length / 2; gap > 0; gap =gap/2)
{
for (int i = gap; i < data.length; i++)
{ //temp保存索引为初始gap的值
temp = data[i];
//从i开始,
for (m = i; m >= gap; m = m-gap)
{ //将按步长分好的同组元素进行比较
if(temp < data[m - gap])
{//升序
data[m] = data[m - gap];
}
else
{
break;
}
}
data[m] = temp;
}
}
}
}
测试代码:
import org.junit.Test;
import junit.framework.TestCase;
public class Sorting3Test extends TestCase {
@org.junit.Test
public void testshellSort() {
int list[] = {1,2,3,4,19,20,23};
// 正常测试+边界测试
int list1[] = {4,3,2,1,23,20,19};
Sorting3.ShellSort(list1);
assertEquals(list1[0], list[0]);//边界
assertEquals(list1[3],list[3]);//正常
// 异常测试
/*int listerror[] = {1,2,3,4,19,20,23};
Sorting3.ShellSort(listerror);
assertEquals(list[9],list[9]);*/
}
}
三、实验中遇到的问题及解决过程
问题1:使用命令行进行编译时,出现“编码GDK的不可映射字符(0x8c)”
问题1解决方法:在需要进行编译的程序前加“-encoding UTF-8”即可
四、其他(感悟、思考等)
这次的实验学习了众多的查找以及排序算法,在编码过程中,我们需要先弄清每种算法的原理。虽然最终得出的结果是相同的,但不同的代码在效率上是有差别的。
五、参考资料
《JAVA程序设计与数据结构教程(第二版)》
《JAVA程序设计与数据结构教程(第二版)学习指导》