The Java JDK's container class sorting algorithm uses mainly insert sorting and merge ordering, which may differ from implementation of different versions, with key code as follows:
Copy Code code 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 performed by exponential search). N*log (n) performance is guaranteed
* And in the average case it'll be faster then any mergesort in which the
* Merge is performed 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 the 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
&& 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);
}
}
See the programming Zhuji has a very interesting sort algorithm-bitmap method its idea is to use 1 bits to represent the existence of integers in [0~n-1]. 1 indicates existence, and 0 indicates that it does not exist. A positive integer is mapped to a bit set, and each bit represents the existence of a positive integer that it maps.
For example, {1,2,3,5,8,13} uses the following collection to represent:
0 1 1 1 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0-0
Pseudo code 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
Try it with Java code, and the efficiency is good enough:
Copy Code code 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 () {
Javashuffle.start = System.nanotime ();
}
public static void Getendtime (final String s) {
Javashuffle.end = System.nanotime ();
SYSTEM.OUT.PRINTLN (S + ":" + (Javashuffle.end-javashuffle.start) + "ns");
}
}
Run Time:
Java Sort Run Time:1257737334ns
Uniquesort Run Time:170228290ns
Java Sort Run Time:1202749828ns
Uniquesort Run Time:169327770ns
If you have duplicate data, you can modify the following:
Copy Code code 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 () {
Javashuffle.start = System.nanotime ();
}
public static void Getendtime (final String s) {
Javashuffle.end = System.nanotime ();
SYSTEM.OUT.PRINTLN (S + ":" + (Javashuffle.end-javashuffle.start) + "ns");
}
}
This algorithm still has obvious limitations, such as to know the largest number of data, more importantly, the degree of data density, such as the maximum value of 1000000 and the size of the array is only 100, then the efficiency will fall very obvious ... However, the use of bitmap method to sort, is really a bright light. Bitmap methods are usually used to store data, to determine whether a data exists or to determine if an array is duplicated.