template
bool BubSort(DataType a[], int l, int r, char z) //冒泡排序
{
int i=0;
int j=0;
DataType k;
if(z=='y')//升序排列
{
for (i=l;i<=r-1;i++)
for (j=r-1;j>=i+1;j--)
if (a[j-1]>a[j])
{
k=a[j-1];
a[j-1]=a[j];
a[j]=k;
}
}
else if(z=='n')//降序排列
{
for (i=l;i<=r-1;i++)
for (j=r-1;j>=i+1;j--)
if (a[j-1] {
k=a[j-1];
a[j-1]=a[j];
a[j]=k;
}
}
else return false;
}
冒泡排序的一个模板函数,供楼主参考!
#include
void main()
{
int a[]=,*p=a;
int i,j,temp,size;
size=sizeof(a)/sizeof(int);
for(i=0;i
for(j=0;j
if(*(p+j)>*(p+j+1))
{
temp=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=temp;
}
}
}
for(i=0;i
上学期学的,生疏了,呵呵
楼主试试:
public class Test {
public static void main(String[] args) {
int[] arr = new int[]{2,3,1};
for(int i=0;i
int tmp = arr[0];
arr[0] = arr[1];
arr[1] = tmp;
}
}
try{
for(int i=0;i<=arr.length;i++){
System.out.println(arr[i]);
}
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
}
}