标签:pac fish script name dom for factor dea author
package com.syd.sort; /** * Description: * ClassName:SelectSort * Package:com.syd.sort * Date:2018/6/8 21:59 * Author: syd-fish */ public class SelectSort { public static void main(String[] args) { int n = 100; int[] arrs = factoryRandomDigit(n); for (int i=0; i<n-1; i++){ int temp = i; for (int j=i+1; j<n; j++){ if (arrs[j]<arrs[temp]){ temp=j; } } if (temp > i){ swap(arrs,i,temp); } } for (int i=0; i<n-1; i++){ if(arrs[i]>arrs[i+1]){ System.err.println("排序错误!"); } } for (int i=0; i<n; i++){ System.err.print(arrs[i]+","); } } private static void swap(int[] arrs ,int arr1, int arr2) { int t = arrs[arr1]; arrs[arr1] = arrs[arr2]; arrs[arr2] = t; } private static int[] factoryRandomDigit(int n) { int[] arrs = new int[n]; for (int i=0 ; i<n; i++){ arrs[i] = (int) (Math.random()*n); } return arrs; } }
用idea还是不太习惯,刚开始把swap方法写成如下这样,废了好一番功夫,才查出来错误
private static void swap(int arr1, int arr2) { int t = arr1; arr1 = arr2; arr2 = t; }
标签:pac fish script name dom for factor dea author
原文地址:https://www.cnblogs.com/syd-fish-cat/p/9157833.html