20172327 2018-2019-1 "program design and data structure" Fifth week study summary

Source: Internet
Author: User
Tags comparable

20172327 2018-2019-1 "program design and data structure" Fifth week study summary textbook Study content summary nineth Chapter sort and find

Find


1. Find: Find the specified element in a project group or determine whether it exists. The project group is called a lookup pool.

2. Common Find method, linear find.

3. Find the goal: efficiently complete the search, with minimal comparison operations. Finding the number of items in a pool typically defines the size of the problem.

4. Static method (class method): Can be activated by the class name

5. In a method declaration, it can be declared static by using the static modifier.

6. Generic methods: Similar to generic classes, instead of creating classes that reference generic parameters, create a method that references generics.

7. Linear Lookup: Start by comparing each value from the head of the table until the target element is found.

8. A two-point lookup will take advantage of the fact that the pool is already sorted, and each comparison will remove half of the feasible candidates.

9. The linear lookup time complexity is O (n), the binary lookup time complexity is log2n, so when the n value is large, the binary lookup is much faster.

Sort


1. Sort: A set of items is arranged in a specified order based on a standard, ascending or descending order.

2. Efficiency-based sorting algorithms are usually divided into two categories: sequential ordering (using a nested loop to sort n elements, about n^2 times), logarithmic (sorting n elements, usually approximately nlog2n times)

3. Sequential sorting: Select sort, insert sort, bubble sort

4. Logarithmic sort: quick sort, merge sort


5. Select the sorting method:

    • Initially finds the smallest (large) element in the sequence, placing it at the beginning of the sequence as the sorted sequence, and then continues to look for the smallest (large) element from the remaining unsorted elements and puts it at the end of the sorted sequence. And so on until all elements are sorted.
    • The Selectionsort method implements two loops, the outer loop controls the next minimum value stored in that position, and the inner loop finds the minimum value of the remaining list by scanning all positions that are greater than or equal to the outer loop of the specified index.

    • Sequence {8, 5, 2, 6, 9, 3, 1, 4, 0, 7} for the implementation of the selection sort:

    • Macro chart:


