62,614
社区成员
发帖
与我相关
我的任务
分享
public static void main(String args[]){
int a=1;
int b=2;
ValueSort vs=new ValueSort();
vs.swap(a, b);
System.out.println(a+" "+b);
}
public void swap(int m,int n){
int temp = m;
m = n;
n = temp;
}
由于是值传递,m、n是a、b的拷贝,在执行swap()时,只是形参m、n的值发生了变化,可是a和b的值并没有变
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a={9,4,5,6,7,8,1};
InsertSort insort=new InsertSort();
insort.insertSort(a);
insort.print(a);
}
private void print(int[] a) {
// TODO Auto-generated method stub
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
private void insertSort(int[] c) {
for(int p=1;p<c.length ;p++){
int tmp=c[p];
int j=p;
for( ;j>0 && (tmp<c[j-1]);j--){
c[j]=c[j-1];
}
c[j]=tmp;
}
}