直接插入排序
来源:互联网 发布:减肥书籍知乎 编辑:程序博客网 时间:2024/05/13 20:19
- Description
给定输入排序元素数目n和相应的n个元素,写出程序,利用内排序算法中直接插入排序算法进行排序,并输出排序过程中每趟及最后结果的相应序列。
- Input
共两行,第一行给出排序元素数目n,第二行给出n个元素,1≤n≤400,每个元素值范围为 [0,100000)
- Output
三个部分:
第1部分为两行,第1行输出文字“Source:”,第2行给出原始序列;
第2部分,开始输出文字“Insert Sort:”,后续输出简单选择排序过程;
第3部分,开始输出文字“Result:”,后续输出排序结果。
- Sample Input
7
48 36 68 72 12 48 2
- Sample Output
Source:
(48) 36 68 72 12 48 2
Insert Sort:
(36 48) 68 72 12 48 2
(36 48 68) 72 12 48 2
(36 48 68 72) 12 48 2
(12 36 48 68 72) 48 2
(12 36 48 48 68 72) 2
(2 12 36 48 48 68 72)
Result:
(2 12 36 48 48 68 72)
#include<stdio.h>#include<stdlib.h>int cmp(const int*a,const*b){ return *a-*b;}int main(){ int n,*a,i,j; scanf("%d",&n); a=(int*)malloc(4*n); for(i=0;i<n;i++) scanf("%d",a+i); printf("Source:\n(%d) ",a[0]); for(j=1;j<n-1;j++) printf("%d ",a[j]); printf("%d\nInsert Sort:\n",a[n-1]); for(i=1;i<n;i++) { qsort(a,i+1,4,cmp); printf("("); for(j=0;j<i;j++) printf("%d ",a[j]); printf("%d)",a[j]); if(j==n-1)printf("\n"); else {for(j++;j<n-1;j++) printf(" %d",a[j]); printf(" %d\n",a[n-1]);} } printf("Result:\n("); for(j=0;j<n-1;j++) printf("%d ",a[j]); printf("%d)\n",a[n-1]); free(a); return 0;}
0 0
- 插入排序--直接插入
- 插入排序------直接插入
- 插入排序---直接插入
- 排序:直接插入排序
- 排序---直接插入排序
- 排序 -- 直接插入排序
- 排序---直接插入排序
- 排序--直接插入排序
- 排序-直接插入排序
- 【排序】直接插入排序
- 排序--直接插入排序
- 排序:直接插入排序
- 【排序】直接插入排序
- 排序--直接插入排序
- 排序--直接插入排序
- 排序-直接插入排序
- 插入排序--直接插入排序
- 插入排序-直接插入排序
- bug笔记: 指针函参传值问题
- 简单选择排序
- 银行储蓄系统
- JVM学习笔记一:Java内存区域
- (我认为的)Java核心概要
- 直接插入排序
- Socket笔记(第二弹:Java Socket通信的基本实现)
- 杭电ACM2028java做法
- 快速排序
- 鼠标悬停在按钮上响应OnMouseHover、OnMouseMove和OnMouseLeave
- 最高的奖励
- tomcat启动Publishing failed with multiple errors
- 圆形进度条(包括仿QQ图片加载进度图)
- JSP学习笔记(三):关于服务器和SVN的区别