- package com.example.demo.dataStructure.sort;
- // 直接插入排序
- public class InsertSort { public static void insertSort(int[] arg) {
- for (int i=0;i<arg.length;i++) {
- int j = i;
- int temp = arg[j];
- while(j>0 && temp < arg[j-1]) {
- arg[j] = arg[j-1];
- j--;
- }
- arg[j] = temp;
- }
- }
- public static void main(String[] args) {
- int[] arg = {9,8,7,6,5,4,3,1,2};
- insertSort(arg);
- for (int i = 0;i< arg.length;i++) {
- System.out.print(arg[i]);
- }
- }
- }
来源: http://www.bubuko.com/infodetail-2765744.html