zoukankan      html  css  js  c++  java
  • 数据结构排序算法插入排序Java实现

    public class InsertDemo {

    public static void main(String args[]) {


    int[] sort ={4,2,1,3,6,5,9,8,10,7} ;
    System.out.println("haha"+sort[0]);
    System.out.println("排序前:");

    print(sort);
    shellSort(sort);
    System.out.println(" 排序后:");
    print(sort);

    }



    //直接插入排序
    public static void insertSort(int[] a) {

    for(int i=1;i<a.length;i++) {

    int temp=a[i];
    int j;
    for(j=i-1; j>=0; j--) {
    if(a[j]>temp) {
    a[j+1]=a[j];
    }
    else {
    break;
    }
    }
    a[j+1]=temp;


    }

    }

    public static void shellSort(int[] arrays) {

    //增量每次都/2
    for (int step = arrays.length / 2; step > 0; step /= 2) {

    //从增量那组开始进行插入排序,直至完毕
    for (int i = step; i < arrays.length; i++) {

    int j = i;
    int temp = arrays[j];

    // j - step 就是代表与它同组隔壁的元素
    while (j - step >= 0 && arrays[j - step] > temp) {
    arrays[j] = arrays[j - step];
    j = j - step;
    }
    arrays[j] = temp;
    }
    }

    }



    //打印
    public static void print(int[] a){
    for (int i = 0; i < a.length; i++) {
    System.out.print(a[i]+" ");
    }
    }

    }

  • 相关阅读:
    Information retrieval (IR class2)
    HTML随笔
    Evaluating Automatically Generated timelines from the Web (paper1)
    Kali 2020.1版本安装
    SystemTap
    Linux之IDIDID
    调试&内核探针
    Return-to-dl-resolve
    转载!
    一张图系列之函数重定位
  • 原文地址:https://www.cnblogs.com/ldcs/p/10710838.html
Copyright © 2011-2022 走看看