Take a look at several common sorting algorithms in the next two days.
1. Bubble Sort Method
Each time the adjacent elements are compared from i=0, if arr[i]>arr[i+1], they are exchanged. Until the biggest element is pushed to the end. Go back to i=0 until it's done.
1 ImportJava.util.Scanner;2 classBubble3 {4 Public Static voidMain (string[] args)5 {6 intn,temp;7 inti,j;8 int[] arr=New int[10000];9Scanner scanner=NewScanner (system.in);Ten OneSystem.out.printf ("Input n="); An=scanner.nextint (); - -System.out.printf ("Number of input N:"); the for(i=0;i<n;i++) -arr[i]=scanner.nextint (); - - for(i=0;i<n;i++) + { - for(j=0;j<n-1-i;j++) + { A if(Arr[j]>arr[j+1])//always swap with adjacent elements at a time at { -temp=Arr[j]; -Arr[j]=arr[j+1]; -arr[j+1]=temp; - } - } in } - to for(i=0;i<n;i++) +System.out.printf ("%3d", Arr[i]); -System.out.printf ("\ n"); the } *}
2. Selection method
Starting with i=0, compare the arr[i] with each subsequent element, placing the smallest element in its current position. Increment I to place the remaining minimum elements at the current I position until completed.
1 ImportJava.util.Scanner;2 classSelect3 {4 Public Static voidMain (string[] args)5 {6 inti,j,h;7 int[] arr=New int[10000];8 intTemp,n;9 TenScanner scanner=NewScanner (system.in); OneSystem.out.printf ("Input n="); An=scanner.nextint (); -System.out.printf ("Number of input N:"); - for(i=0;i<n;i++) thearr[i]=scanner.nextint (); - - for(i=0;i<n;i++) - { +H=i; - for(j=i+1;j<n;j++) + { A if(arr[h]>Arr[j]) atH=J; - } - if(i!=h)//i!=h only need to exchange, otherwise I position is the smallest - { -temp=Arr[i]; -arr[i]=Arr[h]; inarr[h]=temp; - } to } + for(i=0;i<n;i++) -System.out.printf ("%3d", Arr[i]); theSystem.out.printf ("\ n"); * } $}
3. Insert Sort
Inserts the element to be sorted into the appropriate position of the sorted sequence.
1 ImportJava.util.Scanner;2 classInsert3 {4 Public Static voidMain (string[] args)5 {6 inti,j;7 intTemp,n;8 int[] arr=New int[10000];9 TenScanner scanner=NewScanner (system.in); OneSystem.out.printf ("Input n="); An=scanner.nextint (); -System.out.printf ("Number of input N:"); - for(i=0;i<n;i++) thearr[i]=scanner.nextint (); - - for(i=0;i<n;i++) - { +temp=Arr[i]; - for(J=i-1;j>=0 && arr[j]>temp;j--)//inserts the current element Arr[i] into the previously sorted sequence. + { Aarr[j+1]=Arr[j]; at } -arr[j+1]=temp; - } - - for(i=0;i<n;i++) -System.out.printf ("%3d", Arr[i]); inSystem.out.printf ("\ n"); - } to}
4. Hill sort
Hill Sort by adjusting the step K allows the element to move a longer distance each time. The step k decrements, when K=1, is actually the insertion sort method.
1 ImportJava.util.Scanner;2 classShell3 {4 Public Static voidMain (string[] args)5 {6 inti,j;7 inttemp,n,k;8 int[] arr=New int[10000];9 TenScanner scanner=NewScanner (system.in); OneSystem.out.printf ("Input n="); An=scanner.nextint (); -System.out.printf ("Number of input N:"); - for(i=0;i<n;i++) thearr[i]=scanner.nextint (); - -K=n/2; - while(k>=1)//Insert sort when k=1 + { - for(i=k;i<n;i+=k) + { Atemp=Arr[i]; at for(J=i-k;j>=0 && arr[j]>temp;j-=k) - { -arr[j+k]=Arr[j]; - } -arr[j+k]=temp; - } ink/=2;//Step Decreasing - } to + for(i=0;i<n;i++) -System.out.printf ("%3d", Arr[i]); theSystem.out.printf ("\ n"); * } $}
Several common sorting algorithms (bubble, select, insert, Shell, continued)