现在的位置: 首页 > 综合 > 正文

选择排序

2018年05月23日 ⁄ 综合 ⁄ 共 481字 ⁄ 字号 评论关闭

 

package select;
public class Select {
	//交换
			void Swap(int a[],int x,int y){
				int temp = a[x];
					a[x] = a[y];
					a[y] = temp;
					
			}
			//打印出
			void Print(int a[]){
				for(int x: a){
					System.out.print(x);
				}
			}
	public void SelectSort(int a[]) {
		for(int i=0;i<a.length-1;i++){
			int min = i;
			for(int j=i+1;j<a.length;j++){
				if(a[min]>a[j]){
					min = j;
				}
			}
			if(min!=i){
				Swap(a, min, i);
			}		
		}
		Print(a);
	}
}
//test
package test;
import select.Select;
public class Test {

	public static void main(String[] args) {
		int []array ={9,1,5,8,3,7,4,6,2};
		Select sort = new Select();		
		sort.SelectSort(array);
		
	}

}

【上篇】
【下篇】

抱歉!评论已关闭.