Insert Sort
PackageCom.pb.string.demo1;/*** Insert Sort *@authorDenny **/ Public classDemo { Public Static voidMain (string[] args) {int[] arr = {5, 2, 8, 3, 1, 9, 6, 7, 11, 0,-3,-5,-8,-11 }; Insertsort (arr); Print (arr); } Public Static voidInsertsort (int[] arr) { for(inti = 1; i < arr.length; i++) { intkey = Arr[i];//Temporary variable storage /*For (int j = i-1; J >= 0 && arr[j] > key; j--) {arr[j + 1] = Arr[j]; ARR[J] = key; }*/ for(intj=i-1;j>=0;j--){ if(arr[j]>key) { //Exchange Variable ValuesARR[J+1]=ARR[J];//The value of the current subscript i = the value of the current subscript is JArr[j]=key;// } } } } Public Static voidPrintint[] arr) {System.out.print ("["); for(inti = 0; i < arr.length; i++) { if(I! = arr.length-1) {System.out.print (Arr[i]+ ","); } Else{System.out.println (Arr[i]+ "]"); } } }}
Results:
[ -11,-8,-5,-3,0,1,2,3,5,6,7,8,9,11]
java-Insert Sort