Bubble Sort:
Thought: bubbling sort repeatedly visited the sequence to sort, comparing two elements at a time, swapping them out if they were in the wrong order. The work of the sequence of visits is repeated until no more exchange is needed, that is, the sort is complete.
Features: Relatively stable, the number of small order is relatively good
PackageCn.guangboyuan;/** * @authorRed Ants * Public Number: Programmer's Road * Performance comparison of two bubble sorting*/ Public classDubblesort {Private Static BooleanCheckarray (int[] data) { if(Data = =NULL|| Data.length = = 0){ return false; } return true; } Public Static int[] Dubblesort (int[] data) { if(!Checkarray (data)) { return NULL; } inttemp; intRunnum = 0; for(inti = 0; i < data.length; i++) {System.out.println (String.Format ("I=%d", i)); for(intj = i; J < Data.length; J + +) {System.out.print (String.Format ("J=%d,", J)); if(Data[i] >Data[j]) {Temp=Data[i]; Data[i]=Data[j]; DATA[J]=temp; } runnum++; } System.out.println (""); } System.out.println (String.Format ("Dubblesort Run Times:%d", Runnum)); returndata; } Public Static int[] DubbleSort1 (int[] data) { if(!Checkarray (data)) { return NULL; } System.out.println (String.Format ("int array length:%d", data.length)); inttemp; intRunnum = 0; for(inti = 0; i < data.length-1; i++) {System.out.println (String.Format ("I=%d", i)); for(intj = 0; J < Data.length-1-i; J + +) {System.out.print (String.Format ("J=%d,", J)); if(Data[j] > data[j+1]) {temp=Data[j]; DATA[J]=data[j+1]; Data[j+1] =temp; } runnum++; } System.out.println (""); } System.out.println (String.Format ("Dubblesort Run Times:%d", Runnum)); returndata; } Public Static voidMain (string[] args) {int[] data =New int[]{8,4,9,13,11,99,2,1,5,3,6};; Dubblesort (data); for(inti:data) {System.out.print (i+","); } System.out.println (""); int[] Data1 =New int[]{8,4,9,13,11,99,2,1,5,3,6}; DubbleSort1 (DATA1); for(inti:data1) {System.out.print (i+","); } }}
Select Sort:
Thought: first find the smallest element in the array, and second, swap it with the first element. Next, find the second and second exchange. Run time and input independent, minimal data movement
Features: Data movement is minimal, unstable, suitable for use when the number of sorts is less
PackageCn.guangboyuan;Importjava.util.Arrays;/** * @authorRed Ants * Public number: Programmer's Way * Select Sort*/ Public classSelectionsort { Public Static int[] Selectionsort (int[] ints) { inttemp; intRunnum = 0; for(inti = 0; i < ints.length; i++) { for(intj = i+1; J < Ints.length; J + +) { if(ints[j]<Ints[i]) {Temp=Ints[i]; Ints[i]=Ints[j]; INTS[J]=temp; } runnum++; }} System.out.println (String.Format ("Number of runs:%d", Runnum)); returnints; } Public Static voidMain (string[] args) {int[] INTs =New int[]{8,4,9,13,11,99,2,1,5,3,6}; Selectionsort (INTs); System.out.println (arrays.tostring (ints)); }}
Insert Sort:
Thought: first two numbers of arrays are sorted, then the third and first two numbers are sorted, and so on
Features: stable, suitable for most of the ordered when it is better
PackageCn.guangboyuan;Importjava.util.Arrays;/** * @authorRed Ants * Public Number: Programmer's Road * insert sort*/ Public classInsertionsort { Public Static int[] Insertionsort (int[] ints) { inttarget = 0; intRunnum = 0; for(inti = 1; i < ints.length; i++) { intj =i; Target=Ints[i]; while(J > 0 && Target < ints[j-1]) {Ints[j]= Ints[j-1]; J--; Runnum++; } Ints[j]=Target; } System.out.println (String.Format ("Number of runs:%d", Runnum)); returnints; } Public Static voidMain (string[] args) {int[] INTs =New int[]{8,4,9,13,11,99,2,1,5,3,6}; System.out.println (Arrays.tostring (Insertionsort (ints))); }}
Java implementation bubble Sort, select sort, insert sort