1. Insert sorting algorithm to implement Java version
public static int[] insert_sort(int[] a){for (int i = 0; i < a.length; i++){for(int j=i+1;j>0&&j<a.length;j--){if(a[j]<a[j-1]){int tmp = a[j]; //It is logically possible to define the initialization in this way, j variable, a[j] = a[j-1];a[j-1] = tmp;}}} return a; //It is designed to not return here, the original array has also been modified and sorted}2. Select the sorting algorithm to implement the Java version
public static int[] select_sort(int[] a){for (int i = 0; i < a.length; i++){int min_pos = i;for(int j=i+1;j<a.length;j++){if(a[j] < a[min_pos]){min_pos = j;}}int tmp = a[i]; // swap operation a[i] = a[min_pos];a[min_pos] = tmp;}return a;}3. Implementation of the bubble sorting algorithm java
Ordinary bubbles
public static int[] bubble_sort(int[] a){for (int i = 0; i < a.length; i++){//After each trip a[i] is the i-th smallest for(int j = a.length-1;j>i;j--)//There are subsequent j-1 operations to note j>i{if(a[j] < a[j-1]){int tmp = a[j]; // swap operation a[j] = a[j-1];a[j-1] = tmp;}}} return a;}Improve bubble sorting and end early
public static int[] bubble_sort_flag(int[] a){boolean isChange = true;for (int i = 0; i < a.length && isChange; i++){isChange = false;for(int j = a.length-1;j>i;j--)//After subsequent j-1 operations, please note that j>i{if(a[j] < a[j-1]){int tmp = a[j]; // swap operation a[j] = a[j-1];a[j-1] = tmp;isChange = true;}}} return a;}The above are the various sorting algorithms implemented by Java (insert sorting, selection sorting algorithm, bubble sorting algorithm) introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!