1 今天看了Java的第三章
2
冒泡法排序:
package Line;
import java.util.Arrays;
public class MaoPao {
public static void main(String[] args){
int[] ary= { 2,3,4,8,7,6,5 } ;
for(int i=0;i<ary.length-1;i++){
for(int j=0;j<ary.length-i-1;j++){
if(ary[j]>ary[j+1]){
int t=ary[j];ary[j]=ary[j+1];ary[j+1]=t;
}
}
}
System.out.println(Arrays.toString(ary));
}
}
public static void main(String[] args){
int[] ary= { 2,3,4,8,7,6,5 } ;
for(int i=0;i<ary.length-1;i++){
for(int j=0;j<ary.length-i-1;j++){
if(ary[j]>ary[j+1]){
int t=ary[j];ary[j]=ary[j+1];ary[j+1]=t;
}
}
}
System.out.println(Arrays.toString(ary));
}
}
选择排序:
package Line;
import java.util.Arrays;
public class XuanZe {
public static void main(String[] args){
int[] ary = {2,4,3,5,9,7,8};
for(int i=0;i<ary.length-1;i++){
for(int j=i+1;j<ary.length;j++){
if(ary[i]>ary[j]) {
int t=ary[i];ary[i]=ary[j];ary[j]=t;
}
}
}
System.out.println(Arrays.toString(ary));
}
public static void main(String[] args){
int[] ary = {2,4,3,5,9,7,8};
for(int i=0;i<ary.length-1;i++){
for(int j=i+1;j<ary.length;j++){
if(ary[i]>ary[j]) {
int t=ary[i];ary[i]=ary[j];ary[j]=t;
}
}
}
System.out.println(Arrays.toString(ary));
}
}
插入排序:
package Line;
import java.util.Arrays;
public class ChaRu {
public static void main(String[] arys){
int[] ary={2,3,4,6,5,8,0};
int i,j,k;
for(i=1;i<ary.length;i++){
k=ary[i];
for(j=i-1;j>=0&&k<ary[j];j--){
ary[j+1]=ary[j];
}ary[j+1]=k;
}
System.out.println(Arrays.toString(ary));
}
public static void main(String[] arys){
int[] ary={2,3,4,6,5,8,0};
int i,j,k;
for(i=1;i<ary.length;i++){
k=ary[i];
for(j=i-1;j>=0&&k<ary[j];j--){
ary[j+1]=ary[j];
}ary[j+1]=k;
}
System.out.println(Arrays.toString(ary));
}
}
快速排序:
package Line;
import java.util.Arrays;
public class Fast {
public static void main(String[] args){
int vec[]={37,47,23,4,7,4,5,100};
Fast f=new Fast();
f.ppp(vec,0,vec.length-1);
System.out.println(Arrays.toString(vec));
}
public void ppp(int vec[],int low,int high){
if(low<high){
int pivot,pos,i;
pos=low;
pivot=vec[pos];
for(i=low+1;i<vec.length;i++){
if(vec[i]>pivot){
pos++;
int tmp=vec[pos];
vec[pos]=vec[i];
vec[i]=tmp;
}
}
int tmp=vec[low];
vec[low]=vec[pos];
vec[pos]=tmp;
ppp(vec,low,pos-1);
ppp(vec,pos+1,high);
}
}
}
public static void main(String[] args){
int vec[]={37,47,23,4,7,4,5,100};
Fast f=new Fast();
f.ppp(vec,0,vec.length-1);
System.out.println(Arrays.toString(vec));
}
public void ppp(int vec[],int low,int high){
if(low<high){
int pivot,pos,i;
pos=low;
pivot=vec[pos];
for(i=low+1;i<vec.length;i++){
if(vec[i]>pivot){
pos++;
int tmp=vec[pos];
vec[pos]=vec[i];
vec[i]=tmp;
}
}
int tmp=vec[low];
vec[low]=vec[pos];
vec[pos]=tmp;
ppp(vec,low,pos-1);
ppp(vec,pos+1,high);
}
}
}
3 明天继续看书