原理
第一个元素如果大于第二个元素比较,则他们位置调换。假设有6个元素,需要经过6*6 = 36次循环。
代码
/** * 升序 * * @param numArr * @return */ private static String[] ascDetail(String[] numArr) { for (int i = 0; i < numArr.length; i++) { //最后个元素不需要调换位置 if (numArr.length == i + 1) { break; } String thisNum = numArr[i]; String nextNum = numArr[i + 1]; if (thisNum.hashCode() > nextNum.hashCode()) { numArr[i] = nextNum; numArr[i + 1] = thisNum; } } return numArr; } /** * 升序 * * @param numArr * @return */ public static String[] ascSort(String[] numArr) { if (numArr == null) { return null; } String[] result = null; for (int i = 0; i < numArr.length; i++) { result = ascDetail(numArr); } return result; }
标签:nextNum,return,String,冒泡排序,result,numArr,null From: https://www.cnblogs.com/zhougongjin/p/17205696.html