选择排序法

来源:互联网 发布:网络金融征信系统 编辑:程序博客网 时间:2024/06/06 00:26
public class TestNumSort {
public static void main(String[] args) {
int[] a = {5, 7, 4, 6, 2, 9, 8, 1, 3, 10};
TestNumSort test = new TestNumSort();             //记得创建一个对象,才可以调用之后的方法
test.Print(a);
test.NumSortSolution(a);
test.Print(a);
}


private void Print(int[] num) {                 //输出num数组中的所有元素
for(int temp : num) {
System.out.print(temp + " ");
}
System.out.println();
}


private void NumSortSolution (int[] num) {             //选择排序法
int k,j;
for(int i = 0; i < num.length-1; i++ ) {
k = i;
for(j = i + 1; j < num.length; j++) 
if(num[j] > num[k]) k = j;
int t = num[k];
num[k] = num[i];
num[i] = t;
}
}
}
0 0