1. Various sort plots:
Code implementation
1 Importjava.util.Arrays;2 3 Public classArraySort {4 //Select Sort5 Public Static voidSlectesort (int[] arr) {6 System.out.println (arrays.tostring (arr));7 for(inti=0;i<arr.length;i++){8 intminindex=i;9 for(intj=i+1;j<arr.length;j++){Ten if(arr[minindex]>Arr[j]) { Oneminindex=J; A } - } - if(minindex!=i) { the inttemp=Arr[i]; -arr[i]=Arr[minindex]; -arr[minindex]=temp; - } + } - System.out.println (arrays.tostring (arr)); + } A //Bubble Sort at Public Static voidBubsort (int[] arr) { - for(inti=0;i<arr.length-1;i++) {//control comparison: n-1 times (n = number of elements) - /* - * Controls the comparison of two adjacent elements, and if the latter is small, it is exchanged. - * Because after each outer loop, you will find a maximum value placed on the last side - * So the maximum value that J can fetch should be reduced in turn. (j<arr.length-1-i) in */ - for(intj=0;j<arr.length-i-1;j++){ to if(arr[j]>arr[j+1]){ + intB=arr[j+1]; -arr[j+1]=Arr[j]; thearr[j]=b; * } $System.out.println ("arr" + (i+1) + "sub-outer loop," + (j+1) + "After comparison:" +arrays.tostring (arr));Panax Notoginseng } - } the System.out.println (arrays.tostring (arr)); + } A //Insert Sort the Public Static voidInsertsort (int[] arr) { + - for(inti=1;i<arr.length;i++){ $ /* $ * Because the first one to be compared to the number, the front must be counted, so I starting from 1 - */ - intTemp=arr[i];//Temp Temporary Data the intIndex=0; - /*Wuyi * J must be removed from the outer loop to the first start of the number of temp, always forward to the front, if there is no more than the removal of the number of the small number of temp, to the * J==-1 the end of time. - * Otherwise, the number that is smaller than the number found in temp ends Wu */ - for(intj=i-1;j>=-1;j--){ Aboutindex++; $System.out.println ("arr. +i+" outer loop, section "+index+" second comparison "+arrays.tostring (arr)); - if(J==-1) {//If there are no elements to find the front, put the temp in front of the arr[0]; -arr[0]=temp; - Break; A } + if(temp<Arr[j]) { theARR[J+1]=ARR[J];//if the found element is larger than temp, go to the back one -}Else{ $Arr[j+1]=temp;//if the found element is smaller than temp, it is placed after the number temp the Break;//and jump out of this cycle the } the } the } - } in //recursive algorithm-factorial, the computational process of the century is calculated from the last step the Public Static voidFactorial (intb) { the intA=1; About for(inti=1;i<=b;i++) { thea*=i; the } the System.out.println (a); + } - Public Static intFactorial1 (inti) { the while(i!=1){BayiSystem.out.println (i+ "x" + (I-1) + "!"); the returnFactorial1 (i-1) *i; the } - return1; - } the // the Public Static voidMergeint[] arr,intLeftintMidintRight ) { the int[] temp=New int[Right-left+1]; the intlow1=Left ; - intLow2=mid+1; the intIndex=0; the while(low1<=mid&&low2<=Right ) { the if(arr[low1]<Arr[low2]) {94temp[index++]=arr[low1++]; the}Else { thetemp[index++]=arr[low2++]; the }98 } About while(low1<=mid) { -temp[index++]=arr[low1++];101 }102 while(low2<=Right ) {103temp[index++]=arr[low2++];104 } theSystem.out.println ("left=" +Left );106System.out.println ("arr=" +arrays.tostring (arr));107 for(inti=0;i<index;i++){108arr[left++]=Temp[i];109 } theSystem.out.println ("temp=" +arrays.tostring (temp));111System.out.println ("arr" +arrays.tostring (arr)); theSystem.out.println ("===================================");113 } the //a recursive split of an array the Public Static voidCaiint[] arr,intLeftintRight ) { the intMid= (right+left)/2;117 if(left<Right ) {118 Cai (arr,left,mid);119Cai (arr,mid+1, right); - merge (arr,left,mid,right);121 }122 System.out.println (arrays.tostring (arr));123}View Code
Array sorting algorithm in Java