Four simple sorting algorithms (insert, bubble, select and sort, and quick sort) + tower Sort Algorithm

Source: Internet
Author: User

Easy to understand:

Assume that the array length is 8 and the sequence number starts from 0.

1. Insert sorting:

(1) sorting is performed 7 times.

For (INT I = 1; I <array. length; I ++ ){

For (Int J = I; j> 0; j --)

If (array [J-1]> array [J])

Swap (ref array [J-1], ref array [J]) the switching position ensures that the first I position of the array is sorted in order.

}

(2) duplicate (1 ).

2. Bubble Sorting:

(1) sorting is performed 7 times.

For (INT I = 1; I <array. length; I ++ ){

For (Int J = array. Length-1; j> 0; j --)

If (array [J-1]> array [J])

Swap (ref array [J-1], ref array [J]) Swap location, find the smallest item.

}

(2) duplicate (1 ).

3. Select sorting:

(1) sorting is performed 7 times.

For (INT I = 0; I <array. Length-1; I ++ ){

Lowestindex = I;

For (Int J = array. Length-1; j> 0; j --)

If (array [lowestindex]> array [J])

{Lowestindex = J;} // minimum index (TAG ).

Swap (ref array [J], ref array [lowestindex]) Swap location to find the minimum entry.

}

(2) duplicate (1 ).

Private understanding.

4. Fast sorting

quick sorting is currently one of the most widely recognized sorting methods (depending on the problem-solving objects ), although the fast sorting method can reach O (n2) in the worst case, the efficiency of the fast sorting method is quite good in most cases.
the basic spirit of quick sorting is to find the proper axis in the series, split the series into two parts, and sort the series on the left and right respectively, it is the choice of the axis that affects the efficiency of quick sorting
.
the first Quick Sort version introduced here is mentioned in most textbooks because it is the easiest to understand, it is also best suited to the concept of axis separation and left-right sorting
, and is suitable for beginners to explain.
solution
the quick calculation described here is as follows:
1. set the leftmost number to the axis and record its value as S
loop processing:
(1 ). search index I from the left to the right of the sequence until the number greater than S is found
(2 ). search index J from the right to the left of the series until the number less than S is found
(3 ). if I> = J, leave the loop
(4 ). if I (5 ). swap the Left axis with j
(6 ). return to the left of the Axis
(7 ). roll back the right side of the Axis

Perform the followingAlgorithm, The value on the left of the axis will be less than S, and the value on the right of the axis will be greater than S, so that the left and right sides of the axis can be handed back to complete the sorting, for example
As shown in the following example, * indicates the number of switches, and [] indicates the axis:
● [41] 24 76*11 45 64 21 69 19 36 *
● [41] 24 36 11 45*64 21 69 19*76
● [41] 24 36 11 19 64*21*69 45 76
● [41] 24 36 11 19 21 64 69 45 76
● 21 24 36 11 19 [41] 64 69 45 76
In the above example, the value on the left of 41 is smaller than it, and the value on the right is larger than it, so that the left and right sides are handed back to the sorting.

5. Tower Algorithm

Towers of Hanoi is the French M. Claus (Lucas) who took it from Thailand to France in 1883. Hanoi is the capital of the Vietnam war, that is, the current Ho Chi Minh City;
In 1883, the French mathematician Edouard Lucas mentioned this story. It is said that at the time of the century, Benares had a boro tower backed by three diamond bars (PAG ).
God placed 64 gold disks (disc) arranged from top to bottom in ascending order on the first rod, and ordered the monks to move all the gold disks from the first stone rod to the third stone rod, and move
In the course of operation, we observe the principle that big plates are under small plates. If we only move one plate every day, the tower will be damaged when the whole number of dishes is handled, that is, the end of the world.
.
Solution
If the column is marked as ABC, it will be moved from A to C. When there is only one plate, it will be moved directly to C. When there are two plates, B will be treated as the auxiliary column.


If there are more than two disks, it is very easy to cover the third and lower plates. Each time we handle two plates, that is: a-> B, A-> C, B-> C, and
The hidden part is actually the progressive processing of the program.


In fact, if there are n plates, the number of moves required is 2 ^ n-1, so when the number of disks is 64, the required number is:
264-1 = 18446744073709551615
For the year 5.05390248594782e + 16, that is, the 1949th century, if there is no idea about this number, it is assumed that a plate is ready every second, and it will be offered for 5000 years left.
Right.

Exercise:

Effect:

 

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.