The sorting algorithms for containers in java JDK mainly use insert sorting and Merge Sorting. the implementations of different versions may be different. The key code is as follows:
Copy codeThe Code is as follows :/**
* Performs a sort on the section of the array between the given indices
* Using a mergesort with exponential search algorithm (in which the merge
* Is saved med by exponential search). n * log (n) performance is guaranteed
* And in the average case it will be faster then any mergesort in which
* Merge is saved med by linear search.
*
* @ Param in-
* The array for sorting.
* @ Param out-
* The result, sorted array.
* @ Param start
* The start index
* @ Param end
* The end index + 1
*/
@ SuppressWarnings ("unchecked ")
Private static void mergeSort (Object [] in, Object [] out, int start,
Int end ){
Int len = end-start;
// Use insertion sort for small arrays
If (len <= SIMPLE_LENGTH ){
For (int I = start + 1; I <end; I ++ ){
Comparable <Object> current = (Comparable <Object>) out [I];
Object prev = out [I-1];
If (current. compareTo (prev) <0 ){
Int j = I;
Do {
Out [j --] = prev;
} While (j> start
& Amp; current. compareTo (prev = out [j-1]) <0 );
Out [j] = current;
}
}
Return;
}
Int med = (end + start) >>> 1;
MergeSort (out, in, start, med );
MergeSort (out, in, med, end );
// Merging
// If arrays are already sorted-no merge
If (Comparable <Object>) in [med-1]). compareTo (in [med]) <= 0 ){
System. arraycopy (in, start, out, start, len );
Return;
}
Int r = med, I = start;
// Use merging with exponential search
Do {
Comparable <Object> fromVal = (Comparable <Object>) in [start];
Comparable <Object> rVal = (Comparable <Object>) in [r];
If (fromVal. compareTo (rVal) <= 0 ){
Int l_1 = find (in, rVal,-1, start + 1, med-1 );
Int toCopy = l_1-start + 1;
System. arraycopy (in, start, out, I, toCopy );
I + = toCopy;
Out [I ++] = rVal;
R ++;
Start = l_1 + 1;
} Else {
Int r_1 = find (in, fromVal, 0, r + 1, end-1 );
Int toCopy = r_1-r + 1;
System. arraycopy (in, r, out, I, toCopy );
I + = toCopy;
Out [I ++] = fromVal;
Start ++;
R = r_1 + 1;
}
} While (end-r)> 0 & (med-start)> 0 );
// Copy rest of array
If (end-r) <= 0 ){
System. arraycopy (in, start, out, I, med-start );
} Else {
System. arraycopy (in, r, out, I, end-r );
}
}
We can see that there is an interesting Sorting Algorithm on the programming Pearl River. The bitmap method uses 1 bits to represent [0 ~ Whether the integer in n-1] exists. 1 indicates yes, and 0 indicates no. Ing a positive integer to a bit set. Each bit indicates whether the mapped positive integer exists.
For example, {1, 2, 3, 5, 8, 13} uses the following set:
0 1 1 1 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0
The pseudocode is as follows:
For (I in [0 ~ N-1]) bit [I] = 0;
For (I in [0 ~ N-1])
If (I in input file)
Bit [I] = 1
For (I in [0 ~ N-1])
If (bit [I] = 1)
Output I
Using java code, the efficiency is really good:
Copy codeThe Code is as follows: public class javaUniqueSort {
Public static int [] temp = new int [1000001];
Public static List <Integer> tempList = new ArrayList <Integer> ();
Public static int count;
Public static void main (final String [] args ){
List <Integer> firstNum = new ArrayList <Integer> ();
List <Integer> secondNum = new ArrayList <Integer> ();
For (int I = 1; I <= 1000000; I ++ ){
FirstNum. add (I );
SecondNum. add (I );
}
Collections. shuffle (firstNum );
Collections. shuffle (secondNum );
GetStartTime ();
Collections. sort (firstNum );
GetEndTime ("java sort run time ");
GetStartTime ();
SecondNum = uniqueSort (secondNum );
GetEndTime ("uniqueSort run time ");
}
Public static List <Integer> uniqueSort (final List <Integer> uniqueList ){
JavaUniqueSort. tempList. clear ();
For (int I = 0; I <javaUniqueSort. temp. length; I ++ ){
JavaUniqueSort. temp [I] = 0;
}
For (int I = 0; I <uniqueList. size (); I ++ ){
JavaUniqueSort. temp [uniqueList. get (I)] = 1;
}
For (int I = 0; I <javaUniqueSort. temp. length; I ++ ){
If (javaUniqueSort. temp [I] = 1 ){
JavaUniqueSort. tempList. add (I );
}
}
Return javaUniqueSort. tempList;
}
Public static void getStartTime (){
Using huffle. start = System. nanoTime ();
}
Public static void getEndTime (final String s ){
Using huffle. end = System. nanoTime ();
System. out. println (s + ":" + (using huffle. end-using huffle. start) + "ns ");
}
}
Running time:
Java sort run time: 1257737334ns
UniqueSort run time: 170228290ns
Java sort run time: 1202749828ns
UniqueSort run time: 169327770ns
If duplicate data exists, modify the following:
Copy codeThe Code is as follows: public class javaDuplicateSort {
Public static List <Integer> tempList = new ArrayList <Integer> ();
Public static int count;
Public static void main (final String [] args ){
Random random = new Random ();
List <Integer> firstNum = new ArrayList <Integer> ();
List <Integer> secondNum = new ArrayList <Integer> ();
For (int I = 1; I <= 100000; I ++ ){
FirstNum. add (I );
SecondNum. add (I );
FirstNum. add (random. nextInt (I + 1 ));
SecondNum. add (random. nextInt (I + 1 ));
}
Collections. shuffle (firstNum );
Collections. shuffle (secondNum );
GetStartTime ();
Collections. sort (firstNum );
GetEndTime ("java sort run time ");
GetStartTime ();
SecondNum = uniqueSort (secondNum );
GetEndTime ("uniqueSort run time ");
}
Public static List <Integer> uniqueSort (final List <Integer> uniqueList ){
JavaDuplicateSort. tempList. clear ();
Int [] temp = new int [200002];
For (int I = 0; I <temp. length; I ++ ){
Temp [I] = 0;
}
For (int I = 0; I <uniqueList. size (); I ++ ){
Temp [uniqueList. get (I)] ++;
}
For (int I = 0; I <temp. length; I ++ ){
For (int j = temp [I]; j> 0; j --){
JavaDuplicateSort. tempList. add (I );
}
}
Return javaDuplicateSort. tempList;
}
Public static void getStartTime (){
Using huffle. start = System. nanoTime ();
}
Public static void getEndTime (final String s ){
Using huffle. end = System. nanoTime ();
System. out. println (s + ":" + (using huffle. end-using huffle. start) + "ns ");
}
}
This algorithm has obvious limitations. For example, you need to know the largest value in the data, and more importantly, the degree of data confidentiality. For example, the maximum value is 1000000 and the array size is only 100, then the efficiency will be greatly reduced ..... However, the use of Bitmap sorting makes people shine. Bitmap is usually used to store data and determine whether a data exists or whether an array exists repeatedly.