1. Bubble sort
1) Principle: iterate over the sequence to be sorted, a comparison of two elements at a time, assuming they are in the wrong order to exchange them. The work of visiting the series is repeated until there is no need to exchange, that is to say, the sequence is sorted out.
2) Code implementation:
Package Com.test.sort;public class bubblesort{public static void sort (int[] data) {for (int i = 0; i < da Ta.length; i++) { for (int j = data.length-1; j > i; j--) { if (Data[j] < data[j-1]) { swap (data, J , j-1); }}} private static void Swap (int[] data, int a, int b) { int temp = Data[a]; Data[a] = data[b]; DATA[B] = temp; } public static void Main (string[] args) { int[] data = new int[] {3, 5, 3,,, 3423, 675, 4567};
sort (data); System.out.print ("result is: {"); for (int temp:data) { System.out.print (temp + " "); } System.out.print ("}");} }
2, simple selection of sorting
1) Principle: Simple selection of the basic idea of sorting is easy, that is: the first trip, from n elements to find keyword the smallest element and the first element exchange; The second pass, in the n-1 element starting from the second element, keyword the smallest element to be exchanged with the second element; , K-pass, the keyword smallest element is selected from the N-k+1 element beginning with the K element to be exchanged with the K element until the entire sequence is ordered in keyword order.
2) Code implementation:
Package Com.test.sort;public class selectsort{public static void sort (int[] data) {for (int i = 0; i < da Ta.length; i++) { int index = i; for (int j = data.length-1; j > i; j--) { if (Data[j] < Data[index]) { index = j; } } Swap (data, I, index); } } private static void Swap (int[] data, int a, int b) { int temp = Data[a]; Data[a] = data[b]; DATA[B] = temp; } public static void Main (string[] args) { int[] data = new int[] {3, 5, 3,,, 3423, 675, 4567};
sort (data); System.out.print ("Select sort result is: {"); for (int temp:data) { System.out.print (temp + " "); } System.out.print ("}");} }
Bubble sort and simple selection sort--java implementation