Sorting algorithms, basic high-level languages have some to offer. C language has
qsort ()Functions, C + + has
sort ()Functions, the Java language has
ArraysClass (not array). When you use these sorts, you can write your own collation.
The Java API's description of the arrays class is that this class contains various methods for manipulating arrays, such as sorting and searching.
1. Ordering of arrays of basic data types
Note: (1) the sort () in the arrays class uses a "fast-tuned sorting method";
(2) such as int[],double[],char[] arrays of base types, the arrays class simply provides the default ascending order, and does not provide the corresponding descending arrangement.
(3) To sort the arrays of the underlying type in descending order, you need to convert these arrays into corresponding encapsulated class arrays, such as integer[],double[],character[, and so on, to order the arrays of these classes. (In fact, it's better to sort in ascending order than to order it yourself).
Sort by default ascending pairs of groups
Function prototype: static void sort(int[] a) sorts the specified int array in ascending numeric order.
The static void sort(int[] A, int fromindex, int toindex) sorts the specified range of the specified int array in ascending numeric order.
Code instance:
The package _10arrays class provides two sorting methods;
Import Java.util.Arrays;
public class _01 The sort method of the base data type array {public
static void Main (string[] args) {
// int[] A = {1, 4,-1, 5, 0};
char[] A = {' B ', ' d ', ' C ', ' e ', ' a '};
Arrays.sort (a);
The contents of the array a[] become { -1,0,1,4,5} for
(int i = 0; i < a.length; i++) {
System.out.print (A[i] + " ");
}
}
}
2. Sorting data for a composite data type
Function prototypes: (1) public static<t> void sort (t[] a,comparator c) Sorts the specified array of objects according to the order in which the comparer is produced.
(2) Public static<t> void sort (t[] a,int fromindex,int toindex,comparator c) Sorts the specified range of the specified object array according to the order in which the comparer is produced.
Description: This two sorting algorithm is a "tuning merge sort " algorithm.
The package _10arrays class provides two sorting methods;
Import Java.util.Arrays;
Import Java.util.Comparator;
Class node{private int key;
private int value;
public Node (int key, int value) {This.key = key;
This.value = value;
public int Getkey () {return key;
public int GetValue () {return value;
@Override public String toString () {return this.key + "-" + this.value; Class Nodecomparator implements comparator<node>{@Override public int Compare (node O1, node O2) {//Retur
n O1.getkey ()-o2.getkey () >0?1:o1.getkey ()-o2.getkey () <0?-1:0;
Return O1.getvalue ()-o2.getvalue () >0?1:o1.getvalue ()-o2.getvalue () <0?-1:0;
The public class _02 to sort the objects that implement the comparator interface {public static void main (string[] args) {node[] array = new NODE[4];
for (int i = 0; i < Array.Length i++) {//array[i] = new Node (array.length-i, i);
Array[i] = new Node (i+1, array.length-i);
} System.out.println ("before sort"); for (int i = 0; i < Array.Length i++) {SYSTEM.OUt.println (Array[i]);
} arrays.sort (Array, new Nodecomparator ());
System.out.println ("after sort");
for (int i = 0; i < Array.Length i++) {System.out.println (array[i]);
}
}
}