zoukankan      html  css  js  c++  java
  • 20162316刘诚昊 10月9日“查找课堂测试”

    20162316刘诚昊 2017-2018-2 《Java程序设计》10月9日“查找课堂测试”

    测试要求:

    1 用JDB或IDEA单步跟踪在下列数据中(3 8 12 34 54 84 91 110)查找45和54的过程,对比使用顺序查找和二分查找的执行过程
    2提交测试找到或找不到那一步的截图,要全屏,包含自己的学号信息
    3课下把代码推送到代码托管平台

    过程:

    1.书上的代码

    public class Searching {
        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;
                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;
        }
    }
    
    

    2.自己写一个简单的main方法并调用上面的类

    public class Week5_LinearSearch {
        public static void main(String[] args) {
            Comparable[] num = {3, 8, 12, 34, 54, 84, 91, 110, 2316};
            Searching qw = new Searching();
            System.out.println(qw.linearSearch(num,54));
            System.out.println(qw.linearSearch(num,45));
            System.out.println(qw.binarySearch(num,54));
            System.out.println(qw.binarySearch(num,45));
        }
    }
    
    

    3.设定好节点以后用debug单步追踪得到

  • 相关阅读:
    POJ 1659 Frogs' Neighborhood (贪心)
    HDU 2544 最短路 (Floyd)
    CodeForces 632C Grandma Laura and Apples (模拟)
    CodeForces 731F Video Cards (数论+暴力)
    CodeForces 731C Socks (DFS或并查集)
    CodeForces 731B Coupons and Discounts (水题模拟)
    CodeForces 731A Night at the Museum (水题)
    UVaLive 6834 Shopping (贪心)
    zzuli 1484 继续双线
    zzuli 1875多线DP
  • 原文地址:https://www.cnblogs.com/ignor/p/7642095.html
Copyright © 2011-2022 走看看