第16周项目1-插入排序之希尔排序(2)
来源:互联网 发布:淘宝女装比较好的店铺 编辑:程序博客网 时间:2024/05/17 23:26
/** Copyright (c)2015,烟台大学计算机与控制工程学院* All rights reserved.* 文件名称:项目1-2.cbp* 作 者:李涵睿* 完成日期:2015年12月18日* 版 本 号:v1.0* 问题描述:验证希尔排序* 输入描述:无* 程序输出:测试数据*/#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void ShellSort(RecType R[],int n) //希尔排序算法{ int i,j,gap; RecType tmp; gap=n/2; //增量置初值 while (gap>0) { for (i=gap; i<n; i++) //对所有相隔gap位置的所有元素组进行排序 { tmp=R[i]; j=i-gap; while (j>=0 && tmp.key<R[j].key)//对相隔gap位置的元素组进行排序 { R[j+gap]=R[j]; j=j-gap; } R[j+gap]=tmp; j=j-gap; } gap=gap/2; //减小增量 }}int main(){ int i,n=11; RecType R[MaxSize]; KeyType a[]= {16,25,12,30,47,11,23,36,9,18,31}; for (i=0; i<n; i++) R[i].key=a[i]; printf("排序前:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); ShellSort(R,n); printf("排序后:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); return 0;}
运行结果:
2.排序中输出每一趟的中间结果
#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void ShellSort(RecType R[],int n) //希尔排序算法{ int i,j,gap,k; RecType tmp; gap=n/2; //增量置初值 while (gap>0) { for (i=gap; i<n; i++) //对所有相隔gap位置的所有元素组进行排序 { tmp=R[i]; j=i-gap; while (j>=0 && tmp.key<R[j].key)//对相隔gap位置的元素组进行排序 { R[j+gap]=R[j]; j=j-gap; } R[j+gap]=tmp; j=j-gap; } printf("gap=%d:",gap); for (k=0; k<n; k++) printf("%d ",R[k].key); printf("\n"); gap=gap/2; //减小增量 }}int main(){ int i,n=11; RecType R[MaxSize]; KeyType a[]= {16,25,12,30,47,11,23,36,9,18,31}; for (i=0; i<n; i++) R[i].key=a[i]; printf("排序前:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); ShellSort(R,n); printf("排序后:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); return 0;}
运行结果:
0 0
- 第16周项目1-插入排序之希尔排序(2)
- 第16周项目插入排序之希尔排序
- 【【第16周-内部排序项目3—插入排序之希尔排序】】
- 第十五周项目1 验证算法(2)插入排序之希尔排序
- 第16周项目1(2)希尔排序
- 第16周 项目1之希尔排序
- 第十六周 项目一 (2)插入排序之希尔排序
- 第十六周 项目一(2).插入排序之希尔排序
- 第16周项目1-插入排序之直接插入排序(1)
- 第16周项目插入排序之折半插入排序
- 第十五周项目5—插入排序之希尔排序
- 第十六周--数据结构--项目一-- 插入排序之希尔排序
- 第15周 项目1-希尔排序
- 第十六周 插入排序之希尔排序
- 插入排序之希尔(shell)排序
- 第十六周项目1-验证算法插入排序之希尔排序
- 第16周项目1-验证算法(2)希尔排序
- 第16周项目1 验证算法(2)希尔排序
- 二分查找法
- 十一周 项目一 二叉树算法验证(3)
- 【项目 - B-树的基本操作】
- 第16周项目1-验证算法(2)希尔排序
- linux相关问题解决方案
- 第16周项目1-插入排序之希尔排序(2)
- 数据结构实践——是否二叉排序树?
- /taskArtifacts/cache.properties (No such file or directory)
- 第十四周 排序二叉树中查找的路径 (补)
- GCD学习
- 第13周项目验证算法(4)
- 十一周 项目一 二叉树算法验证(4)
- html修炼养成--关于在swiper中添加动画的方法
- 第15周实践项目一(2)---直接插入排序