码迷,mamicode.com
首页 > 编程语言 > 详细

排序算法——希尔排序

时间:2018-06-24 19:32:08      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:version   range   for   希尔排序算法   img   算法   time   shel   代码   

一、C程序实现

/*******************************************************
* Description: 希尔排序算法
* Author: shujuxiong
* Version: 1.0
* Time: 2018-06-24
*******************************************************/
#include <stdio.h>

//函数:打印数组
void PrintDataArray(int a[], int n)
{
    for(int i=0; i<n; i++)
        printf("%d ",a[i]);
    printf("\n");
}


//希尔排序,版本1
void ShellSort1(int a[], int n)
{
    //分组
    for(int h = n/2; h >= 1; h/=2)
    {
        //直接插入排序
        for(int i = h; i < n; i++)
        {
            for(int j = i-h; j >= 0 && a[j] > a[j+h];j-=h)
            {
                int tmp = a[j+h];
                a[j+h] = a[j];
                a[j] = tmp;
            }
        }
    }
    PrintDataArray(a, n);
}


//希尔排序,版本2
void ShellSort2(int a[], int n)
{
    int h = 1;
    while(h < n/3)
        h = 3*h + 1;
    while(h >= 1)
    {
        for(int i = h; i<n; i++)
        {
            for(int j = i-h; j>=0 && a[j]>a[j+h]; j-=h)
            {
                int tmp = a[j+h];
                a[j+h] = a[j];
                a[j] = tmp;
            }
        }
        h = h/3;
    }
    PrintDataArray(a, n);
}

//测试用例
int main()
{
    int a[] = {3,1,7,5,2,4,9,6};
    int len = sizeof(a)/sizeof(a[0]);
    ShellSort1(a, len);
    ShellSort2(a, len);

    return 0;
}

 运行结果:

技术分享图片

 

二、Java程序实现

/**
 * @description: 希尔排序算法
 * @author: shujuxiong
 * @version: 1.0
 * @date: 2018-06-24
 */

public class ShellSort {
	public static void sort(int[] a)
	{
		int N = a.length;
		int h = 1;
	    while(h < N/3)
	        h = 3*h + 1;	    
	    while(h >= 1)
	    {
	        for(int i = h; i<N; i++)
	        {
	            for(int j = i-h; j>=0 && a[j]>a[j+h]; j-=h)
	            {
	                int tmp = a[j+h];
	                a[j+h] = a[j];
	                a[j] = tmp;
	            }
	        }
	        h = h/3;
	    }	    
	}
	
	
	//方法:打印数组
	private static void printDataArray(int[] a)
	{
		int N = a.length;
		for(int i = 0; i < N; i++)
			System.out.printf("%d ",a[i]);
		System.out.printf("\n");
	}
	
	
	//测试用例
	public static void main(String[] args)
	{
		int[] arr = {3,1,7,5,2,4,9,6};
		sort(arr);
		printDataArray(arr);
	}	

}

 运行结果:

技术分享图片

 

三、Python 代码实现

# -*- coding: utf-8 -*-
"""
Description: 希尔排序算法
Author: shujuxiong
Version: 1.0
Date: 2018-06-24
"""

import copy

##希尔排序
def shellSort(relist):
    N = len(relist)
    h = 1
    while (h < N/3):
        h = int(3*h + 1)
    while (h>=1):
        for i in range(h, N):
            j = i-h
            while j>=0 and relist[j+h]<relist[j]:
                tmp =relist[j+h]
                relist[j+h] = relist[j]
                relist[j] = tmp
                j-=h                 
        
        h = int(h/3)
    return relist

##测试用例
def main():
    mylist = [3,1,7,5,2,4,9,6]
    print(shellSort(copy.copy(mylist)))
    
if __name__==‘__main__‘:
    main()

运行结果:

技术分享图片

排序算法——希尔排序

标签:version   range   for   希尔排序算法   img   算法   time   shel   代码   

原文地址:https://www.cnblogs.com/shujuxiong/p/9221074.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!