Classic sorting Algorithm (Java edition) reprint
1. Bubble Sort Bubble
The simplest sort method is the bubble sort method. The basic idea of this approach is to think of the elements to be sorted as "bubbles" that are vertically arranged, smaller elements lighter and thus upward. In the bubble sorting algorithm we have to deal with this "bubble" sequence several times. The so-called process, is to check the sequence from the bottom up, and always pay attention to the sequence of two adjacent elements is correct. If the order of two adjacent elements is found to be incorrect, that is, the "light" elements are below, exchanging their positions. Obviously, after processing, the "lightest" element floats to its highest position, and after two times, the "light" element floats to the next high position. In the second pass, you do not have to check because the element at the highest position is already the lightest element. In general, when I pass the processing, I do not have to check the higher position above the elements, because after the previous i-1 the processing, they have been correctly sequenced. This algorithm can be implemented as follows.
The algorithm is as follows:
/**
* Bubble Sort
* @paramsrc Array to sort
*/
void Dobubblesort (int[] src)
{
int len=src.length;
for (int i=0;i<len;i++)
{
for (int j=i+1;j<len;j++)
{
int temp;
if (Src[i]>src[j])
{
TEMP=SRC[J];
Src[j]=src[i];
Src[i]=temp;
}
}
Printresult (I,SRC);
}
}
2. Choose Sort Selection
The basic idea of choosing sort is: treat the sort of record sequence to n-1 the processing, the 1th time processing is the L[1..N] the smallest person and l[1] Exchange position, the 2nd time processing is L[2..N] the smallest person and l[2] exchange position, ..., the first time processing is to be l[i. N] The smallest person in the l[i] exchange position. Thus, after I pass the processing, the position of the first I record has been arranged in order from small to large.
Of course, in practice, you can also sort by selecting the largest person from the record to be sorted and the first record interchange location, as needed, in order from large to small.
The algorithm is as follows:
/**
* Select sort
* @paramsrc Array to sort
*/
void Dochoosesort (int[] src)
{
int len=src.length;
int temp;
for (int i=0;i<len;i++)
{
Temp=src[i];
Int J;
Subscript for the minimum number of int samllestlocation=i;//
for (j=i+1;j<len;j++)
{
if (src[j]<temp)
{
temp=src[j];//Remove Minimum value
samllestlocation=j;//Remove the lowest value of the subscript
}
}
Src[samllestlocation]=src[i];
Src[i]=temp;
Printresult (I,SRC);
}
}
3. Insert sorting Insertion Sort
The basic idea of inserting a sort is that, after i-1-through, l[1..i-1] is in the right order. I-pass processing only l[i] into the appropriate position of l[1..i-1], so that l[1..i] is a sequence of orderly. To achieve this, we can use a sequential comparison method. First compare L[i] and l[i-1], if l[i-1]≤l[i] 騆 [1..I] has been ordered, the first time the processing is finished, otherwise exchange l[i] and l[i-1] position, continue to compare l[i-1] and l[i-2] until a certain position J (1≤j≤i-1) is found, Make l[j]≤l[j+1].
In short, the insertion sort is where each step inserts a pending data in its size to the appropriate position in the sorted data until all is inserted. Insert Sort method The direct insert sort and binary insert sort are two kinds, here only the direct insert sort, binary insert sort leave to "find" content.
Figure 1 illustrates the process of directly inserting a sequence of 4 elements, which requires (a), (b), (c) three insertions.
Figure 1 Inserting a sort of 4 elements
In the following insertion sorting algorithm, in order to write a program we can introduce a sentinel element L[0], which is smaller than any record in L[1..N]. So, we have a constant-∞ in the type ElementType of the element, which is smaller than any record that might occur. If the constant-∞ is not good in advance, it is necessary to check if the current position is 1 before deciding whether L[i] will move forward, and if the current position is 1 o'clock, the processing of the I-pass should be closed. Another option would be to put L[i] into l[0] at the beginning of the first processing, which would also guarantee the end of the I-pass process at the appropriate time. The current position will be judged by the algorithm below.
The algorithm is as follows:
/**
* Insert sort (while loop implementation)
* @paramsrc Array to sort
*/
void DoInsertSort1 (int[] src)
{
int len=src.length;
for (int i=1;i<len;i++)
{
int temp=src[i];
int j=i;
while (src[j-1]>temp)
{
SRC[J]=SRC[J-1];
j--;
if (j<=0)
Break
}
Src[j]=temp;
Printresult (I+1,SRC);
}
}
/**
* Insert sort (for loop implementation)
* @paramsrc Array to sort
*/
void DoInsertSort2 (int[] src)
{
int len=src.length;
for (int i=1;i<len;i++)
{
Int J;
int temp=src[i];
for (j=i;j>0;j--)
{
if (src[j-1]>temp)
{
SRC[J]=SRC[J-1];
}else//if the current number, not a small number in front, that means not less than the previous number,
Because the front is already lined up, so directly out of the current round of comparison
Break
}
Src[j]=temp;
Printresult (I,SRC);
}
}
Classic sorting Algorithm (Java edition)