插入排序(包括希尔排序)及其变体


package com.algorithm;


/**
* 插入排序及其变体
*
* List可转化为数组进行排序
* Object数组中的元素必须实现Comparable接口,即元素必须是可比的
*/
public class InsertSort {

/**
* 直接插入排序
*/
public static void insertSort(Object[] a){
Object cur = null; //保存当前遍历到的元素
int len = a.length;
for(int i = 1;i < len;i++){
if(((Comparable)a[i]).compareTo(a[i-1]) < 0){ //如果后一个元素小于前一个
cur = a[i];
a[i] = a[i-1];
int j;
//在前面已经有序的表中查找插入位置,并移动元素
for(j = i-2;j>=0 && ((Comparable)cur).compareTo(a[j]) < 0;j--){
a[j+1] = a[j];
}
a[j+1] = cur;
}
}
}

/**
* 二分插入排序
* 使用二分查找查找插入位置时因为前面元素都已经有序
* 减小查找插入位置的比较次数,但元素移动次数不变
*/
public static void binaryInsertSort(Object[] a){
Object cur = null;
int len = a.length;
for(int i=1;i<len;i++){
if(((Comparable)a[i]).compareTo(a[i-1]) < 0){ //后一个元素小于前一个
cur = a[i];
int low = 0; //下界
int high = i - 1;
while(low <= high){ //找插入位置的索引
int mid = (low + high) / 2;
if(((Comparable)cur).compareTo(a[mid]) < 0){
high = mid - 1;
}else{
low = mid +1;
}
}
for(int j = i -1;j > high;j--){ //把high之后的元素右移
a[j+1] = a[j];
}
a[ high + 1] = cur;
}
}
}


/**
* 2-路插入排序
* 需要一个辅助数组s,及指向s的第一个及最后一个位置的索引,first和last,
* 即从first遍历到final得到的列是有序的
* 2-路插入排序是在二分插入排序的基础上进行改进,
* 减少了排序过程中移动的元素的次数,但增加了辅助空间.
*
*/
public static void twoRoadInsertSort(Object[] a){
int len = a.length;
Object[] s = new Object[len]; //辅助数组
s[0] = a[0];
int first = len;
int last = 0;
for(int i = 1;i < len;i++){
if(((Comparable)a[i]).compareTo(s[0])<0){ //小于s[0],插入到s[0]之前,即从数组最后开始插入
int j;
for(j = first; j < len - 1 && ((Comparable)a[i]).compareTo(s[j])>0;j++){
s[j-1] = s[j];
}
s[j-1] = a[i];
first--;
}else{ //大于s[0],插入到s[0]之后
int j;
//查找插入位置并移动元素
for(j = last; j > 0 && ((Comparable)a[i]).compareTo(s[j])<0;j--){
s[j+1] = s[j];
}
s[j+1] = a[i];
last++;
}
}
System.out.println("first:"+first +" last:"+last);
//重新对a赋值
int count = 0;
for(int i = first;i < len;i++){
a[count++] = s[i];
}
for(int i=0;i<=last;i++){
a[count++] = s[i];
}
}


//test
public static void main(String[] args) {
Integer[] data = {49,38,65,97,76,13,27,49,14};
insertSort(data);
//binaryInsertSort(data);
//twoRoadInsertSort(data);
for(Integer d:data){
System.out.println(d);
}
}
}



希尔排序(改进的插入排序)

package com.sort;

/**
* 希尔排序(它也是一种插入排序)
* 原理:首先根据增量,将相隔增量位置的元素组成一个子序列,
* 对子序列进行插入排序,一趟排序后若干子序列已经有序。
* 然后减少增量,继续重复上述操作,最后一趟排序的增量必为1,
* 即对整个序列进行一次插入排序。最后一趟排序后整个列表有序。
*
* 产生希尔排序的原因:
* 1.若待排记录序列"基本有序",即i记录之前的所有记录中
* 大于i的记录较少,这时候插入排序的效率可以大大提高
* 2.列表元素较少时,插入排序的效率也比较高。
*/
public class ShellSortDemo {

/**
* 根据设置的增量多次排序
*/
public static void shellSort(Object[] a){
//最后一个增量必为1。即对整个序列进行一次插入排序
int[] dks = {5,3,1};
//进行dsk.length插入排序
for(int i=0;i<dks.length;i++){
ShellInsert(a, dks[i]);
}
}

/**
* 某一趟排序,增量为dk。
* 意思为:将相隔dk的记录组成一个子序列。
* 对子序列进行插入排序。
* 比如:增量为5时
* 初始记录 49 38 65 97 76 13 27 49 55 4
* 子序列1: 49 13
* 子序列2: 38 27
* 子序列3: 65 49
* 子序列4: 97 55
* 子序列5: 76 4
* 可对上述自序列分别进行插入排序
*/
private static void ShellInsert(Object[] a,int dk){
int len = a.length;
Object cur = null;
for(int i=dk;i<len;i++){
if(((Comparable)a[i]).compareTo(a[i-dk]) < 0){
cur = a[i];
int j;
//在左边有序表中查找插入位置,查找过程中元素右移
for(j = i -dk; j>=0 && ((Comparable)cur).compareTo(a[j]) < 0;j-=dk){
a[j+dk] = a[j];
}
a[j+dk] = cur; //找到正确插入位置
}
}
}

//just for test
public static void main(String[] args) {
Integer[] data = {49,38,65,97,76,13,27,49,55,4};
shellSort(data);
for(Integer d:data){
System.out.println(d);
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值