分类: Java
2008-07-09 23:34:40
//交换排序之--最简单的冒泡排序法
public static void BubbleSort(int[] a)
{
int i,j,temp;
int n = a.length;
for(i=0;i
for(j=i+1;j
if (a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
//插入排序-- 直接插入排序
public static void InsertSort(int[] a)
{
int i,j,temp;
int n = a.length;
for(i=1;i
temp = a[i];
j=i-1;
while((a[j]>temp)&&(j>=0))
{
a[j+1] = a[j];
j--;
}
a[j+1] = temp;
}
}
//选择排序
public static void SelectSort(int[] a)
{
int i,j,min,temp;
int n=a.length;
for(i=0;i
min = a[i];
for(j=i+1;j
if(a[j]
temp = min;
min =a[j];
a[j] = temp;
}
}
a[i] =min ;
}
}
public static void main(String[] args)
{
int[] arr = {45,76,32,32,5,4,54,7,943,3};
int n = arr.length;
BubbleSort(arr);
for(int i=0;i
System.out.println();
InsertSort(arr);
for(int i=0;i
System.out.println();
SelectSort(arr);
for(int i=0;i
}
}
(上述出自;http://hi.baidu.com/haoshuang3394/blog/item/3033044c2c2338f8d72afc9e.html 未经验证)