package com.tju.sort;
/**
* Created by xiangyang.laixiang on 2016/8/2.
*/
public class SelectionSort {
public static int getMinKey(int a[], int count, int from){
int minKey = from;
for(int i=from+1;i<count;i++)
{
if(a[i]<a[minKey])
{
minKey = i;
}
}
return minKey;
}
public static void selectSort(int a[], int n)
{
int minKey = 0;
for (int i=0;i<n-1;i++)
{
minKey=i;
int key = getMinKey(a,n,i+1);
if(minKey != key)
{
int temp=a[minKey];
a[minKey] = a[key];
a[key]=temp;
}
}
}
public static void main(String[] args)
{
int a[]={6,1,2,7,8,9,3,6};
selectSort(a,a.length);
for (int value : a)
{
System.out.println(value);
}
}
}
网友评论