class SelectSort { public static void main (String[] args) { int[] a = {3, 9,1, 8, 2, 4, 5, 6, 7}; print(a); System.out.println("After SelectSort:"); selectSort(a); print(a); } private static void print(int[] a) { for(int i=0; i<a.length; ++i) { System.out.print(a[i] + " "); } System.out.println(); } // private static void selectsort(int[] a) { // for(int i=0; i<a.length-1; ++i) { // for(int j=i+1; j<a.length; ++j){ // if(a[j] < a[i]) { // int tmp = a[j]; // a[j] = a[i]; // a[i] = tmp; // } // } // } // } // Improved version private static void selectSort(int[] a) { int min, tmp; for(int i=0; i<a.length-1; ++i) { min = i; for(int j=i+1; j<a.length; ++j){ if(a[j] < a[min]) { min = j; } } tmp = a[min]; a[min] = a[i]; a[i] =tmp; } } }
SelectSort.java
最新推荐文章于 2025-08-06 16:23:50 发布