The example of this article describes the implementation method of the choice of Java sorting advanced. Share to everyone for your reference. Specifically as follows:
Select sort (Selection sort) is a simple and intuitive sort algorithm. Its working principle is as follows. The smallest (Large) element is first found in the unordered sequence, placed at the start of the sort sequence, and then continues to look for the smallest (large) element from the remaining unsorted elements and then to the end of the sorted sequence. And so on until all the elements are sorted.
The primary advantage of selecting a sort is related to data movement. If an element is in the correct final position, it is not moved. Select the sort to exchange a pair of elements each time, at least one of them will be moved to its final position, so the table of n elements is sorted in total for up to n-1 exchanges. In all sorts of sorting methods that rely entirely on swapping to move elements, choosing a sort is a very good one.
Worst time complexity о (n²)
О of optimal Time complexity (n²)
О of average time complexity (n²)
Worst spatial complexity о (n) Total, O (1) Auxiliary
Code implementation:
Package com.baobaotao.test; /** * Sorting Research */public class Sort {/** * Select sort * @param array array/public static void Select
Sort (int[] array) {int length = Array.Length;
int index = 0;
for (int i=0;i<length-1;i++) {index = i;
for (int j=i+1;j<length;j++) {if (Array[j] < Array[index]) {index = j;
} swap (array, I, index);
Printarr (array); }/** * In the order of small to large Exchange array * @param a incoming array * @param b the number of incoming to exchange B * @param c the number of incoming to exchange c * * * * public s
tatic void Swap (int[] A, int b, int c) {if (b = = c) return;
int temp = A[b];
A[B] = A[c];
A[C] = temp;
/** * Print Array * @param array/public static void Printarr (int[] array) {for (int c:array) {
System.out.print (c + "");
} System.out.println (); public static void Main (string[] args) {int[] number={11,95,45,15,78,84,51,24,12};
Selectsort (number); }
}
Output:
95
11 (
11)
12 15 24 45 51 78 84 95 (+)---------
I hope this article will help you with your Java programming.