Java 數組 之 一維數組 插入排序算法
阿新 • • 發佈:2018-06-17
emp temp 比較 一維數組 元素 TP 數組 java In
http://www.verejava.com/?id=16992686811331
/* 用插入排序: 思路: 1. 在數組中 取未排序的新元素, 跟已經排序的元素比較, 如果新元素小於已排序的元素右移,插入新元素 */ public class InsertSort { public static void sort(int[] arrays){ for (int i = 0; i < arrays.length-1; i++) { int insertElement=arrays[i];//取未排序的新元素 int temp=i; for (int j = temp-1; j >=0; j--) { if(insertElement<arrays[j]){//如果新元素小於已排序的元素右移 arrays[j+1]=arrays[j]; temp--; } } arrays[temp]=insertElement;//插入新元素 } } public static void main(String[] args) { int[] scores={90,70,50,80,60,85}; sort(scores); for(int score:scores){ System.out.print(score+","); } } }
http://www.verejava.com/?id=16992686811331
Java 數組 之 一維數組 插入排序算法