第十四周项目1
来源:互联网 发布:人力资源 知乎 编辑:程序博客网 时间:2024/06/07 03:20
/*
*Copyright (c)2017,烟台大学计算机与控制工程学院
*All rights reservrd.
*文件名称 :test.cpp
*作者:杜昕晔
*完成时间:2017年12月14日
*版本号:v1.0
*问题描述:插入排序之希尔排序
问题及代码:
1.希尔排序
#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
- 第十四周项目1
- 第十四周项目1
- 第十四周【项目1
- 第十四周 【项目1
- 第十四周项目1
- 第十四周项目1
- 第十四周项目1
- 第十四周项目1
- 第十四周项目1
- 第十四周--项目1-Animal
- 第十四周项目2--1
- 第十四周项目1二叉排序树
- 第十四周上机任务项目1-1
- 第十四周项目1 数组大折腾
- 第十四周上机任务项目1-2
- 第十四周 项目1抽象基类
- 第十四周 项目1--数组大闹腾
- 第十四周项目1-数组大折腾
- 16.springboot任务调度
- 内存堆栈的奇怪现象
- java -- StringBuffer、Arrays系统方法用法
- 第3条:用私有构造器或者枚举类型强化Singleton属性
- 【工具】这是一篇获取工具的帖子
- 第十四周项目1
- Mac下关于pygame安装的问题
- leetcode-216. Combination Sum III
- python matplotlib中显示中文问题
- 冒泡排序-c++代码实现
- 欢迎使用CSDN-markdown编辑器
- C/C++windows多线程
- Spring之条件注解@Conditional,条件(系统)不同注入的对象也不同。
- 磁盘文件系统