20192312吴欣欣 实验七《查找与排序》实验报告
20192312 2020-2021-1 实验七 《查找与排序》实验报告
课程:《程序设计与数据结构》
班级: 1923
姓名: 吴欣欣
学号:20192312
实验教师:王志强
实验日期:2020年11月26日
必修/选修: 必修
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 ex7;
import com.ibm.icu.impl.UResource;
import java.util.Arrays;
import java.util.Scanner;
public class Searching {
int data[];
public Searching(int m[]){
data=m;
}
public int[] Input(){
final int CAPACITY=3;
data=new int[CAPACITY];
int i=0;
Scanner scan=new Scanner(System.in);
System.out.print("输入数组(以*作为结尾):");
while(scan.hasNextInt()){
if(i==data.length)
data= Arrays.copyOf(data,data.length+CAPACITY);
data[i]=scan.nextInt();
i++;
}
scan.next();
return data;
}
public int LinearSearch(int target){
int m[]=Arrays.copyOf(data,data.length+1);
int i,tail=m.length-1;
m[tail]=target;
for(i=0;;i++){
if(m[i]==target)return i+1;
}
}
}
Sorting代码
package ex7;
import java.util.Arrays;
import java.util.Scanner;
public class Sorting {
int []data;
int top;
public Sorting(int m[]){
data=m;
}
public void Input() {
final int CAPACITY = 3;
data= new int[CAPACITY];
Scanner scan = new Scanner(System.in);
int i = 0;
System.out.print("输入数组(以*作为结尾):");
while (scan.hasNextInt()) {
if (i == data.length)
data = Arrays.copyOf(data, data.length + CAPACITY);
data[i] = scan.nextInt();
i++;
}
top=i;
scan.next();
}
public void swap(int []m,int a,int b){
int temp;
temp=m[a];
m[a]=m[b];
m[b]=temp;
}
public void Print(int m[]){
for(int i=0;i<top;i++){
System.out.print(m[i]+" ");
}
System.out.println("");
}
public void SelectionSort(){
int i,j;
for(i=0;i<top-1;i++){
int temp=i;
for(j=i+1;j<top;j++){
if(data[j]<data[temp])
temp=j;
}
if(temp!=i) {
swap(data, temp, i);
}
}
Print(data);
}
}
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中补充查找算法并测试
提交运行结果截图
Node
package cn.edu.besti.cs1923.W2312;
public class Node {
int index;
public int data;//关键字段
public Node leftNode;//左节点
public Node rightNode;//右节点
public Node() {
index=-1;
this.data = -1;
leftNode = null;
rightNode = null;
}
public Node(int value,int i) {
index=i;
this.data = value;
leftNode = null;
rightNode = null;
}
public int findNode(Node root, int key) {
Node current = root;
while (current.data != key) {
if (key < current.data) {//左节点
current = current.leftNode;
} else {//右节点
current = current.rightNode;
}
if (current == null) {
return -1;
}
}
return current.index+1;
}
public Node InsertNode(int m[]) {
Node root = new Node(m[0],0);
int i=1;
while (m[i] != 0 && i < m.length) {
Node current = root;
while(current.data!=m[i]){
if(m[i]<current.data){
if(current.leftNode==null)
current.leftNode=new Node(m[i],i);
current=current.leftNode;
}
else if (m[i]>current.data){
if(current.rightNode==null)
current.rightNode=new Node(m[i],i);
current=current.rightNode;
}
}
i++;
}
return root;
}
}
Searching
package cn.edu.besti.cs1923.W2312;
import java.util.Arrays;
import java.util.Scanner;
public class Searching {
int data[];
int top;
public Searching(int m[]){
data=m;
for(top=0;top<data.length&&m[top]!=0;top++);
}
public int[] Input(){
final int CAPACITY=3;
data=new int[CAPACITY];
int i=0;
Scanner scan=new Scanner(System.in);
System.out.print("输入数组(以*作为结尾):");
while(scan.hasNextInt()){
if(i==data.length)
data= Arrays.copyOf(data,data.length+CAPACITY);
data[i]=scan.nextInt();
i++;
}
top=i;
scan.next();
return data;
}
public int LinearSearch(int data[],int target){
int m[]=Arrays.copyOf(data,data.length+1);
int i,tail=m.length-1;
m[tail]=target;
for(i=0;;i++){
if(m[i]==target){
if(i==m.length-1)return -1;
else return i+1;
}
}
}
public int BinarySearch(int t[] ,int min,int max,int target,int i){
int m[]=Arrays.copyOf(t,t.length+1);
int mid=(min+max)/2;
if(i==0) {
Sorting sort=new Sorting(m);
sort.SelectionSort();
}
if(target==m[mid])return mid+1;
else if(target>m[mid]) {
if(mid>=min&&mid<=max)
return BinarySearch(m, mid + 1, max, target, i + 1);
else return -1;
}
else {
if(mid>=min&&mid<=max)
return BinarySearch(m, min, mid - 1, target, i + 1);
else return -1;
}
}
public int InsertSearch(int t[] ,int min,int max,int target,int i){
int m[]=Arrays.copyOf(t,t.length+1);
int mid=min+(target-m[min])/(m[max]-m[min])*(max-min);
if(i==0){
Sorting sort =new Sorting(m);
sort.SelectionSort();
}
if(target==m[mid])return mid+1;
else if(target>m[mid]) {
if(mid>=min&&mid<=max)
return BinarySearch(m, mid + 1, max, target, i + 1);
else return -1;
}
else {
if(mid>=min&&mid<=max)
return BinarySearch(m, min, mid - 1, target, i + 1);
else return -1;
}
}
public int FibonacciSearch(int data[] ,int length,int target){
int low=0;
int high=length-1;
int F[]=new int[20];
int m[]=Arrays.copyOf(data,data.length+1);
Sorting sort=new Sorting(m);
sort.SelectionSort();
Fibonacci(F);//构造一个斐波那契数组F
int k=0;
while(length>F[k]-1)//计算n位于斐波那契数列的位置
++k;
int [] temp;//将数组a扩展到F[k]-1的长度
temp=Arrays.copyOf(m,F[k]-1);
for(int i=length;i<F[k]-1;++i)
temp[i]=m[length-1];
while(low<=high)
{
int mid=low+F[k-1]-1;
if(target<temp[mid])
{
high=mid-1;
k-=1;
}
else if(target>temp[mid])
{
low=mid+1;
k-=2;
}
else
{
if(mid<length)
return mid+1; //若相等则说明mid即为查找到的位置
else
return length; //若mid>=n则说明是扩展的数值,返回n-1
}
}
return -1;
}
public void Fibonacci(int F[])
{
F[0]=0;
F[1]=1;
for(int i=2;i<20;++i)
F[i]=F[i-1]+F[i-2];
}
public int BinaryTreeSearch(int data[],int target){
int m[]=Arrays.copyOf(data,top+1);
Node node=new Node();
node.InsertNode(m);
return node.findNode(node.InsertNode(m),target);
}
public int HashSearch(int data[] ,int target){
int m[]=HashInsert(data);
int l=m.length;
int i=target%l,j=target%l;
if(target==m[i])return i+1;
for( i=(i+1)%l;i!=j;i=(i+1)%l){
if(target==m[i])return i+1;
}
return -1;
}
public int[] HashInsert(int data[]){
int m[]=new int[top];
int j=0,l=m.length;
for(int i=0;i<l;i++){
j=data[i]%l;
if(m[j]==0)m[j]=data[i];
else{
while(m[j]!=0)j=(j+1)%l;
m[j]=data[i];
}
}
for(j=0;j<l;j++)
System.out.print(m[j]+" ");
System.out.println("");
return m;
}
public int BlockSearch (int data[],int target) throws ArrayIndexOutOfBoundsException{
int n=(top-1)/5+1;
int[][]m= new int [n][5];
int arr[]=Arrays.copyOf(data,top+1);
Sorting sort=new Sorting(arr);
sort.SelectionSort();
int max[]=new int[n];
int t=0;
for(int i=0;i<n;i++) {
int j;
for (j = 0; j < 5; j++) {
m[i][j] = arr[t];
t++;
if(t>=top)break;
}
if(j==5)
max[i]=m[i][4];
else max[i]=arr[t-1];
}
for(t=n-1;t>=0&&target<=max[t];t--);
if(target<arr[0]||target>arr[arr.length-2])return -1;
Searching search=new Searching(m[t+1]);
int p=search.LinearSearch(m[t+1],target);
if(p==-1)return -1;
return 5*(t+1)+p;
}
}
测试代码
4.补充实现课上讲过的排序方法:希尔排序,堆排序,二叉树排序等(至少3个)
测试实现的算法(正常,异常,边界)
提交运行结果截图(如果编写多个排序算法,即使其中三个排序程序有瑕疵,也可以酌情得满分)
package cn.edu.besti.cs1923.W2312;
import java.util.Arrays;
import java.util.Scanner;
public class Sorting {
int []data;
int top;
public Sorting(int m[]){
data=m;
for(top=0;top<data.length&&m[top]!=0;top++);
}
public void Input() {
final int CAPACITY = 3;
data= new int[CAPACITY];
Scanner scan = new Scanner(System.in);
int i = 0;
System.out.print("输入数组(以*作为结尾):");
while (scan.hasNextInt()) {
if (i == data.length)
data = Arrays.copyOf(data, data.length + CAPACITY);
data[i] = scan.nextInt();
i++;
}
top=i;
scan.next();
}
public void swap(int []m,int a,int b){
int temp;
temp=m[a];
m[a]=m[b];
m[b]=temp;
}
public String Print(int m[]){
String a="";
for(int i=0;i<top;i++){
a+=m[i]+" ";
System.out.print(m[i]+" ");
}
System.out.println("");
return a;
}
public String SelectionSort(){
int i,j;
for(i=0;i<top-1;i++){
int temp=i;
for(j=i+1;j<top;j++){
if(data[j]<data[temp])
temp=j;
}
if(temp!=i) {
swap(data, temp, i);
}
}
return Print(data);
}
public String ShellSort(){
for(int gap=top/2;gap>0;gap/=2){
for(int i=gap;i<top;i++){
int j=i;
while (j-gap>=0&&data[j]<data[j-gap]){
swap(data,j,j-gap);
j-=gap;
}
}
}
return Print(data);
}
}
5.5.编写Android程序对实现各种查找与排序算法进行测试
提交运行结果截图
推送代码到码云(选做,加分)
package com.example.searching;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText s1;
EditText s2;
EditText s3;
Button a1;
Button a2;
Button a3;
Button a4;
Button a5;
Button a6;
Button a7;
Button a8;
Button a9;
int d[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1 = findViewById(R.id.editText);
s2 = findViewById(R.id.editText2);
s3 = findViewById(R.id.editText3);
a1 = findViewById(R.id.button);
a2 = findViewById(R.id.button2);
a3 = findViewById(R.id.button3);
a4 = findViewById(R.id.button4);
a5 = findViewById(R.id.button5);
a6 =findViewById(R.id.button6);
a7 = findViewById(R.id.button7);
a8 = findViewById(R.id.button8);
a9 = findViewById(R.id.button9);
a1.setOnClickListener(select);
a2.setOnClickListener(confirm);
a3.setOnClickListener(shell);
a4.setOnClickListener(linear);
a5.setOnClickListener(binary);
a6.setOnClickListener(blocking);
a7.setOnClickListener(hash);
a8.setOnClickListener(fibonacci);
a9.setOnClickListener(insert);
}
View.OnClickListener select = new View.OnClickListener() {
@Override
public void onClick(View v) {
Sorting sort=new Sorting(d);
s2.setText(sort.SelectionSort());
}
};
View.OnClickListener confirm = new View.OnClickListener() {
@Override
public void onClick(View v) {
String b = s1.getText().toString();
String[] c = b.split(" ");
d = new int[c.length];
for(int i=0;i<c.length;i++)
{
int num = Integer.parseInt(c[i]);
d[i] = num;
}
}
};
View.OnClickListener shell = new View.OnClickListener() {
@Override
public void onClick(View v) {
Sorting sort=new Sorting(d);
s2.setText(sort.ShellSort());
}
};
View.OnClickListener linear = new View.OnClickListener() {
@Override
public void onClick(View v) {
String b1 =s3.getText().toString();
int target = Integer.parseInt(b1);
Searching search=new Searching(d);
int position=search.LinearSearch(d,target);
String result;
if(position==-1)result="Fail!";
else result="Succeed!"+"The position is:"+position;
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
}
};
View.OnClickListener binary = new View.OnClickListener() {
@Override
public void onClick(View v) {
String b1 =s3.getText().toString();
int target = Integer.parseInt(b1);
Searching search=new Searching(d);
int position=search.BinarySearch(d,0,d.length-1,target,0);
String result;
if(position==-1)result="Fail!";
else result="Succeed!"+"The position is:"+position;
Toast.makeText(getApplicationContext(), new Sorting(d).SelectionSort(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
}
};
View.OnClickListener insert = new View.OnClickListener() {
@Override
public void onClick(View v) {
String b1 =s3.getText().toString();
int target = Integer.parseInt(b1);
Searching search=new Searching(d);
int position=search.InsertSearch(d,0,d.length,target,0);
String result;
if(position==-1)result="Fail!";
else result="Succeed!"+"The position is:"+position;
Toast.makeText(getApplicationContext(), new Sorting(d).SelectionSort(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
}
};
View.OnClickListener blocking = new View.OnClickListener() {
@Override
public void onClick(View v) {
String b1 =s3.getText().toString();
int target = Integer.parseInt(b1);
Searching search=new Searching(d);
int position=search.BlockSearch(d,target);
String result;
if(position==-1)result="Fail!";
else result="Succeed!"+"The position is:"+position;
Toast.makeText(getApplicationContext(), new Sorting(d).SelectionSort(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
}
};
View.OnClickListener hash = new View.OnClickListener() {
@Override
public void onClick(View v) {
String b1 =s3.getText().toString();
int target = Integer.parseInt(b1);
Searching search=new Searching(d);
int position=search.HashSearch(d,target);
String result;
if(position==-1)result="Fail!";
else result="Succeed!"+"The position is:"+position;
Toast.makeText(getApplicationContext(), search.HashPrint(d), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
}
};
View.OnClickListener fibonacci = new View.OnClickListener() {
@Override
public void onClick(View v) {
String b1 =s3.getText().toString();
int target = Integer.parseInt(b1);
Searching search=new Searching(d);
int position=search.FibonacciSearch(d,d.length,target);
String result;
if(position==-1)result="Fail!";
else result="Succeed!"+"The position is:"+position;
Toast.makeText(getApplicationContext(), new Sorting(d).SelectionSort(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="INSERT"
app:layout_constraintBottom_toTopOf="@+id/button6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.904"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.984" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="64dp"
android:text="Input"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.454"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="132dp"
android:text="selection"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.268"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:text="confirm"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="88dp"
android:layout_marginLeft="88dp"
android:layout_marginTop="132dp"
android:text="shell"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.629"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="220dp"
android:text="Result"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.021"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="76dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.474"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.362" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_marginBottom="44dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.557" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:text="@android:string/search_go"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.01"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/editText3"
app:layout_constraintVertical_bias="0.026" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="blocking"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.912"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.742" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="160dp"
android:text="Fibonacci"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:text="binary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.606" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="112dp"
android:text="order"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.024"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.565" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="160dp"
android:text="hash"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
线性查找
二叉查找
插值查找
分块查找
哈希查找
3.心得体会
命令行基本上已经忘得差不多了,经过这次学习又加深了印象。