插入排序与希尔排序





package com.demo.utils;  
  
//排序  
public class Sort {  
  
    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
        int[] a = { 11, 3, 1, 5, 7, 9, 2, 4, 6, 8, 10 };  
        InsertSort(a);  
    }  
  
    // 插入排序  
    public static int[] InsertSort(int[] a) {  
  
        for (int index = 0; index < a.length; index++) {  
            // 当前指针  
            int subIndex = index;  
            int currentData = a[index]; // 等待插入的数据  
            while ((subIndex > 0) && (a[subIndex - 1] > currentData)) {  
                a[subIndex] = a[subIndex - 1];  
                subIndex--;  
            }  
            a[subIndex] = currentData; // 插入到合适的位置  
        }  
        printArray(a);  
        return a;  
    }  
  
    // 希尔排序  
    public static int[] shellSort(int[] a) {  
        int h = 1;  
        int tempData;  
  
        // 计算分成几组  
        while (h <= a.length / 3) {  
            h = h * 3 + 1;  
        }  
  
        while (h > 0) {  
            for (int pointer = h; pointer < a.length; pointer++) {  
                tempData = a[pointer];  
                while (pointer > h - 1 && a[pointer - h] >= tempData) {  
                    a[pointer] = a[pointer - h];  
                    pointer = pointer - h;  
                }  
                a[pointer] = tempData;  
            }  
            h = (h - 1) / 3;  
        }  
        printArray(a);  
        return a;  
    }  
  
    // 打印数组  
    public static void printArray(int[] a) {  
        for (int i = 0; i < a.length; i++) {  
            System.out.print(a[i] + " ");  
        }  
        System.out.println("");  
    }  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值