1.定义一个Searching和Sorting类,并在类中实现linearSearch,SelectionSort方法,最后完成测试。
要求不少于10个测试用例,提交测试用例设计情况(正常,异常,边界,正序,逆序),用例数据中要包含自己学号的后四位
提交运行结果图。
程序代码
SearchingTest
public class SearchingTest extends TestCase {
Searching a = new Searching();
Sorting b = new Sorting();
@Test
public void testSearching()
{
int[] t1 = {19,14,23,1,68,20,84,27,55,11,10,79};
assertEquals(false,a.linearSearch(t1,9,12)); //正常
assertEquals(true,a.linearSearch(t1,1,12)); //正常
assertEquals("Abnormal",a.linearSearch(t1,1,18)); //异常
assertEquals(true,a.linearSearch(t1,79,12)); //边界
int[] t2 = {5,2,7,43,21,65,23};
assertEquals(true,a.linearSearch(t2,2,7));
assertEquals(false,a.linearSearch(t2,42314,7));
assertEquals("Abnormal",a.linearSearch(t2,1,18));
assertEquals(true,a.linearSearch(t2,5,7));
int[] t3 = {1,24,5,5,2,2,45,56};
assertEquals(true,a.linearSearch(t3,24,8));
assertEquals(false,a.linearSearch(t3,0,8));
assertEquals("Abnormal",a.linearSearch(t3,1,18));
assertEquals(true,a.linearSearch(t3,56,8));
int[] t4 = {2,423,42,435,32,45,4,43,2};
assertEquals(true,a.linearSearch(t4,4,9));
assertEquals(false,a.linearSearch(t4,123,9));
assertEquals("Abnormal",a.linearSearch(t4,1,18));
assertEquals(true,a.linearSearch(t4,2,9));
int[] t5 = {967,65,5687,56,332,46,23};
assertEquals(true,a.linearSearch(t5,332,7));
assertEquals(false,a.linearSearch(t5,12,7));
assertEquals("Abnormal",a.linearSearch(t5,1,18));
assertEquals(true,a.linearSearch(t5,967,7));
}
@Test
public void testSorting(){
Comparable[] t6 = {1,2,5,88,3,14};
b.selectionSort(t6);
assertEquals("1 2 3 5 14 88 ",b.print(t6)); //正常乱序排序
Comparable[] t7 = {1,4,8,2,3,10};
b.selectionSort(t7);
assertEquals("1 2 3 4 8 10 ",b.print(t7)); //正常乱序排序
Comparable[] t8 = {1,2,6,8,9,14};
b.selectionSort(t8);
assertEquals("1 2 6 8 9 14 ",b.print(t8)); //正序排序
Comparable[] t9 = {34,27,24,13,10};
b.selectionSort(t9);
assertEquals("10 13 24 27 34 ",b.print(t9)); //逆序排序
Comparable[] t10 = {100,98,92,11};
b.selectionSort(t10);
assertEquals("11 92 98 100 ",b.print(t10)); //逆序排序
Comparable[] t11 = {20,19,23,14};
b.selectionSort(t11);
assertEquals("14 19 20 23 ",b.print(t11)); //学号测试用例
}
}
Searching
public class Searching
{
public static Comparable linearSearch (int[] arr,int target,int length)
{
int i=0;
int a = target;
if(length>arr.length)
return "Abnormal";
else
{
while(arr[i]!=target)
{
i++;
if(i==arr.length)
break;
}
return i==arr.length?false:true;
}
}
public int blocking(int[] arr, int target) {
}
public boolean print(int[] arr) {
}
}
Sorting
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 String print(Comparable[] data){
String result = "";
for(int i=0;i<data.length;i++)
result += ""+data[i]+" ";
return result;
}
}
运行截图
2.重构你的代码
把Sorting.java Searching.java放入 cn.edu.besti.cs1823.(姓名首字母+四位学号) 包中(例如:cn.edu.besti.cs1823.G2301)
把测试代码放test包中
重新编译,运行代码,提交编译,运行的截图(IDEA,命令行两种)
参考http://www.cnblogs.com/maybe2030/p/4715035.html ,学习各种查找算法并##在Searching中补充查找算法并测试
提交运行结果截图
运行截图
参考http://www.cnblogs.com/maybe2030/p/4715035.html ,学习各种查找算法并##在Searching中补充查找算法并测试
提交运行结果截图
程序代码
Linked
public class Linked {
public int a;
public Linked next = null;
public Linked temp,top;
public int element;
public Linked(int a) {
this.a = a;
}
public int getnum(){
//String b = Integer.toString(a);
return a;
}
public void setNext(Linked next){
this.next=next;
}
public Linked getNext() {
return next;
}
public void insert(Linked Head, int a, Linked insertnode){
Linked temp = Head;
if(a==0){
insertnode.setNext(temp);
//Head = insertnode;
}
else {
for(int i=0;i<a;i++)
{
top = temp;
temp = temp.getNext();
}
top.setNext(insertnode);
insertnode.setNext(temp);
temp=top;
}
}
public void delete(Linked Head, int a){
Linked temp = Head;
int i=0;
if(a==0)
{
;
}
else {
while(i!=a-1){
temp = temp.getNext();
i++;
}
top = temp;
temp=temp.getNext();
temp=temp.getNext();
top.setNext(temp);
temp=top;
}
}
public void sort(Linked Head, int count){
Linked temp = Head;
Linked c,d;
for(int i = 0;i<count-1;i++)
{
c = temp;
d = c.getNext();
for(int j =0;j<count-i-1;j++)
{
if((c.getElement())<(d.getElement()))
{
int t;
t = c.getElement();
c.setElement(d.getElement());
d.setElement(t);
}
c=c.getNext();
d=d.getNext();
}
}
}
public int getElement()
{
return a;
}
public void setElement (int elem)
{
a=elem;
}
public String print(Linked head){
String result="元素:
"+head.getnum()+"
";
while(head.getNext()!=null){
result +=(head.getNext()).getnum()+"
";
head=head.getNext();
}
return result;
}
}
BinaryTree
public class BinaryTree {
Compareable tree = new Compareable(-1);
Compareable head = new Compareable(-1);
int[] a = new int[12];
public BinaryTree()
{
head.setNext(tree);
}
public void s(int[] b)
{
tree.setI(b[0]);
for(int i=1;i<b.length;i++)
{
Compareable c = new Compareable(b[i]);
tree = head.getNext();
while (tree.geti()!=b[i])
{
if(tree.geti()<b[i]&&(tree.getNext()!=null))
{
tree = tree.getNext();
}
else if(tree.geti()<b[i]&&tree.getNext()==null)
{
tree.setNext(c);
tree=c;
}
else if(tree.geti()>b[i]&&tree.getSecondnext()!=null)
tree = tree.getSecondnext();
else if(tree.geti()>b[i]&&tree.getSecondnext()==null)
{
tree.setSecondnext(c);
tree = c;
}
}
}
}
public Compareable get()
{
return head.getNext();
}
}
Compareable
class Compareable {
private int i=-1;
public Compareable next=null;
public Compareable secondnext=null;
public Compareable(int i)
{
this.i=i;
}
public int compareto(Compareable a)
{
int result = i-a.geti();
return result;
}
public int geti()
{
return i;
}
public void setI(int i)
{
this.i=i;
}
@Override
public String toString()
{
String string = i+"";
return string;
}
public void setNext(Compareable a)
{
next = a;
}
public void setSecondnext(Compareable b)
{
secondnext = b;
}
public Compareable getNext()
{
return next;
}
public Compareable getSecondnext()
{
return secondnext;
}
}
Searhing
public class Searching
{
public boolean order(int[] arr,int target){
int i=0;
int a = target;
while(arr[i]!=target)
{
i++;
if(i==arr.length)
break;
}
return i==arr.length?false:true;
}
public void sort(int arr[]){
for(int i =1;i<arr.length;i++) {
for(int j=0;j<arr.length-i;j++) {
if(arr[j]>arr[j+1]) {
int temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
public boolean binary(int[] arr,int min,int max,int mid,int target){
boolean found = false;
mid = (min + max) / 2;
int midd = mid;
if(arr[midd]==target)
found = true;
else if (arr[midd]!=target)
{
if(target<arr[midd])
{
max = midd-1;
midd--;
found = binary(arr,min,max,midd,target);
}
else if(target>arr[midd])
{
min = midd+1;
midd++;
found = binary(arr,min,max,midd,target);
}
}
return found;
}
public int binaryshow(int[] arr,int min,int max,int mid,int target){
int found = 0;
mid = (min + max) / 2;
int midd = mid;
if(arr[midd]==target)
found = arr[midd];
else if (arr[midd]!=target)
{
if(target<arr[midd])
{
max = midd-1;
midd--;
found = binaryshow(arr,min,max,midd,target);
}
else if(target>arr[midd])
{
min = midd+1;
midd++;
found = binaryshow(arr,min,max,midd,target);
}
}
return found;
}
public int[] hash(int[] arr){
int[] arr1 = {0,0,0,0,0,0,0,0,0,0,0,0};
for(int i=0;i<arr.length;i++)
{
if(arr1[arr[i]%11] == 0)
arr1[arr[i]%11] = arr[i];
else
{
for(int j=2;j<arr.length;j++)
if(arr1[j-1] == 0)
{
arr1[j-1] = arr[i];
break;
}
}
}
return arr1;
}
public int hashsearch(int[] result,int target){
int k = target%11,i,re = 0;
if(result[k]==target)
re = result[k];
else
{
for(i=k;k<result.length;k++)
{
if(result[k]==target)
{
re = result[k];
break;
}
}
}
return re;
}
public Linked[] linkedhash(Linked[] linked){
Linked[] arr1 = new Linked[12];
int i;
for(i=0;i<12;i++)
arr1[i] = new Linked(0);
for(i=0;i<linked.length;i++)
{
if((arr1[linked[i].getnum()%11]).getnum() == 0)
arr1[linked[i].getnum()%11] = linked[i];
else
{
arr1[linked[i].getnum()%11].setNext(linked[i]);
}
}
return arr1;
}
public int linkedsearch(Linked[] re1, int target){
int k = target%11,i,re = 0;
if(re1[k].getnum()==target)
re = re1[k].getnum();
else
{
Linked re2 = re1[k].getNext();
//re2 = new Linked(0);
if(re2.getnum()==target)
re = re2.getnum();
}
return re;
}
public static boolean FibonacciSearch(int[] table, int keyWord) {
//确定需要的斐波那契数
int i = 0;
while (getFibonacci(i) - 1 == table.length) {
i++;
}
//开始查找
int low = 0;
int height = table.length - 1;
while (low <= height) {
int mid = low + getFibonacci(i - 1);
if (table[mid] == keyWord) {
return true;
} else if (table[mid] > keyWord) {
height = mid - 1;
i--;
} else if (table[mid] < keyWord) {
low = mid + 1;
i -= 2;
}
}
return false;
}
public static int getFibonacci(int n) {
int res = 0;
if (n == 0) {
res = 0;
} else if (n == 1) {
res = 1;
} else {
int first = 0;
int second = 1;
for (int i = 2; i <= n; i++) {
res = first + second;
first = second;
second = res;
}
}
return res;
}
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 a[mid];
if (a[mid] > value)
return InsertionSearch(a, value, low, mid - 1);
else
return InsertionSearch(a, value, mid + 1, high);
}
public static int blocking(int[] arr,int target){
int[] ar1 = new int[arr.length];
int[] ar2 = new int[arr.length];
int[] ar3 = new int[arr.length];
int i=0,j=0,k=0,l=0;
int result = 0;
for(i=0;i<arr.length;i++)
{
if(0<=arr[i]&&arr[i]<=20)
{
ar1[j] = arr[i];
j++;
}
else if (20<arr[i]&&arr[i]<=60)
{
ar2[k] = arr[i];
k++;
}
else
{
ar3[l] = arr[i];
l++;
}
}
i=0;
if(0<=target&&target<=20)
{
for(i=0;i<ar1.length;i++)
if(ar1[i]==target)
{
result = ar1[i];
break;
}
}
else if (20<target&&target<=60)
{
for(i=0;i<ar2.length;i++)
if(ar2[i]==target)
{
result = ar2[i];
break;
}
}
else
{
for(i=0;i<ar3.length;i++)
if(ar3[i]==target)
{
result = ar3[i];
break;
}
}
return result;
}
public static void ShellSort(int[] data)
{
int i= 0, temp = 0, j = 2;
for (int incr = data.length / j; incr > 0; incr /= j)
{
for (int x = incr; x < data.length; x++)
{
temp = (int) data[x];
for (i = x - incr; i >= 0; i -= incr)
{
if (temp < (int) data[i])
data[i + incr] = data[i];
else
break;
}
data[i + incr] = temp;
}
}
}
public String print(int[] arr){
String result = "";
for(int i=0;i<arr.length;i++)
result += ""+arr[i]+" ";
return result;
}
}
treesearching
public class treeearching {
public static boolean erchashu(Compareable tree, Compareable target)
{
while (tree!=null)
{
if(tree.geti()<target.geti())
{
tree=tree.next;
}
else if (tree.geti()>target.geti())
{
tree = tree.secondnext;
}
else if(tree.geti()==target.geti())
return true;
}
return false;
}
}
运行截图
补充实现课上讲过的排序方法:希尔排序,堆排序,二叉树排序等(至少3个)
测试实现的算法(正常,异常,边界)
提交运行结果截图(如果编写多个排序算法,即使其中三个排序程序有瑕疵,也可以酌情得满分)
程序代码
Sort
public class Sort {
private static int a[];
private static int size;
private static String list="";
//希尔排序,正序
public static <T>
String shellSort_positive(int[] a){
int temp = a.length / 2;
int first, last;
while (temp > 0){
for (int i = 0; i + temp <= a.length - 1; i++){
first = i;
last = i + temp;
if (a[first] > a[last]){
int temp2 = a[first];
a[first] = a[last];
a[last] = temp2;
}
}
temp = temp / 2;
}
String result = "";
for (int i = 0; i < a.length; i++){
result = result + a[i] + " ";
}
return result;
}
//逆序
public static <T>
String shellSort_inverse(int[] a){
int temp = a.length / 2;
int first, last;
while (temp > 0){
for (int i = 0; i + temp <= a.length - 1; i++){
first = i;
last = i + temp;
if (a[first] < a[last]){
int temp2 = a[first];
a[first] = a[last];
a[last] = temp2;
}
}
temp = temp / 2;
}
String result = "";
for (int i = 0; i < a.length; i++){
result = result + a[i] + " ";
}
return result;
}
}
import junit.framework.TestCase;
import org.testng.annotations.Test;
public class SortTest extends TestCase { //t1—t5为正序检测,t6-t10为逆序检测
String t1 = "1 2 8 1000 2314 ", t2 = "1 23 2314 ", t3 = "2 6 2314 ",
t4 = "1 14 2314 8000 ", t5 = "2 14 23 2314 ", t6 = "2314 23 14 8 ",
t7 = "2314 1000 8 ", t8 = "2314 10 5 4 ", t9 = "8080 2314 22 2 ", t10 = "2314 66 9 8 ";
@Test
public void test1(){
int[] t = {2314,1,8,1000,2};
assertEquals(t1, Sort.shellSort_positive(t));
//kkk
}
@Test
public void test2(){
int[] t = {1,2314,23};
assertEquals(t2, Sort.shellSort_positive(t));
}
@Test
public void test3(){
int[] t = {2,6,2314};
assertEquals(t3, Sort.shellSort_positive(t));
}
@Test
public void test4(){
int[] t = {14,1,8000,2314};
assertEquals(t4, Sort.shellSort_positive(t));
}
@Test
public void test5(){
int[] t = {2,2314,23,14};
assertEquals(t5, Sort.shellSort_positive(t));
}
@Test
public void test6(){
int[] t = {23,14,2314,8};
assertEquals(t6, Sort.shellSort_inverse(t));
}
@Test
public void test7(){
int[] t = {1000,2314,8};
assertEquals(t7, Sort.shellSort_inverse(t));
}
@Test
public void test8(){
int[] t = {2314,10,5,4};
assertEquals(t8, Sort.shellSort_inverse(t));
}
@Test
public void test9(){
int[] t = {22,2314,8080,2};
assertEquals(t9, Sort.shellSort_inverse(t));
}
@Test
public void test10(){
int[] t = {2314,66,9,8};
assertEquals(t10, Sort.shellSort_inverse(t));
}
}