插入排序(Java实现)

来源:互联网 发布:一个淘宝店铺卖多少钱 编辑:程序博客网 时间:2024/04/29 01:56

插入排序

public class InsertSort {public static void main(String[] args) {int[] a = {3,2,0,9,5,1,6,8,7};insertSort(a);for(int i =0 ;i<a.length;i++) {System.out.print(a[i]+" ");}}private static void insertSort(int[] a) {int i,j;int temp;for(i = 1; i<a.length;i++) {temp = a[i];j = i;if(a[j-1] > temp) {while(j - 1 >= 0 && a[j-1]>temp) {a[j] = a[j-1];j--;}}a[j] = temp;}}}


原创粉丝点击