标签:
package sort; import java.util.Scanner; public class SelectionSort { public static int arr[]; static{ System.out.println("Enter the array to sort, and use ‘,‘ as a separator:"); Scanner sc=new Scanner(System.in); String s=sc.nextLine(); String n[]=s.split(","); arr=new int[n.length]; System.out.println("Before sort:"); for(int i=0;i<arr.length;i++){ arr[i]=Integer.parseInt(n[i]); System.out.print(arr[i]+" "); } System.out.println(); } public static void main(String args[]){ int m=arr.length-1; selectionSort(m); System.out.println("after sort:"); for(int i=0;i<=m;i++) System.out.print(arr[i]+" "); System.out.println(); } public static void selectionSort(int m){ int minValue=0; int minValueIndex=0; int temp=0; for(int i=0;i<=m;i++){ minValue=arr[i]; minValueIndex=i; for(int j=i+1;j<=m;j++){ if(arr[j]<minValue){ minValue=arr[j]; minValueIndex=j; } } temp=arr[i]; arr[i]=arr[minValueIndex]; arr[minValueIndex]=temp; System.out.print("Currenting sort:"); for(int k=0;k<arr.length;k++){ System.out.print(arr[k]+" "); } System.out.println(); } } }
标签:
原文地址:http://www.cnblogs.com/lvbubu/p/4681433.html