6. Insert Sort method:

    • The insertion sort is implemented on an implementation, usually in the order of In-place (that is, the ordering of extra space using only O (1), so that in the backward-forward scanning process, the ordered elements need to be moved back and forth gradually, providing the insertion space for the newest elements.
    • The Insertionsort method uses two loops to sort an array of objects, the outer loop controls the index of the next inserted value in the array, and the inner loop compares the values previously inserted and stored at a smaller index.
    • The Insert Sort algorithm works as follows:
      1. Starting with the first element, the element can be thought to have been sorted
      2. Take the next element and scan from the back forward in the sequence of ordered elements
      3. If the element (sorted) is greater than the new element, move the element to the next position
      4. Repeat step 3 until the sorted element is found to be less than or equal to the position of the new element
      5. After inserting a new element into the location
      6. Repeat steps 2~5
    • Sequence {6, 5, 3, 1, 8, 7, 2, 4} The implementation of the insertion sort is as follows:

    • Macro chart:


7. Bubble Sorting Method:

    • It repeatedly visits the elements to be sorted, compares the adjacent two elements in turn, and, if they are in the wrong order, swaps them until no elements need to be exchanged again, sorting is done. The algorithm is named because the smaller (or larger) element will slowly "float" through the switch to the top of the sequence.
    • The outer for loop in the Bubblesort method represents the n-1 wheel data traversal, and the inner for loop scans the data from beginning to the next, making a pair comparison of the adjacent data if necessary to exchange it.
    • The bubbling sorting algorithm works as follows:
      1. Compare adjacent elements, if the previous one is larger than the latter one, put them in two swap positions.
      2. Do the same work for each pair of adjacent elements, starting from the first pair to the end of the last pair. When this is done, the final element will be the maximum number.
      3. Repeat the above steps for all elements except the last one.
      4. Repeat the above steps each time for fewer elements until there are no pairs of numbers to compare.
      The process for bubbling the sequence {6, 5, 3, 1, 8, 7, 2, 4} is implemented as follows:

Macro:


8. Quick Sort Method:

    • The quick sort algorithm accomplishes sorting the entire list by partitioning the list and then recursively sorting the two partitions.
    • The Quicksort method relies heavily on the partition method, which initially separates the sorting area. The two inner while loops of the partition method are used to find the interchange element that is located in the wrong partition. The first loop scans from the left to the right to look for elements larger than the partition element, and the second loop scans from the right to the left to look for elements smaller than the partition element.
    • The operation process of the fast sorting algorithm:
      1. Select an element from the sequence as a "datum" (pivot).
      2. Place all elements that are smaller than the base value in front of the datum, and all elements larger than the base value are placed behind the datum (the same number can be on either side), this is called the partitioning (partition) operation.
      3. For each partition recursively steps one to one, the end condition of the recursion is that the size of the sequence is 0 or 1, then the whole has been sequenced.
      Sequence: {1, 3, 4, 2, 8, 9, 8, 7, 5}, the Datum element is 5, and 5 is swapped with the first 8 after a division operation, thus changing two elements 8
      The relative order.
      Macro:


9. Merge Sort method:

    • The merge sort algorithm completes sorting the list by dividing the list recursively into two halves until each sub-list contains an element, and then merges the sub-lists into a sort order.
    • The implementation of merge sort is divided into recursive implementation and non-recursive (iterative) implementation. Recursive implementation of the merge sort is a typical application of divide and conquer strategy in algorithm design, we divide a big problem into small problems and solve them separately, then solve the whole problem with the answer of all the small questions. The merge sort of non-recursive (iterative) implementation is first performed with 22 merges, then 44 merges, then 88 merges, and continues until the entire array is merged.
    • The merge Sort method operates as follows:
      1. Apply the space so that it is the sum of two sorted sequences that are used to store the merged sequence
      2. Set two pointers, the initial position is the starting position of two sorted series
      3. Compare the elements pointed to by two pointers, select a relatively small element into the merge space, and move the pointer to the next position
      4. Repeat step 3 until a pointer reaches the end of the sequence
      5. Copy all remaining elements of another sequence directly to the end of the merge sequence
      Examples of merge sorts for sequence {6, 5, 3, 1, 8, 7, 2, 4} are as follows:

Macro:

Cardinal Sort Method


1. Unify all the values to be compared (positive integers) to the same digit length, and 0 for the shorter number of digits. Then, start with the lowest bit, and then order one at a time. So that from the lowest bit to the highest level, the sequence becomes an ordered sequence.

2. If our disorder is T = [2314, 5428, 373, 2222, 17], then the process of ordering it is shown in the following two.
Cardinal sort Process Diagram-1:

Problems in teaching materials learning and the solving process
    • Question 1: In the static method section, there is an instance variable, I don't quite understand what this means?
    • Problem resolution and Analysis: instance variables: In the declaration of a class, a property is represented by a variable. This variable, called an instance variable, is inside the class declaration but is declared outside of the other member methods of the class. Each object of the class maintains its own copy of an instance variable.

    • Question 2: What is @SuppressWarnings and how to use it.
    • Problem solving and Analysis:
      1. Indicates that the specified compiler warning should be deselected in the annotation element (and all program elements included in the annotation element). Note that the set of warnings that are deselected in a given element is a superset of all warnings that are deselected in the containing element. For example, if you comment on a class to suppress a warning and comment on a method to suppress another warning, both warnings will be deselected in this method.
      2. Depending on the style, the programmer should always use this comment on the innermost nested element, where the use is valid. If you want to suppress a warning in a particular method, you should comment on the method instead of commenting on its class.

Problems in code debugging and the resolution process
    • Question 1: When implementing the Pp9.3sorting class adaptation, I feel confused about the number of loops that merge sort and quick sort, and I don't know how to calculate it.

    • Solution Analysis:
      Through the study of students ' methods, I think there are two ways to solve, one is directly
        public static <T extends Comparable<T>>    void mergeSort(T[] data)    {        times = times2 = 0;        long startTime=System.nanoTime();        mergeSort(data, 0, data.length - 1);        long endTime = System.nanoTime();        long temp = times + times2;        System.out.println("程序运行时间:" + (endTime - startTime) + "ns");        System.out.println("比较次数为:"+ temp);    }    private static int times;    private static <T extends Comparable<T>>    void mergeSort(T[] data, int min, int max)    {        if (min < max)        {            times++;            int mid = (min + max) / 2;            mergeSort(data, min, mid);            mergeSort(data, mid+1, max);            merge(data, min, mid, max);        }    }

By introducing times, the sum of the last times is calculated in the loop.
There is one more:

public static <T extends Comparable<T>>    void mergeSort(T[] data)    {        int count=0;        long startTime = System.nanoTime();//获取开始的时间;        count = mergeSort(data, 0, data.length - 1);        long endTime = System.nanoTime();//获取结束的时间;        System.out.println("程序所需时间:"+ (endTime - startTime) + "ns");        System.out.println("比较次数:"+ count);    }        private static <T extends Comparable<T>>        int mergeSort(T[] data, int min, int max)    {        int count = 0;        if (min < max)        {            int mid = (min + max) / 2;            mergeSort(data, min, mid);            mergeSort(data, mid+1, max);            merge(data, min, mid, max);        }        return count;    }

Count is returned by changing the lower class to a class that has a return value.

Code Hosting

Pairing and mutual evaluation

Correct use of markdown syntax (plus 1 points)
Complete features in the template (plus 1 points)
Problems and solutions in textbook learning (plus 3 points)
Problem and resolution in code debugging, no problem
Feelings, experience real (add 1 points)
Reviews seriously, can point out the blog and the Code of the problem (plus 1 points)

    • 20172317
      Based on the scoring criteria, I scored the above blog: 2 points. The score is as follows:
    • 20172320
      Based on the scoring criteria, I scored the above blog: 8 points. The score is as follows:

      • Pairs of learning content
        • The 9th chapter of the textbook, running the code on the textbook
        • Finish after-class self-test questions, and refer to the answer study
        • Finish after-class self-test questions, and refer to the answer study
        • Complete programming Project: At least complete PP9.2, PP9.3
Other (sentiment, thinking, etc., optional)

This week will be sorted all over again, feel some understanding is good, writing is still better, but some know it runs the process but the code implementation can not be done, which makes people very anxious. I will also look at the code, the two-way implementation of the merge sort, fast sorting and the cardinality of the order to study a bit more, do not understand the understanding.

Learning progress Bar
lines of code (new/cumulative) Blog Volume (Add/accumulate) Learning Time (new/cumulative) Important Growth
Goal 5000 rows 30 Articles 400 hours
First week 0/0 1/1 8/8
Second week 1306/1306 1/2 20/28
Third week 1291/2597 1/3 18/46
Week Four 4361/6958 2/5 20/66
Week Five 1755/8713 1/6 20/86

Reference: Why is it so difficult to estimate software engineering applications, software engineering estimation methods

    • Planned study time: 10 hours

    • Actual learning time: 8 hours

    • Improved situation:

(See more modern software engineering courseware
Software engineer Competency Self-evaluation table)

Resources
    • Java Programming and Data Structure Tutorial (second edition)

    • Tutorial on Java Programming and data Structure (second edition) Learning Guide
    • Java Objects and reference variables
    • Why Java does not support multiple inheritance

20172327 2018-2019-1 "program design and data structure" Fifth week study summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.