本帖最后由 韩天雷 于 2014-6-6 23:21 编辑
- package sort;
- public class SelectSort
- {
-
- public static void selectsort(int [] arr)
- {
- for (int x=0;x<arr.length-1;x++ )
- {
- for (int y=x+1;y<arr.length;y++ )
- {
- if (arr [x]< arr[y])
- {
- int temp=arr[x];
- arr[x]=arr[y];
- arr[y]=temp;
- }
- }
- }
- }
- public static void main(String[] args)
- {
- int arr[] = {1,2,4,3,5,8};
- printArray(arr);
- selectsort(arr);
- printArray(arr);
- }
- public static void printArray(int[]arr)
- {
- System.out.print("[");
- for (int x =0;x<arr.length ;x++ )
- {
- if (x!=arr.length-1)
- System.out.print (arr[x]+",");
- else
- System.out.print(arr[x]+"]");
- }
- }
-
- }
复制代码
定义arr数组 int arr[] = {1,2,3,4,5,6}; |