Three sorts: Bubble, select, insert Sort
public static void Bubblesort (int[] source) {//Exchange class sort thought: 22 compare the keywords to be sorted, and find that the records are exchanged in reverse until there are no reversed records. for (int i = source.length-1, i > 0; i--) {for (int j = 0; J < i; J + +) {if (source[ J] > Source[j + 1]) {swap (source, J, j+1); }}}} public static void Selectsort (int[] source) {//Select class sort thought : First finds the smallest element in an unordered sequence, holds it to the starting position of the sort sequence, and then finds the next smallest element from the remaining unsorted elements and places it at the end of the sorted sequence. for (int i = 0, i < source.length; i++) {for (int j = i+1; J < Source.length; J + +) { if (Source[j] > Source[i]) {Swap (source, I, j); }}}}//Starting with the first element, the element can assume that the element//is taken out of the next element, scanned from behind in the already sequenced element, if the element is greater than the new one, the element is moved to the next public static void Insertsort (int[] source) {for (int i = 1; i < source.length; i++) {for (int j = i; (J > 0) && (Source[j] < source[j-1]); j--) {Swap (source, J, j-1); }}} private static void Swap (int[] source, int x, int y) {in T temp = source[x]; SOURCE[X] = Source[y]; Source[y] = temp; public static void Main (string[] args) {int[] a = {4, 2, 1, 3, 4, 6, 7, 8, 0}; int i; Bubblesort (a); for (i = 0;i<a.length;i++) {System.out.printf ("%d", a[i]); }
Three sorts of Java: bubbling, selecting, inserting sort