--------------------------------------------------------------------------------------------------------
This question is an examination of the sort
--------------------------------------------------------------------------------------------------------
Algorithm one (bubbling sort)
Adjacent to the two comparisons, the larger to replace the cycle, so big will be like bubbling one by one pops up (funny 0.0)
1 ImportJava.util.*;2 public classMain {3 public Static voidmain (string[] Args) {4Scanner sc =NewScanner (system.in);5 intn =Sc.nextint ();6 int[] A =New int[n];7 for(inti=0;i<n;i++)8a[i] =Sc.nextint ();9 //Bubble SortTen for(inti=0;i<n-1;i++){ one for(intk=0;k<n-1-i;k++){ a if(a[k]>a[k+1]){ - intTMP =a[k]; -a[k] = a[k+1]; thea[k+1] =tmp; - } - } - } + for(intI:a) { -System.out.print (i+ ""); + } a } at}
Algorithm two (select sort)
Each loop finds the smallest subscript to be substituted to the front (there are temporary variables to save the subscript, manually select Qaq)
1 ImportJava.util.*;2 public classMain {3 public Static voidmain (string[] Args) {4Scanner sc =NewScanner (system.in);5 intn =Sc.nextint ();6 int[] A =New int[n];7 for(inti=0;i<n;i++)8a[i] =Sc.nextint ();9 //Select SortTen for(intk=0; k<n-1; k++) { one intMin =k; a for(inti=k+1; i<n; i++) { - if(a[i] <A[min]) { -Min =i; the } - } - if(k! =Min) { - inttemp =a[k]; +a[k] =a[min]; -a[min] =temp; + } a } at for(intI:a) { -System.out.print (i+ ""); - } - } -}
Algorithm three (insert sort)
Think of the front part as the insertion area, each time you insert the following number into the front (if it is smaller than the previous number, move the previous number backward, if it is larger than the direct replacement)
1 ImportJava.util.*;2 public classMain {3 public Static voidmain (string[] Args) {4Scanner sc =NewScanner (system.in);5 intn =Sc.nextint ();6 int[] A =New int[n];7 for(inti=0;i<n;i++)8a[i] =Sc.nextint ();9 //Insert SortTen for(inti=1;i<n;i++){ one intTMP =a[i]; a for(intk=i-1;k>=0;k--){ - if(a[k]>Tmp) { -a[k+1] =a[k]; thea[k] =tmp; -}Else - break; - } + } - for(intI:a) { +System.out.print (i+ ""); a } at } -}
Other Algorithms (temporarily Written)
Algorithm one (similar to the insertion sort, which is sorted directly when the value is Obtained)
1 ImportJava.util.*;2 public classMain {3 public Static voidmain (string[] Args) {4Scanner sc =NewScanner (system.in);5 intn =Sc.nextint ();6 int[] A =New int[n];7 //single Row8 for(inti=0;i<n;i++){9a[i] =Sc.nextint ();Ten for(intj=0;j<i;j++){ one if(a[i]<A[j]) { a intTMP =a[i]; -a[i] =a[j]; -a[j] =tmp; the } - } - } - for(intI:a) { +System.out.print (i+ ""); - } + } a}
Algorithm two (bubbling and inserting the combination geek, the first didn't understand the penetration algorithm on the write a freak 0.0)
1 ImportJava.util.*;2 public classMain {3 public Static voidmain (string[] Args) {4Scanner sc =NewScanner (system.in);5 intn =Sc.nextint ();6 int[] A =New int[n];7 //the combination of bubbling and inserting freaks8 for(inti=0;i<n;i++)9a[i] =Sc.nextint ();Ten for(inti=1;i<n;i++){ one for(intk=0;k<i;k++){ a if(a[i]<A[k]) { - intTMP =a[i]; -a[i] =a[k]; thea[k] =tmp; - } - } - } + for(intI:a) { -System.out.print (i+ ""); + } a } at}
Order of entry training sequences