# Description of Java Merge Sorting Algorithm, Bubble Sorting Algorithm, selection sorting algorithm, insertion sorting algorithm, and quick Sorting Algorithm

Source: Internet
Author: User

Algorithm Is a set of clearly defined rules used to solve a problem within a limited step. In layman's terms, it is the process of solving computer problems. In this process, no matter whether it is a problem-solving idea or writing Program All are implementing certain algorithms. The former is an algorithm implemented by reasoning, and the latter is an algorithm implemented by operations.
An algorithm should have the following five important features:
1. Poor: An algorithm must end after performing a finite step;
2. validation: each step of an algorithm must be exactly defined;
3. Input: An algorithm has 0 or more inputs to characterize the initial state of an operation object;
4. Output: An algorithm has one or more outputs to reflect the results of input data processing. Algorithms without output are meaningless;
5. Feasibility: in principle, the algorithm can be accurately run and can be completed after a limited number of computations are performed using pen and paper.
Merge sort is another type of sorting method. The meaning of merging is to combine two or more ordered data sequences into a new ordered data sequence, therefore, it is also called the merge algorithm. The basic idea is to assume that array A has n elements, and array a is composed of N ordered subsequences. The length of each subsequence is 1, then merge them in two ways to obtain an n/2 ordered subsequences with a length of 2 or 1, and then merge them in two, it is worthwhile to obtain a sequence of ordered data whose length is N. This sorting method is called 2-way merge sorting.
For example, if array A has 7 data records, which are 49 38 65 97 76 13 27, then the operation process using the Merge Sorting Algorithm is 7:
Initial Value [49] [38] [65] [97] [76] [13] [27]
It is regarded as composed of seven subsequences with a length of 1.
[38 49] [65 97] [13 76] [27] after the first merge
It is regarded as composed of four subsequences with a length of 1 or 2.
[38 49 65 97] [13 27 76] after the second merge
It is considered to be composed of two subsequences with a length of 4 or 3.
[13 27 38 49 65 76 97] after the third merge
Figure 6 Merge Sorting Algorithm Process Diagram
The core operation of the merge algorithm is to combine two adjacent sequential sequences in one-dimensional array into one ordered sequence. Merge algorithms can also be implemented using recursive algorithms. The form is simple, but the practicality is poor.
The number of merge operations of the merge algorithm is a very important amount. The number of merge operations is calculated when the array contains 3 to 4 elements.
It is twice. When there are 5 to 8 elements, the number of merging operations is three. When there are 9 to 16 elements, the number of merging operations is four. According to this rule, when there are n subsequences, we can infer that the number of merging times is
X (2> = N, the minimum X that meets the sub-condition ).
Bubble Algorithm Description:
Before interpreting the Bubble sorting algorithm, we first introduce an algorithm that puts the maximum number of 10 numbers (placed in array A) at the last position. The algorithm is described as follows:
(1) compare two adjacent numbers from array a [1] to array a [10. That is, a [1] and a [2] are compared. After comparison, a [2] is compared with a [3 ,...... Finally, compare a [9] with a [10.
(2) In each comparison process, if the previous number is greater than the previous one, two numbers will be adjusted, that is, the larger number will be adjusted to the back, and the smaller one will be adjusted to the front. For example, in the first comparison, if a [1] is greater than a [2], the values of a [1] and a [2] are exchanged. Use six pieces of data to describe the above algorithms.
Assume that the six data entries are: A [] = 5 7 4 3 8 6
A [1] A [2] a [3] a [4] a [5] a [6]
5 7 4 3 8 6 for the first time, a [1] = 5 compared with a [2] = 7, 7> 5, no matching is performed.
5 7 4 3 8 6 second time, a [2] = 7 and a [3] = 4 comparison, 4 <7,
Then the data after the second comparison is 5 4 7 3 8 6
5 4 7 3 8 6 3, a [3] = 7 and a [4] = 3, 3 <7,
The data after the third comparison is 5 4 3 7 8 6
5 4 3 7 8 6 4 times, a [4] = 7 compared with a [5] = 8, 8> 7, no reconciliation.
5 4 3 7 8 6 5 times, a [6] = 6 compared with a [5] = 8, 6 <8,
So the fifth and last results are:
5 4 3 7 6 8
**************************************** ***************
Select the Sorting Algorithm Description:
Before introducing the selection and sorting method, we first introduce an algorithm that puts the smallest number at the first position. Of course, we can also use the Bubble sorting method mentioned above. Now we use a new algorithm: the guiding ideology is not to rush to change the position first. First, check the number one by one from a [1]. To check the minimum number, write down the position P of the number, then, we call a [p] And a [1] to change the smallest data from a [1] to a [10] to the first position. The algorithm steps are as follows:
1) assume that the number in a [1] is the smallest, and write down the position P = 1;
2) compare a [p] And a [I] (I changed from 2 to 10, if the number of a [I] is smaller than the number in a [p], the I value is assigned to P so that P always points to the position of the minimum number scanned currently, that is to say, a [p] is always the minimum number of all scans. After one-to-one comparison, P points to the location of the smallest number in 10, that is, a [p] is the smallest number in 10;
3) when we compare the numbers of a [p] And a [1], the smallest number is in a [1], that is, at the beginning.
If you repeat this algorithm now, the range of the series for comparison moves one position backward. That is, when the second comparison is performed, the range ranges from 2nd to the nth number. In this range, locate the smallest number position P, and then compare a [p] with a [2, in this way, the number starting from 2nd to the decimal place in the nth number is in a [2], and the minimum number is found in the third time from the nth number to the nth number, and then compare a [p] with a [3 ...... After this process repeats the N-1, the N number in array a is arranged in the order of ascending. The sorting method is to select the sorting method.
**************************************** *************************
Description of the inserted Sorting Algorithm:
By studying the above two methods, you can understand the basic idea of sorting, or you can arrange any unordered array in ascending or descending order. Now we assume that there is an ordered data sequence that requires inserting a number in this sorted data sequence. However, after the insertion, the data sequence is still ordered, in this case, we need to use a new sorting method-insert sorting method. The basic operation of insert sorting is to insert a data to the sorted data, in this way, we can obtain a new ordered data with a number plus one.
Question: There are n data in array A, which are arranged in ascending order. Input a number X and insert the value of X into array, so that the inserted array A is still arranged in ascending order.
The algorithm for solving this problem is:
1) Find the position where X should be inserted by comparing the size, if it should be placed at the I position;
2) Move all array elements starting with I (including I) One position backward, that is, a [I + 1]: = A [I];
Quick sorting is an improvement of Bubble sorting. Its basic idea is: Split the data to be sorted into two independent parts by means of a lie-down sorting, and all the data in one part is smaller than all the data in the other part, then, the data is sorted by the second method. The whole sorting process can be recursive to convert the entire data into an ordered sequence.
Assume that the array to be sorted is a [1]... A [n], first select a data (usually the first data) as the key data, and then put all the numbers that match it in front of it, all the numbers that are larger than it are placed behind it. This process is called "One lie" and "Fast sorting. The following algorithm is used to sort data quickly:
1) set two variables, I: = 1, J: = N;
2) use the first array element as the key data and assign it to X, that is, X: = A [1];
3) Start from J to search forward, that is, start from the back to search forward (J: = J-1), find the first value less than X, the two exchange;
4) Search backward from I, that is, search backward from the beginning (I: = I + 1), find the first value greater than X, and exchange the two;
5) Repeat steps 3rd and 4 until I = J;
For example, the values of array a to be sorted are: (initial key data X: = 49)
A [1] A [2] a [3] a [4] a [5] a [6] a [7]:
49 38 65 97 76 13 27
After the first exchange: 27 38 65 97 76 13 49
(Start from the end of step 3 of the algorithm.
After the second exchange: 27 38 49 97 76 13 65
(According to the fourth step of the algorithm, find the value> X from the beginning, 65> 49, and exchange the two. At this time, I: = 3)
After the third exchange: 27 38 13 97 76 49 65
(Follow the fifth step of the algorithm to find the third step for executing the algorithm again.
After the fourth exchange: 27 38 13 49 76 97 65
(According to the fourth step of the algorithm, find the value greater than X from the beginning, 97> 49, the two exchange, at this time J: = 4)
At this time, when the third day is not executed, I = J is found, and then the result is: 27 38 13 49 76 97 65, that is to say, all the numbers greater than 49 are behind 49, so all the numbers smaller than 49 are above 49.
Fast sorting is a recursive call to this process-split the data sequence with 49 as the midpoint and perform similar fast sorting on the previous and subsequent parts respectively to complete the fast sorting of all data sequences, finally, the data sequence is converted into an ordered sequence. According to this idea, the entire process of fast sorting for the preceding array a is shown in 6:
Initial status {49 38 65 97 76 13 27}
After a quick sorting, it is divided into {27 38 13} 49 {76 97 65}
Fast sorting of the first and second parts {13} 27 {38}
End ended {49 65} 76 {97} 49 {65}
End
**************************************** *********************************
Figure 6 whole process of fast sorting
Description of the algorithm for fast sorting
1) The number of N (assuming n = 10) is set and stored in the S array;
2) In s [1 .. N]. Take an element as the benchmark. For example, t = s [1] is used to determine the position K where T should be in the sorting result. The position of this K is: s [1 .. K-1] <= s [k] <= s [k + 1 .. n], that is, the number before S [k] is smaller than s [K], and the number after s [k] is greater than s [k];
3) The use of the sub-governance idea (that is, the big strategy and small Strategy) can be further applied to s [1 .. K-1] And s [k + 1 .. N] the two groups of data are sorted quickly until the group object has only one data.
If the specific data is as follows, the first process of fast sorting is:
Array Subscript: 1 2 3 4 5 6 7 8 9 10
45 36 18 53 72 30 48 93 15 36
I j
(1) 36 36 18 53 72 30 48 93 15 45
(2) 36 36 18 45 72 30 48 93 15 53
(3) 36 36 18 15 72 30 48 93 45 53
(4) 36 36 18 15 45 30 48 93 72 53
(5) 36 36 18 15 30 45 48 93 72 53
Related Keywords:
Related Article

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.