您的位置:首页 > 其它

直接插入排序

2015-11-04 15:23 176 查看
直接插入排序:把一个无序的表进行排列成有序顺序的表。每次从无序表中选取第一个元素,插入到有序表中,使得有序表仍然有序。

源码:

package insertsort;

public class InsertSort {
public static void insertSort(int[] array) {
for (int i = 1; i < array.length; i++) {

            int j = i;

            while (j>=1 && array[j]<array[j - 1]) {

             int temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;

                j--;

            }

        }
}

public static void main(String[] args) {
int[] testArray = {23, 25, 12, 42, 35};
insertSort(testArray);
System.out.println("The result is:");
for (Integer item : testArray) {
System.out.print(item);
System.out.print(' ');
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: