Bubble Sort:
int[] hehe={4,7,2,5,6,9,0}; for(intI=0; i){ for(intj=i+1; j){ if(hehe[i]>Hehe[j]) { inttemp=Hehe[i]; Hehe[i]=Hehe[j]; HEHE[J]=temp; } } }
Insert Sort
int[] a={ -,7,8,9,Ten,1,2, +}; inti,j,t,h; for(i=1; i<a.length;i++) {T=a[i];//the number of one digit after the depositJ=i-1; while(j>=0&& t<A[j]) {A[j+1]=A[J];//the last digit equals the previous oneJ--; } a[j+1]=t;//if a while is made, the latter number equals the afterSystem. out. Print ("Section"+i+"Step sort Result:");//outputs the results of each step sort for(h=0; h<a.length;h++) {System. out. Print (" "+A[H]);//Output} System. out. Print ("\ n"); }
Simple sorting
for(inti =0; I < hehe.length-1; i++) { intMin =i; //Find Smallest name for(intj = i +1; J < Hehe.length; J + +) { if(Hehe[j] < hehe[min])//Find Minimum value note here is not judge A[j]<a[i]Min =J; } //Swap Data if necessary if(min! =i) {inttemp =Hehe[i]; Hehe[i]=Hehe[min]; Hehe[min]=temp; } } for(inti =0; i < hehe.length; i++) {System. out. println (Hehe[i]); }
Sorting by dichotomy
int[] A = {4,2,1,6,3,6,0, -5,1,1 }; for(inti =0; I <Ten; i++) {System. out. printf ("%d", A[i]); } System. out. println ("No. 0 Time"); intI, J; intLow , high, mid; inttemp; for(i =1; I <Ten; i++) {Temp=A[i]; Low=0; High= i-1; Mid= (low + high)/2; //The value of high , that is, the number of times to change while(Low <=High ) {Mid= (low + high)/2; if(A[mid] >temp) high= Mid-1;//calculation of the number of times Else Low= Mid +1; } //Log in Exchange for(j = i-1; J > high; j--) {a[j+1] =A[j]; } A[high+1] =temp; } for(i =0; I <Ten; i++) {System. out. printf ("%d", A[i]); } System. out. println ("after sorting"); }
Quick Sort
Public classQuickSort { Public voidQuick_sort (int[] Arrays,intlenght) { if(NULL= = Arrays | | Lenght <1) {System. out. println ("input error!"); return; } _quick_sort (Arrays,0, Lenght-1); } Public void_quick_sort (int[] Arrays,intStartintend) { if(Start >=end) { return; } inti =start; intj =end; intValue =Arrays[i]; Boolean flag=true; while(I! =j) {if(flag) {if(Value >Arrays[j]) {Swap (arrays, I, j); Flag=false; } Else{J--; } } Else { if(Value <Arrays[i]) {Swap (arrays, I, j); Flag=true; } Else{i++; }}} SNP (arrays); _quick_sort (Arrays, start, J-1); _quick_sort (Arrays, I+1, end); } Public voidSNPint[] arrays) { for(inti =0; i < arrays.length; i++) {System. out. print (Arrays[i] +" "); } System. out. println (); } Private voidSwapint[] Arrays,intIintj) {inttemp; Temp=Arrays[i]; Arrays[i]=Arrays[j]; ARRAYS[J]=temp; } Public Static voidMain (String args[]) {quickSort Q=NewQuickSort (); int[] A = { the, -, $, A, $,5 }; Q.quick_sort (A,6); }}
Several sorting algorithms in Java: Bubble sort, insert sort, binary sort, simple sort, quick sort