The effect of sorting efficiency is generally compared from 3 aspects: The number of data comparisons, the number of data movement, the size of memory space occupied.
We do a total comparison of bubble sort, select sort, insert sort, and quick sort. In general, the bubble sorting algorithm is not used, because its number of comparisons and the number of moves in several sorting algorithms are the most, its only advantage is that the algorithm is simple, easy to understand, so when the data volume is very small, it will have some application value. The selection sort is the square of N, like the bubble sort, in the number of comparisons, but it lowers the number of exchanges to the lowest, so you can apply selection sorting in cases where the amount of data is small and the exchange data is more time-consuming than the comparison data.
In most cases, the insertion sort algorithm is the best choice when the amount of data is small or basically orderly. Fast sorting is usually the best way to sort larger amounts of data.
The above sorting algorithm takes up very little memory space and requires only one additional variable to temporarily store the data items at the time of exchange. So there is no comparability of the size of the memory footprint.
The number of times to insert a sort is still the square of N, but in general, it's a bit faster than the bubble sort, faster than the sort selection. It is often used in the final stages of a complex sorting algorithm, such as a quick sort.
Algorithm: After i-1 after processing, l[1..i-1] has been arranged in a good order. The first-time process will only insert l[i] into the appropriate position in l[1..i-1],
Make L[1..I] a sequence of rows. To achieve this goal, we can use the sequential comparison method.
First comparison l[i] and l[i-1], if l[i-1]<=l[i], then L[1..I] has been sorted out, the first time processing is over;
Otherwise swap l[i] with the 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] up to date
Advantages: Less moving elements, only one auxiliary space required
N*n of time complexity
When the number of records to be sorted n is very small, this is a good way to sort. But when n is very large, it is not
For example: int[] values = {5, 2, 4, 1, 3};
Sorting process:
1th Time: 2,5,4,1,3
2nd time: 2,4,5,1,3
3rd time: 1,2,4,5,3
4th Time: 1,2,3,4,5
Java code:
Copy Code code as follows:
public class Insertsort {
public static void Main (string[] args) {
Int[] values = {5, 2, 4, 1, 3};
Sort (values);
for (int i = 0; i < values.length; ++i) {
System.out.println (Values[i]);
}
}
public static void sort (int[] values) {
int temp;
int j = 0;
for (int i = 1; i < values.length; i++) {
if (values[i]<values[i-1])//The judgment here is important, this shows the reason for the insertion sort to be faster than the bubble sort and select sort.
{
temp = Values[i];
The data moves backwards
for (j=i-1; j>=0 && temp<values[j]; j--)
{
VALUES[J+1] =values[j];
}
Inserting data into the j+1 location
Values[j+1] =temp;
System.out.print ("First" + (i + 1) + "Times:");
for (int k = 0; k < values.length; k++) {
System.out.print (values[k]+ ",");
}
System.out.println ("");
}
}
}
}
A second example
Copy Code code as follows:
Package Cn.cqu.coce.xutao;
public class Zhijiecharu {
public static void Main (String args[]) {
int a[]={1,2,34,67,8,9,6,7,56,34,232,99};
int i,j,k;
for (i=0;i<a.length;i++)
System.out.print (a[i]+ "T");
System.out.println ();
for (i=1;i<a.length;i++) {
for (j=i-1;j>=0;j--)
if (A[i]>a[j])
Break
if (j!=i-1) {
int temp;
Temp=a[i];
for (k=i-1;k>j;k--)
A[K+1]=A[K];
A[k+1]=temp;
}
}
for (i=0;i<a.length;i++)
System.out.print (a[i]+ "T");
System.out.println ();
}
}