Java merge sort algorithm, bubble sort algorithm, select Sort algorithm, insert sort algorithm, quick sort algorithm description _java

Source: Internet
Author: User
An algorithm is a set of well-defined rules that are used to solve a problem within a finite step. Popular point, that is, the process of computer problem solving. In this process, whether the formation of a problem-solving ideas or writing programs, are in the implementation of some kind of algorithm. The former is the algorithm of inference implementation, the latter is the algorithm of operation implementation.
An algorithm should have the following five important features:
1. Poor: An algorithm must ensure that the implementation of a finite step after the end;
2. Certainty: Each step of the algorithm must have a precise definition;
3. Input: An algorithm has 0 or more inputs to depict the initial condition of the Operation object;
4. Output: An algorithm has one or more outputs to reflect the results of the processing of input data. The algorithm without output is meaningless;
5. Feasibility: The algorithm in principle can be accurately run, and people with a pen and paper to do a finite number of operations can be completed.
Merge sort is a different sort of sorting method, which means merging two or more ordered sequences of data into a new ordered sequence of data, so it is called merging algorithm. Its basic idea is to assume that array A has N elements, then you can consider that array A is composed of N ordered subsequence, each of which has a length of 1 and then a 22 merge, and a sequence of n/2 with a length of 2 or 1 is obtained, followed by 22 merges, so repeated, It is worthwhile to get an ordered sequence of data with a length of N, which is called a 2-way merge sort.
For example, array A has 7 data, respectively: 49 38 65 97 76 13 27, then the process of merging the sorting algorithm is shown in Figure 7:
Initial value [49] [38] [65] [97] [76] [13] [27]
As consisting of 7 subsequence of length 1.
After the first merger [38 49] [65 97] [13 76] [27]
As consisting of 4 subsequence of length 1 or 2.
After the second merger [38 49 65 97] [13 27 76]
As consisting of 2 subsequence of length 4 or 3.
After the third merger [13 27 38 49 65 76 97]
Fig. 6 process diagram of merging sort algorithm
The core operation of the merging algorithm is to combine two two sequential sequences in one-dimensional array into an ordered sequence. The merging algorithm can also be implemented by recursive algorithm, which is simpler in form, but has poor practicability.
The number of merged algorithm merges is a very important amount, according to the calculation when there are 3 to 4 elements in the array, the number of merges
is 2 times, when there are 5 to 8 elements, the number of merges is 3, when there are 9 to 16 elements, the number of merges is 4, according to this rule, when there are N child sequences, it can be inferred that the number of merges is
X (2>=n, the smallest x that meets the sub condition).
Bubble Algorithm Description:
Before interpreting the bubble sort algorithm, we first introduce an algorithm that places the largest number in the last position of 10 digits (in array a). The algorithm is described as follows:
(1) from the array a[1] to the a[10], compare two numbers of 22. That is, a[1] and a[2] compared, after comparison, a[2] and a[3] compared, ... Finally, A[9] and a[10] are compared.
(2) in the process of comparison each time, if the previous number is larger than the latter one, then swap two digits, that is, the larger number to the back, smaller to the front. For example, in the first comparison, if the a[1] is more than a[2] The value of a[1] and a[2 is interchanged. The following figure uses 6 data to illustrate the above algorithm.
Suppose 6 data is: 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 and a[2]=7 Compare, 7>5, do not swap.
5 7 4 3 8 6 The second time, a[2]=7 and a[3]=4 compare, 4<7, swap,
So after the second comparison, the data is 5 4 7 3 8 6
5 4 7 3 8 6 third time, a[3]=7 and a[4]=3 compare, 3<7, swap,
So the third time after the comparison of the data is 5 4 3 7 8 6
5 4 3 7 8 6 fourth times, a[4]=7 and a[5]=8 Compare, 8>7, do not swap.
5 4 3 7 8 6 fifth times, a[6]=6 and a[5]=8 compare, 6<8, swap,
So the fifth and final result is
5 4 3 7 6 8
*******************************************************
Select the Sort algorithm Description:
Before introducing the selection method, we introduce an algorithm to put the smallest number in the first position. Of course, we can use the bubble sort method mentioned above, now we switch to a new algorithm: its guiding principle is not in a hurry to change the position, first from the a[1] start one by one, see which number is the smallest record of the number of position p, Wait for a while to complete the scan, then the a[p] and a[1] swap, then a[1] to a[10 the smallest data in the first position. The steps of the algorithm are as follows:
1), first assume that the number of a[1] is the smallest, write down the position of the p=1;
2), in turn a[p] and A[i] (I from 2 to 10) to compare, each time a comparison is made, if the number of a[i] is smaller than the number in a[p], the value of I is assigned to P, so that p always points to the position of the smallest number currently scanned, that is, a[p] always equals the smallest number of all scanned numbers. After one after the other, P points to the smallest number of 10 digits, i.e. a[p] is the smallest number in the 10 numbers;
3) The number of a[p] and a[1], then the smallest number in a[1], that is, in the front.
If you repeat this algorithm now, but each time you repeat it, the range of the series that is being compared moves back one position. That is, the second time the comparison range is from the 2nd number to the nth number, in this range to find the smallest number of positions P, then the a[p] and the a[2] swap, so from the 2nd number to the nth number of the smallest in the a[2], the third time from the 3rd number to the number of N to find the smallest number, and then the a[p] and a [3] Swap ... This process repeats n-1 times, and the number of N in array A is sorted in order from small to large. The method of sorting is to select the Sort method
*****************************************************************
Insert Sort Algorithm Description:
You can learn the basic idea of sorting by learning the above two methods, or you can make permutations of any unordered array from large to small (descending) or from small to large (ascending). Now suppose there is an ordered sequence of data, requires inserting a number in this sorted sequence of data, but requires that the sequence of data be inserted after the insertion, and that a new sort method--insert Sort Method--Inserts a sort of data into an ordered sequence of data. So we can get a new sequential data with number plus one.
Title: A array of n data, in the order of small to large, enter a number x, insert the value of X into the array A, so that the inserted array of a is still in the small to large arrangement.
So the solution to this problem is:
1), by comparing the size to find where x should be inserted, if it should be placed in the first position;
2) to move all the array elements starting from I (including i) back one position, that is, a[i+1]:=a[i];
A quick sort is an improvement to the bubble sort. Its basic idea is to divide the data into two separate parts by a lie sort a part of all the data is smaller than the other part of all the data, and then the two parts of the data are sorted quickly, the whole sort process can be recursive, so as to achieve the entire data into an ordered sequence.
Suppose the array to sort is a[1] ... A[n], first pick a single piece of data (usually the first data) as the key data, and then put all the number than it before it, all the number larger than it is placed behind it, the process known as a lie fast sort. A quick sort of lying algorithm is:
1, set two variables I, J, the beginning of the sorting i:=1,j:=n;
2 the first array element as the key data, assigned value to X, namely x:=a[1];
3), starting from the beginning of the search, that is, from the start forward search (j:=j-1), find the first value less than x, the exchange between the two;
4), from I start backward search, that is, from the front start backward search (i:=i+1), find the first value greater than X, both exchange;
5, repeat the 3rd, 4 steps until i=j;
For example: The value of the array A to be sorted is: (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
(in the third step of the algorithm, start looking from behind.)
After the second exchange: 27 38 49 97 76 13 65
(according to the fourth step of the algorithm from the front to find >x value, 65>49, both exchange, at this time i:=3)
After the third Exchange: 27 38 13 97 76 49 65
(in the fifth step of the algorithm, the third step to perform the algorithm again starts from behind.)
After the fourth Exchange: 27 38 13 49 76 97 65
(in the fourth step of the algorithm, start looking for a value greater than X, 97>49, the Exchange, the j:=4)
At this point again to perform the third time to find I=j, thus ending a quick sort of lying, then after a quick sort of lying after the results are: 27 38 13 49 76 97 65, that is, the number greater than 49 is all in the back of 49, so less than 49 of the number of all in front of 49
A quick sort is a recursive call to this process--dividing the data sequence with a midpoint of 49, making a similar quick sort of the preceding and subsequent sections, to complete the fast sequencing of all the data sequences, and finally to turn this data sequence into an ordered sequence, The whole process of fast ordering the above array A is based on this idea, as shown in Figure 6:
Initial state {49 38 65 97 76 13 27}
A quick sort is divided into {27 38 13} 49 {76 97 65}
Quick Sort {13} 27 {38} for both front and rear parts respectively
End END {49 65} 76 {97} 49 {65} End
End
*************************************************************************
Figure 6 Quick Sort whole process
Algorithm description for fast sorting
1, the number of n (assumed n=10), stored in the S array;
2), in S[1. N] takes an element as a baseline, such as T=s[1, to set the position where T should be in the sort result, where K is located: S[1. K-1]&LT;=S[K]&LT;=S[K+1..N], that is, the number before s[k] is less than s[k], and the number after s[k is greater than s[k];
3, the use of divided ideas (that is, Dahua small strategy) can be further to the s[1. K-1] and s[k+1. N] Two sets of data are sorted quickly until the grouped object has only one data.
If the specific data are as follows, then the first lie quick sort process 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

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.