插入排序/希尔排序/快速排序C语言实现
2015-06-16 16:28
363 查看
#include<stdio.h> #include<stdlib.h> #include<string.h> void swap(int *a,int *b) { int tmp=*a; *a=*b; *b=tmp; } void insertSort(int *a,int n) { int tmp,i,j; for(i=1;i<n;i++) { tmp=a[i]; for(j=i;j>0&&tmp<a[j-1];j--)//find the location to insert,the smaller a[j]=a[j-1]; a[j]=tmp; } } void shellSort(int *a,int n) { int i,j,incre,tmp; for(incre=n/2;incre>0;incre/=2) { for(i=incre;i<n;i++) { tmp=a[i]; for(j=i;j>=incre;j-=incre) { if(tmp<a[j-incre]) a[j]=a[j-incre]; else break; } a[j]=tmp; } } } int partition(int *a,int low,int high) { int key=a[low]; while(low<high) { while(low<high&&key<=a[high]) high--; swap(&a[low],&a[high]); while(low<high&&key>=a[low]) low++; swap(&a[low],&a[high]); a[low]=key; } return low; } void quickSort(int *a,int low,int high) { int inter; if(low<high) { inter=partition(a,low,high); quickSort(a,low,inter-1); quickSort(a,inter+1,high); } } int main() { int n; while(scanf("%d",&n)!=EOF) { int *nums=(int *)malloc(sizeof(int)*n); for(int i=0;i<n;i++) scanf("%d",&nums[i]); adjustHeap(nums,n); for(i=0;i<n;i++) printf("%d ",nums[i]); printf("\n"); } return 0; }
相关文章推荐
- VC++的UNICODE工程一些常用转码
- C语言学习笔记(一)——学前知识概述
- C++中深拷贝和浅拷贝
- 【深度探索C++对象模型读书笔记】【第7章】站在对象模型的尖端
- 【深度探索C++对象模型读书笔记】【第7章】站在对象模型的尖端
- 进入房间报错。
- C++ 获取文件夹下的所有文件名
- C++中的单例模式
- 顺序栈的c++实现
- C++ 标准输入输出 函数 fopen fread等系列函数使用总结
- C++读写INI配置文件的类实例
- C++ 标准IO库 iostream,fstream,sstream
- c++ new 的几个用法
- c++拷贝构造函数(深拷贝,浅拷贝)详解
- C++和extern C
- c++字符串的排列组合
- C++虚函数讲解
- C++ Custom Control控件 向父窗体发送对应的消息
- c++ vector用法
- 使用C/C++编写PHP Extension