Bubble sort
Compare adjacent elements, if the first one is larger than the second, then swap their positions; each pair of adjacent elements is compared sequentially, and the last element should be the largest.
int[] array = {10,13,12,8,11,6};// Array from small to large sort for (int i = 0; i < array.length - 1; i++) { for (int j = 0; j < array.length - i - 1;j ++) { if (array[j] > array[j &NBSP;+&NBSP;1]) { a = Array[j]; array[j] = array[j &NBSP;+&NBSP;1];&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ARRAY[J&NBSP;+&NBSP;1] = a; } }}system.out.println ( Arrays.tostring (array));//[6, 8, 10, 11, 12, 13]
Compare sort
Take the elements in the array, followed by the elements, if they are greater than the subsequent elements, then the exchange of their position, in this way the array after comparison is a small to large sorted array.
Array from small to large sort for (int i = 0; i < Array.Length; i++) {for (int j = i + 1; j < Array.length;j + +) {if (Array[i] > Array[j]) {a = Array[i]; Array[i] = Array[j]; ARRAY[J] = A; }}}system.out.println (arrays.tostring (array));//[6, 8, 10, 11, 12, 13]
Recursive
The way the function itself calls itself is called recursion.
Public int[] sortarray (int[] array,int index) { int a = 0; if (index <= array.length - 1) { for (int i = index + 1; i < array.length;i ++) { if (array[index] > array[i]) { a = array[index]; array[index] = array[i]; array[i] = a; } } sortarray (Array,index + 1); } return array;} System.out.println (Sortarray (array,0))
Two-point Search
Two-point lookup speed, data sources need to arrange in order
Query 11 in the array subscript int queryparam = 11; Arrays.sort (array);//Sort int i = 0;int start = 0;int end = Array.length-1;while (start <= end) {i = (start + end)/ 2; if (Queryparam = = Array[i]) {System.out.println (i); Break } else if (Queryparam < array[i]) {end = i; } else if (Queryparam > Array[i]) {start = i; }}
Java Algorithm Chapter Summary