20,182,311 2019-2020-1 "Object-oriented programming and data structures" Experiment 7 report

20,182,311 2019-2020-1 "Object-oriented programming and data structures" Experiment 7 report

Course: "Programming and Data Structures"
Class: 1823
Name: Cold punching
Student ID: 20182311
experiments Teacher: Johnny
experiment Date: October 30, 2019
Compulsory / Elective: Compulsory

1. Experimental content

  • Searching and Sorting define a class, and implements linearSearch, SelectionSort class methods, and finally to complete the test. Test requires less than 10, the design submitted test cases (normal, abnormal, boundary, positive sequence, reverse), the data to be included with the embodiment after their student number four operation results submitted FIG.
  • Refactor your code into the Sorting.java Searching.java cn.edu.besti.cs1823 (initials + four-digit student number) package (for example: cn.edu.besti.cs1823.G2301)., The test Code release test packages recompile, run the code, submit to compile, run shots (IDEA, a command line)
  • Reference http://www.cnblogs.com/maybe2030/p/4715035.html, learn a variety of search algorithm search algorithm in adding Searching and test run results submitted screenshots.
  • Sorting method talked about the realization of supplementary lesson: Hill sort, heap sort, binary tree sorting (at least 3), algorithm (normal, abnormal, boundary) test run results achieved submit screenshots (If you write more sorting algorithm, even if three of the sort program is defective, you can get out as appropriate)
  • Android app to achieve a variety of sorting algorithms to find and test results submitted to run shots, pushing code to the code cloud (elected to do, extra points).

2. Experimental procedure and results

  • Searching and Sorting define a class, and implements linearSearch, SelectionSort class methods, and finally to complete the test.
  • Sorting method talked about the realization of supplementary lesson: Hill sort, heap sort, binary tree sorting
  • Reference , and learn a variety of search algorithms supplemented Searching in
    • Linear search: iterate and find the standard array element is returned, -1 is not found;
    • Binary search:
      • Binary implemented: using first and last mark the beginning and end of all the elements, mid fetch (first + last) / 2, when the cycle of first <= last, to achieve each lookup value points.
      • Recursive: substantially following code, increasing the recursive method requires two parameters is used to mark where recursive step.
      public static Comparable BinarySearch2(Comparable[] data,Comparable target,int first,int last){
              int mid=(first+last)/2;
              if(data[mid].compareTo(target)==0){
                  return mid+1;
              }
              if(data[mid].compareTo(target)>0)
                   return BinarySearch2(data,target,first,mid-1);
              if(data[mid].compareTo(target)>0)
                  return BinarySearch2(data,target,mid+1,last);
              return false;
      }
      • Interpolation Find: a modified version of the binary search method, except that the following modifications mid way values:
       mid=first+(target-data[first])/(data[last]-data[first])*(last-first);
      • Hash lookup: determining access the elements by index modulo arithmetic.
      • Fibonacci Find: Get Fibonacci number, then use the subscript golden section algorithm determines access elements.
      • Insertion sort: using a double loops and is inserted, the entire table gradually from the front backwards ordered.
      • Binary insertion sort: insertion sort improvements, changed binary linear explore explore
      • Quick Sort: root element of any division form and then recursively sub-sort the table of two, to complete an entire set of sorting.
      • Merge sort: the first recursive table into sub-tables each containing only one element, then sequentially combined sub-table.
      • Select the bubble sort:



  • Refactor your code and recompile the package into cn.edu.besti.cs1823.Lc20182311 submit compile IDEA, a command line run shot.

  • Android app to achieve a variety of sorting algorithms to find and test the
    layout of Android really a headache, quick to cover.

3. Experimental problems encountered in the process and settlement process

  • Question 1: Use compareto compare two comparable array elements error
  • Problem 1 Solution: Use compareTo method to compare two objects, first of all to class inheritance comprable interfaces, it did. However, the following code will be given, because only the first line declares an array of an array can not be used without instantiating compareTo method.
Comprare[] a=new Comprare[4];
a[1].comprareTo(a[2]);
  • Question 2: Why arrays.sort () method must implement the Comparable interface?
  • 问题2解决方案:Arrays.sort(Object[] objs)方法的排序机制是,在内部把Object数组里面强制转换为Comparable类型,再调用compareTo()方法进行排序。调用CompareTo()方法需要实现Comparable接口。

4.参考资料

Guess you like

Origin www.cnblogs.com/lengchong/p/11870639.html