标签:.com man xtend spl 依次 sele 代码 使用方法 删除
Searching
和Sorting类
,并在类中实现LinearSearch(教材P162),SelectionSort方法(P169),最后完成测试。Sorting.java Searching.java
放入cn.edu.besti.cs1723.(姓名首字母+四位学号)包中(例如:cn.edu.besti.cs1723.G2301)Searching类
——LinearSearchtarget
进行比较,当比较的结果为true时,将true赋值给found输出,否则返回false。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;
}
Sorting类
——SelectionSort命令 | 用途 |
---|---|
cd | 切换目录,其中cd.或cd~是回到上一级目录,cd..是返回根目录 |
ls | 列出文件夹内容 |
mkdir | 创建目录 |
mv file1 file2 | 移动file1到file2 |
rm | 删除文件 |
man command | 查看命令的参考手册 |
vi file.java | 创建名为file的java文件 |
javac file.java | 创建名为file的class文件 |
java file | 运行java文件 |
操作 | 用途 |
---|---|
i | 进入插入模式 |
wq | 保存并退出 |
q | 强制退出 |
// 二分查找(书上的方法)
public static <T extends Comparable<T>> boolean binarySearch(T[] data, int min, int max, T target)
{
boolean found = false;
int midpoint = (min + max) / 2; // determine the midpoint
if (data[midpoint].compareTo(target) == 0) {
found = true;
} else if (data[midpoint].compareTo(target) > 0)
{
if (min <= midpoint - 1) {
found = binarySearch(data, min, midpoint - 1, target);
}
}
else if (midpoint + 1 <= max) {
found = binarySearch(data, midpoint + 1, max, target);
}
return found;
}
// 二分查找(博客中的方法)
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;
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 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;
// 寻找k值,k值要求k值能使得F[k]-1恰好大于或等于n
int k = 0;
while(n > Fibonacci(k) - 1)
{
k++;
}
//因为无法直接对原数组增加长度,所以定义一个新的数组
//采用System.arraycopy()进行数组间的赋值
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;
// 在temp的左边进行查找
if(temp[mid] > key) {
high = mid - 1;
k = k - 1;
}
// 在temp的右边进行查找
else if(temp[mid] < key) {
low = mid + 1;
k = k - 2;
}else {
if(mid <= n) {
return mid;
}
//当mid位于新增的数组中时,返回n
else {
return n;
}
}
}
return 0;
}
public static void ShellSort(int[] data, int length){
int i = length;
while (i > 1){
// 动态定义间隔
i = (i + 1) / 2;
for (int j = 0;j < length - i;j++){
// 当对应比较的两个数中前者大于后者时进行交换
if (data[j + i] < data[j]){
int temp = data[j + i];
data[j + i] = data[j];
data[j] = temp;
}
}
}
}
public void testBinaryTreeSort(){
String list[] = {"1","3","6","13","17","20","23","81","99","2303"};
LinkedBinarySearchTree tree = new LinkedBinarySearchTree();
tree.addElement(2303);
tree.addElement(99);
tree.addElement(81);
tree.addElement(23);
tree.addElement(20);
tree.addElement(17);
tree.addElement(13);
tree.addElement(6);
tree.addElement(3);
tree.addElement(1);
int a = tree.size();
String[] b = new String[a];
for (int i = 0;i < a;i++){
b[i] = String.valueOf(tree.findMin());
tree.removeMin();
}
// 正常测试+边界测试
assertEquals(b[0],list[0]);
}
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
Create Test
。Junit4
然后点击OK。extend TestCase
,在尝试之后发现真的没有透印了,就很开心地解决了这个问题并收获了结对伙伴的嘲笑。20172303 2018-2019-1 《程序设计与数据结构》实验三报告
标签:.com man xtend spl 依次 sele 代码 使用方法 删除
原文地址:https://www.cnblogs.com/PFrame/p/10074360.html