Algorithm Part I: elementary sorts

Source: Internet
Author: User
Tags sorts

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

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.