zoukankan      html  css  js  c++  java
  • JAVA排序--[插入排序]

     1 package com.array;
     2 
     3 public class Sort_Insert {
     4     /*
     5      * 项目名称:插入排序 ; 
     6      * 项目要求:用JAVA对数组进行排序,并运用插入排序算法; 
     7      * 作者:Sevck;
     8      */
     9     public void sort(int arr[]) {
    10         for (int i = 1; i < arr.length; i++) {
    11             int insertval = arr[i];
    12             int index = i - 1;
    13             while (index >= 0 && insertval < arr[index]) {
    14                 // 将arr[index]向后移动
    15                 arr[index + 1] = arr[index];
    16                 index--;
    17             }
    18             // 将insertval插入适当位置
    19             arr[index + 1] = insertval;
    20         }
    21         for (int i = 0; i < arr.length; i++) {
    22             System.out.print(arr[i] + "  ");// 遍历
    23         }
    24     }
    25 
    26     public static void main(String[] args) {
    27         int arr1[] = { 7, 3, 2, 9, 15, 1, 14 };
    28         Sort_Insert a = new Sort_Insert();
    29         a.sort(arr1);
    30     }
    31 
    32 }
  • 相关阅读:
    What Kind of Friends Are You? ZOJ 3960
    博弈随笔(未完待续)
    Mergeable Stack ZOJ
    LIS ZOJ
    差分约束 HDU
    How far away ? HDU
    wya费用流
    不知道说些什么
    ext大法好啊
    bzoj2348
  • 原文地址:https://www.cnblogs.com/sevck/p/4498666.html
Copyright © 2011-2022 走看看