More important three basic sorts where the insertion sort is optimal, the selection sort is centered, the bubble sort is the worst
PackageCom.zzw. Sorting; Public class alogrithm { Public Static void Main(string[] args) {int[] Datas =New int[Ten];//Initialize dataInitData (datas);//Print initial dataPrintdata (datas);//Sort the dataSelectsort (datas);//print sorted dataPrintdata (datas); }/** * Initialize data * * @param datas * * Public Static void InitData(intDatas[]) { for(inti =0; i < datas.length; i++) {//randomly generates a value of 1-100Datas[i] = (int) (Math.random () * -) +1; } }/** * Print Data * * @param datas * * Public Static void Printdata(intDatas[]) { for(inti =0; i < datas.length; i++) {System.out.print (Datas[i] +","); } System.out.println (); }/** * Insert sort * / Public Static void Insertsort(intDatas[]) {intj =0;The following table where the second data begins to be inserted inti =0;//Number of inserts for(i =1; i < datas.length; i++) {inttemp = Datas[i]; for(j = i-1; J >=0; j--) {if(Datas[j] > Temp) {datas[j +1] = Datas[j]; }Else{ Break; } }//Judge J==-1, or the first position less than or equal to tempDatas[j +1] = temp; } }/** * Bubble sort * * Public Static void Bubblesort(intDatas[]) {intj =0;inti =0; for(j = datas.length-1; J >0; j--) { for(i =0; I < J; i++) {if(Datas[i] > datas[i +1]) {inttemp = Datas[i +1]; Datas[i +1] = Datas[i]; Datas[i] = temp; } } } }/** * Select sort * * Public Static void Selectsort(intDatas[]) { for(intI=0; i<datas.length-1; i++) {intMinindex=i; for(intj=i+1; j<datas.length;j++) {if(Datas[minindex]>datas[j]) {minindex=j; } }intTemp=datas[i]; Datas[i]=datas[minindex]; Datas[minindex]=temp; } }}
Basic sort (including insert, select, bubble Sort)