2017年10月9日 课堂查找测试补写博客
2017-11-10 22:17
190 查看
测试截图
这个课堂测试并不难,测试成功后拼图的时候慢了一些,没来得及提交。这个测试的关键在于——明白查找的方法的参数类型为Comparable类型,所以在声明数组时创建为Comaparable类型的即可
测试代码
/** * Created by 齐力锋 on 2017/10/9. */ public class SearchTest { public static void main (String args[]) { Comparable[] i = new Comparable[100]; i[0] = 3 ; i[1] = 8; i[2] = 12 ; i[3] = 34 ; i[4] = 54; i[5] = 84; i[6] = 91; i[7] = 110; i[8] = 23; i[9] = 26; Searching.linearSearch(i,54); System.out.println("排序成功" + Searching.linearSearch(i,54) ); } } // Searching.binarySearch(i,45); // Searching.binarySearch(i,54);
查找的另一个测试代码
/** * Created by 齐力锋 on 2017/10/9. */ //******************************************************************** // Sorting.java Java Foundations // // Contains various sort algorithms that operate on an array of // Comparable objects. //******************************************************************** public class Sorting { //----------------------------------------------------------------- // Sorts the specified array of integers using the selection // sort algorithm. //----------------------------------------------------------------- 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); } } //----------------------------------------------------------------- // Swaps two elements in the specified array. //----------------------------------------------------------------- private static void swap(Comparable[] data, int index1, int index2) { Comparable temp = data[index1]; data[index1] = data[index2]; data[index2] = temp; } //----------------------------------------------------------------- // Sorts the specified array of objects using an insertion // sort algorithm. //----------------------------------------------------------------- public static void insertionSort(Comparable[] data) { for (int index = 1; index < data.length; index++) { Comparable key = data[index]; int position = index; // Shift larger values to the right while (position > 0 && data[position - 1].compareTo(key) > 0) { data[position] = data[position - 1]; position--; } data[position] = key; } } //----------------------------------------------------------------- // Sorts the specified array of objects using a bubble sort // algorithm. //----------------------------------------------------------------- public static void bubbleSort(Comparable[] data) { int position, scan; 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); } } //----------------------------------------------------------------- // Sorts the specified array of objects using the quick sort // algorithm. //----------------------------------------------------------------- public static void quickSort(Comparable[] data, int min, int max) { int pivot; if (min < max) { pivot = partition(data, min, max); // make partitions quickSort(data, min, pivot - 1); // sort left partition quickSort(data, pivot + 1, max); // sort right partition } } //----------------------------------------------------------------- // Creates the partitions needed for quick sort. //----------------------------------------------------------------- private static int partition(Comparable[] data, int min, int max) { // Use first element as the partition value Comparable partitionValue = data[min]; int left = min; int right = max; while (left < right) { // Search for an element that is > the partition element while (data[left].compareTo(partitionValue) <= 0 && left < right) left++; // Search for an element that is < the partitionelement while (data[right].compareTo(partitionValue) > 0) right--; if (left < right) swap(data, left, right); } // Move the partition element to its final position swap(data, min, right); return right; } //----------------------------------------------------------------- // Sorts the specified array of objects using the merge sort // algorithm. //----------------------------------------------------------------- public static void mergeSort(Comparable[] data, int min, int max) { if (min < max) { int mid = (min + max) / 2; mergeSort(data, min, mid); mergeSort(data, mid + 1, max); merge(data, min, mid, max); } } //----------------------------------------------------------------- // Sorts the specified array of objects using the merge sort // algorithm. //----------------------------------------------------------------- public static void merge(Comparable[] data, int first, int mid, int last) { Comparable[] temp = new Comparable[data.length]; int first1 = first, last1 = mid; // endpoints of first subarray int first2 = mid + 1, last2 = last; // endpoints of second subarray int index = first1; // next index open in temp array // Copy smaller item from each subarray into temp until one // of the subarrays is exhausted while (first1 <= last1 && first2 <= last2) { if (data[first1].compareTo(data[first2]) < 0) { temp[index] = data[first1]; first1++; } else { temp[index] = data[first2]; first2++; } index++; } // Copy remaining elements from first subarray, if any while (first1 <= last1) { temp[index] = data[first1]; first1++; index++; } // Copy remaining elements from second subarray, if any while (first2 <= last2) { temp[index] = data[first2]; first2++; index++; } // Copy merged data into original array for (index = first; index <= last; index++) data[index] = temp[index]; } public static void main(String args[]) { Comparable[] number = new Comparable[8]; number[0]= 3; number[1] = 8; number[2]=12; number[3]=34; number[4]= 54; number[5] = 84; number[6] = 91 ; number[7] = 110; Sorting.quickSort(number,0,7); for (Comparable I :number) System.out.println(I); } }
相关文章推荐
- 20162312张家铖 查找课堂测试补分博客
- hash课堂测试补分博客
- 第六周作业总结(内含查找排序课堂测试及课下作业报告)
- 20162316刘诚昊 10月9日“查找课堂测试”
- 20162312张家铖 10.9查找课堂测试
- 转载一篇关于软件测试的博客,方便以后查找。
- 20162313苑洪铭 查找练习课堂测试
- 迪杰斯特拉课堂测试补写博客
- 测试博客链接
- 2017-2018-1 20155208 课堂测试(ch06)(补做)
- 创建博客-使用HTTPie测试Web服务
- Mongodb 之insert瞬时完成,测试数据---飞天博客
- 测试下博客
- 用node.js建博客(五) - 用vows以BDD方式测试程序
- 课堂测试-单元测试(比较大小)
- 测试使用markdonw写博客
- 博客开通,首发文章测试一把:)
- 测试在csdn博客中插入代码
- 测试一下我的博客
- 我就是测试测试下开源中国的博客子系统是如何设计的!