Common Java sorting algorithms
Bubble sort, select sort, quick sort
1 PackageCom.javaee.corejava;2 3 Public classDatasort {4 5 PublicDatasort () {6 //TODO auto-generated Constructor stub7 }8 9 Public Static voidMain (string[] args) {Ten int[] p = {34, 21, 54, 18, 23, 76, 38, 98, 45, 33, 27, 51, 11, 20, 79, One30, 89, 41 }; A - LongStart =System.currenttimemillis (); - theDatasort.bubblesort (P);//Bubble Sort - //Datasort.selectsort1 (p);//Select sort 1 - //Datasort.selectsort2 (p);//Select sort 2 - //Datasort.quicksort (p, 0, p.length-1);//Quick Sort + -System.out.println ("Time used:" + (System.currenttimemillis ()-start)); + for(inti = 0; i < p.length; i++) { ASystem.out.print (P[i] + ""); at } - - } - //Bubble Sort - Public Static voidBubblesort (int[] data) { - for(inti=0;i<data.length-1;i++){ in for(intj=0;j<data.length-i-1;j++){ - if(data[j]>data[j+1]){ to inttemp=Data[j]; +Data[j]=data[j+1]; -data[j+1]=temp; the } * } $ }Panax Notoginseng } - //Select sort 1 the Public Static voidSelectSort1 (int[] data) { + if(data==NULL|| Data.length==0){ A return; the } + for(inti=0;i<data.length-1;i++){ - intmin=i; $ for(intj=i+1;j<data.length;j++){ $ if(data[j]<Data[min]) { -min=J; - } the } - if(i!=min) {Wuyi inttemp=Data[i]; thedata[i]=Data[min]; -data[min]=temp; Wu } - } About } $ //Select sort 2 - Public Static voidSelectSort2 (int[] data) { - for(inti=0;i<data.length-1;i++){ - for(intj=i+1;j<data.length;j++){ A if(data[i]>Data[j]) { + inttemp=Data[i]; thedata[i]=Data[j]; -data[j]=temp; $ } the } the } the } the //Quick Sort - Public Static voidQuickSort (int[] Data,intStartintend) { in intkey=Data[start]; the intI=start; the intj=end; About while(i<j) { the while(data[j]>key&&i<j) { thej--; the } +data[i]=Data[j]; - while(data[i]<key&&i<j) { thei++;Bayi } thedata[j]=Data[i]; the } -data[i]=key; - if(i-1>start) { theQuickSort (data,start,i-1); the } the if(i+1<end) { theQuickSort (data,i+1, end); - } the } the the}
Common Java sorting algorithms