JAVA-insert sorting, java-sorting
Insert sort
Package com. pb. string. demo1;/*** insert sort * @ author Denny **/public class Demo {public static void main (String [] args) {int [] arr = {5, 2, 8, 3, 1, 9, 6, 7, 11, 0,-3,-5,-8,-11}; insertSort (arr); print (arr );} public static void insertSort (int [] arr) {for (int I = 1; I <arr. length; I ++) {int key = arr [I]; // Temporary Variable storage/* for (int j = I-1; j> = 0 & arr [j]> key; j --) {arr [j + 1] = arr [j]; ar R [j] = key;} */for (int j = I-1; j> = 0; j --) {if (arr [j]> key) {// swap variable value arr [j + 1] = arr [j]; // the value of the current subscript I = the value of the current subscript j arr [j] = key; // }}} public static void print (int [] arr) {System. out. print ("["); for (int I = 0; I <arr. length; I ++) {if (I! = Arr. length-1) {System. out. print (arr [I] + ",");} else {System. out. println (arr [I] + "]") ;}}}
Result:
[-11,-8,-5,-3, 0, 1, 2, 3, 5, 6, 8, 9, 11]