排序算法 插入排序

来源:互联网 发布:淘宝上筒子钱真假 编辑:程序博客网 时间:2024/06/17 04:10
package com.sort.test;public class InsertSort {public static void main(String[] args) {int a[] = {49,38,65,97,76,13,27,49,78,34,12,-1,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51}; ////InsertSort(a);for (int i = 0; i < a.length; i++) {System.out.print("  "+ a[i]);}}/** * 直接插入排序 *  * int a[] = {49,38,65,97} * @param array a */public static void InsertSort(int[] a){int temp = 0;     for(int i = 1; i< a.length; i++){int j = i-1;// 当i=1 ,时候j = 0 ; temp = a[i];// 把下标1的赋值复给temp ;for(; j>=0 && a[j] > temp ; j--){ // 如果前面的值比后边的大,要重新排一下//将大于temp 的值整体后移一个单位a[j+1] = a[j];}a[j+1] = temp;}}}