1、数组的冒泡排序
public void bubbleSort(int a[]) {
int n = a.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
2、数组的选择排序
public void selectSort(int a[]) {
for (int n = a.length; n > 1; n--) {
int i = max(a, n);
int temp = a[i];
a[i] = a[n - 1];
a[n - 1] = temp;
}
}
3、数组的插入排序
public void insertSort(int a[]) {
int n = a.length;
for (int i = 1; i < n; i++) { //将a[i]插入a[0:i-1]
int t = a[i];
int j;
for (j = i - 1; j >= 0 && t < a[j]; j--) {
a[j + 1] = a[j];
}
a[j + 1] = t;
}
}
4、设置两层循环
for(int i=0;i<arrayOfInts.length;i++)
{
for(int j=i+1;j<arrayOfInts.length;j++)
{
if(arrayOfInts[i]>arrayOfInts[j])
{
a=arrayOfInts[i];
arrayOfInts[i]=arrayOfInts[j];
arrayOfInts[j]=a;
}
}
}
5、还有一种方法就是用Arrays.sort()方法:
//导入包
import java.util.Arrays;
public class Two3{
public static void main(String[]args)
{
int[]arrayOfInts={32,87,3,589,12,7076,2000,8,622,127};
Arrays.sort(arrayOfInts);
for(int i=0;i<arrayOfInts.length-1;i++)
{
System.out.print(arrayOfInts[i]+" ");
}
}
}
小示例:
Array stuInfo = Array.CrateInstance(typeOf(Student),5);
Array stuScore = Array.CreateInstance(typeOf(double),5);
Array.Sort(stuScore,stuInfo);
Array.Reverse(stuInfo);