直接选择排序:数组A[n],遍历数组元素,将最小的元素放在第一位,剩余元素重复该操作。
编程实现:
/**
* <p>Title:DirectChooseSort </p>
* <p>Description:选择排序之直接选择排序 </p>
* <p>Company: </p>
* @author Sunny
* @date 2016-9-25下午3:47:49
*/
public class DirectChooseSort {
public static void main(String[] args) {
int[] arr={7,6,4,9,0,5,2,8,3,1};
sort(arr,10);
}
/**
*
* <p>MethodName:sort </p>
* <p>Description:直接选择排序算法 </p>
* <p>@param arr 数组
* <p>@param n</p> 数组元素个数
* <p>Return:void </p>
* @author Sunny
* @date 2016-9-25下午3:59:49
*/
public static void sort(int[] arr,int n){
int t,k;
for(int i=0;i<n-1;i++){
k=i;
for(int j=i+1;j<n;j++){
if(arr[j]<arr[k]) k=j;
}
if(k!=i){
t=arr[i];
arr[i]=arr[k];
arr[k]=t;
}
print(arr);
}
}
/**
*
* <p>MethodName:print </p>
* <p>Description:数组元素单行打印 </p>
* <p>@param arr</p>
* <p>Return:void </p>
* @author Sunny
* @date 2016-9-25下午4:07:52
*/
public static void print(int[] arr){
for(int element:arr){
System.out.print(element+" ");
}
System.out.println();
}
}
* <p>Title:DirectChooseSort </p>
* <p>Description:选择排序之直接选择排序 </p>
* <p>Company: </p>
* @author Sunny
* @date 2016-9-25下午3:47:49
*/
public class DirectChooseSort {
public static void main(String[] args) {
int[] arr={7,6,4,9,0,5,2,8,3,1};
sort(arr,10);
}
/**
*
* <p>MethodName:sort </p>
* <p>Description:直接选择排序算法 </p>
* <p>@param arr 数组
* <p>@param n</p> 数组元素个数
* <p>Return:void </p>
* @author Sunny
* @date 2016-9-25下午3:59:49
*/
public static void sort(int[] arr,int n){
int t,k;
for(int i=0;i<n-1;i++){
k=i;
for(int j=i+1;j<n;j++){
if(arr[j]<arr[k]) k=j;
}
if(k!=i){
t=arr[i];
arr[i]=arr[k];
arr[k]=t;
}
print(arr);
}
}
/**
*
* <p>MethodName:print </p>
* <p>Description:数组元素单行打印 </p>
* <p>@param arr</p>
* <p>Return:void </p>
* @author Sunny
* @date 2016-9-25下午4:07:52
*/
public static void print(int[] arr){
for(int element:arr){
System.out.print(element+" ");
}
System.out.println();
}
}
过程输出:
0 6 4 9 7 5 2 8 3 1
0 1 4 9 7 5 2 8 3 6
0 1 2 9 7 5 4 8 3 6
0 1 2 3 7 5 4 8 9 6
0 1 2 3 4 5 7 8 9 6
0 1 2 3 4 5 7 8 9 6
0 1 2 3 4 5 6 8 9 7
0 1 2 3 4 5 6 7 9 8
0 1 2 3 4 5 6 7 8 9