选择排序(JAVA实现)

来源:互联网 发布:造价数据库 编辑:程序博客网 时间:2024/06/04 19:57
选择排序概念:首先找到最先的元素然后和第一个元素交换,然后在剩下的元素中找到最小的元素与第二个元素替换,以此类推。
import java.util.*;public class SelectionSort {    public int[] selectionSort(int[] a, int n) {        // write code here        for(int i=0;i<n;i++){            int min = i;            for(int j=i+1;j<n;j++){                if(a[j]<a[min]){                    min = j;                }            }            if(i!=min){                int temp = a[min];               a[min] = a[i];                 a[i] = temp;            }                    }        return a;    }}

1 0