选择排序

 

package com.duapp.itfanr;

public class ArrayDemo {

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 printArr(int [] arr){
 for (int x = 0; x < arr.length; x++) {
 System.out.println(arr[x]);

}

 }
 public static void main(String args[]) {

int[] arr = { 1, 4, 3, 2, 5 };
 System.out.println("before sort \n") ;
 printArr(arr) ;
 selectSort(arr) ;
 System.out.println("after sort \n") ;
 printArr(arr) ;

}

}
 

你可能感兴趣的:(选择排序)