# 20182327 2019-2020-1 《数据结构与面向对象程序设计》实验七报告
课程:《程序设计与数据结构》
班级: 1823
姓名:赵天昊
学号:20182327
实验教师:王志强
实验日期:2019年10月30日
必修/选修: 必修
1.实验内容
1.定义一个Searching和Sorting类,并在类中实现linearSearch,SelectionSort方法,最后完成测试。
要求不少于10个测试用例,提交测试用例设计情况(正常,异常,边界,正序,逆序),用例数据中要包含自己学号的后四位2.重构你的代码
把Sorting.java Searching.java放入 cn.edu.besti.cs1823.(姓名首字母+四位学号) 包中(例如:cn.edu.besti.cs1823.G2301)
把测试代码放test包中3.参考http://www.cnblogs.com/maybe2030/p/4715035.html ,学习各种查找算法并在Searching中补充查找算法并测试
4.补充实现课上讲过的排序方法:希尔排序,堆排序,二叉树排序等(至少3个)
测试实现的算法(正常,异常,边界)5.编写Android程序对实现各种查找与排序算法进行测试,提交运行结果截图
2.实验过程和结果
- 实验一:
- 实验二:
-
//线性查找
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-1, mid;
while (result == null && first <= last)
{
mid = (first + last) / 2; // determine midpoint
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 Comparable sequenceSearch(Comparable a[], Comparable value, int n) {
for (int i = 0; i < n; i++)
if (a[i].compareTo(value) == 0)return i; return -1;
}
//插值查找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
{
for (int index = 1; index < data.length; index++)
{
T key = data[index];
int position = index;
while (position > 0 && data[position-1].compareTo(key) > 0)
{
data[position] = data[position-1];
position--;
}
data[position] = key;
}
}
//冒泡排序
public static >
void bubbleSort(T[] data)
{
int position, scan;
T temp;
for (position = data.length - 1; position >= 0; position--)
{
for (scan = 0; scan <= position - 1; scan++)
{
if (data[scan].compareTo(data[scan+1]) > 0)
swap(data, scan, scan + 1);
}
}
}
//快速排序
public static > void quickSort(T[] data)
{
quickSort(data, 0, data.length - 1);
}
private static >
void quickSort(T[] data, int min, int max)
{
if (min < max)
{
int indexofpartition = partition(data, min, max);
quickSort(data, min, indexofpartition - 1);
quickSort(data, indexofpartition + 1, max);
}
}
private static >
int partition(T[] data, int min, int max)
{
T partitionelement;
int left, right;
int middle = (min + max) / 2;
partitionelement = data[middle];
// move it out of the way for now
swap(data, middle, min);
left = min;
right = max;
while (left < right)
{
// search for an element that is > the partition element
while (left < right && data[left].compareTo(partitionelement) <= 0)
left++;
// search for an element that is < the partition element
while (data[right].compareTo(partitionelement) > 0)
right--;
// swap the elements
if (left < right)
swap(data, left, right);
}
swap(data, min, right);
return right;
}
//希尔排序
public static > void shellsort(T[] DATA){
for (int gap=DATA.length/2;gap>0;gap=gap/2){
for (int n = gap;n=0&&DATA[i].compareTo(DATA[i-gap])<0){
swap(DATA,i,i-gap);
i-=gap;
}
}
}
}
//堆排序
public static >void heapSort(T[] data)
{
ArrayHeap ArrayHeap = new ArrayHeap<>();
for (int n=0;n>void binaryTreeSort(T[] data)
{
LinkedBinarySearchTree linkedBinarySearchTree = new LinkedBinarySearchTree<>();
for (int i=0;i
3. 实验过程中遇到的问题和解决过程
- 问题一:调用队列头的数据时,出现了空指针报错
问题一解决方法:
重新编辑驱动代码,确认调用无误值已初始化。- 问题二:一个应用的activity跳转到另一个应用的activity
问题二解决方法:
Intent intent = new Intent();
ComponentName cn = new ComponentName("com.corp.timetest","com.corp.timetest.LoginActivity");
intent.setComponent(cn);
或者设置className,取代设置componentName:
intent.setClassName("com.landicorp.packname.test", "com.landicorp.packname.test.secondpkg.jump.TaskAffinityTestActivity");
- 问题三:Android Studio找不到R.menu
- 问题三解决办法:创建res/menu文件夹,创建menu_main文件。
其他(感悟、思考等)
一节课学完安卓,不知道是我飘了还是老师太信任我,好多同学好牛,自己好菜,各种把打好的代码推翻重来,参考别人代码改来改去把自己的程序变成了四不像,运行不了,本周更加熟悉了运行一个目的堆了四五个程序的Java常态,继续虚心学习吧。