1. Implementation of sorting Selection
2. Implementation of insert sorting
3. Implementation of shell sorting
Pay attention to the selection of H values in the code.
4. Shuffling (random algorithm)
Problem description: given that the number of elements in a group is n Array I, the positions of each element are randomly rescheduled, and the probability of each element appearing at each position is equal.
Solution (1 ):
Idea: declare an array J of the double type whose length is N, and generate n random variables and assign them to the elements in J in sequence. The I array corresponds to the values in the J array one by one. Sort the J array. When the position of elements in J changes, the corresponding position of elements in I also changes. When array J is sorted, the new order of array I is obtained.
Solution (2 ):
Idea: traverse the elements in array I in sequence. When the elements whose index is n are traversed, a random number x ranging from 0 to N is generated, and the elements whose index is n are exchanged with those whose index is X.
Implementation:
5. convex hull (convex hull problem)
Problem description: There are N points in the plane. Several points are found in the N points to form a polygon so that all points are included in the polygon.
Application:
(1) There are obstacles in the middle of the two points. Find the shortest path in the two points.
(2) given n points on the plane, find the two points with the farthest distance.
First, we need to recognize two facts: (1) the convex hull of N points will surely traverse all vertices on the convex hull in a counterclockwise direction. (2) set the smallest point of the ordinate value of N points to. When the convex hull is traversed counterclockwise from a, the angle between the point on the convex hull and the line between A and the X axis will gradually increase.
To solve the problem:
How can I determine whether X-> Y-> Z is counter-clockwise?
We can easily determine the values of the following matrices.
Implementation:
Specific implementation of the solution:
Algorithm Part I: elementary sorts