排序数据结构(用模板实现)
2013-12-07 12:18
274 查看
排序数据结构(用模板实现)
#include <iostream>
using namespace std;
#include <conio.h>
#include <algorithm>
//////////////////////////////////////////////////////////////////////////
// the bubble sort1
//////////////////////////////////////////////////////////////////////////
//template<class T>void sort(T* a,int n)
//{
// for (int i=1;i<n;i++)
// for(int j=1;j<=n-i;j++)
// if(a[j-1]>=a[j])
// swap(a[j-1],a[j]);
//
//}
//////////////////////////////////////////////////////////////////////////
// the selection sort2
//////////////////////////////////////////////////////////////////////////
//template<class T>void sort(T* a,int n)
//{
// for (int i=0;i<n-1;i++)
// {
// int min=i;
// for(int j=i+1;j<n;j++)
// if(a[j]<a[min])
// min=j;
// swap(a[min],a[i]);
// }
//}
//////////////////////////////////////////////////////////////////////////
// the insertion sort3
//////////////////////////////////////////////////////////////////////////
//template<class T>void sort(T* a,int n)
//{
// for (int i=1;i<n;i++)
// {
// T temp,j;
// temp=a[i];
// for(j=i;j>0&&a[j-1]>temp;j--)
// {
// a[j]=a[j-1];
// }
// a[j]=temp;
// }
//}
//////////////////////////////////////////////////////////////////////////
// the Merge sort4
//////////////////////////////////////////////////////////////////////////
//template<class T>void merge(T* a,int n1,int n2)
//{
// T* temp=new T[n1+n2];
// int i=0,j1=0,j2=0;
// while(j1<n1&&j2<n2)
// temp[i++]=(a[j1]<=a[n1+j2]?a[j1++]:a[n1+j2++]);
// while(j1<n1)
// temp[i++]=a[j1++];
// while(j2<n2)
// temp[i++]=(a+n1)[j2++];
// for(i=0;i<n1+n2;i++)
// a[i]=temp[i];
// delete []temp;
//}
//template<class T>void sort(T* a,int n)
//{
// if (n>1)
// {
// int n1=n/2;
// int n2=n-n1;
// sort(a,n1);
// sort(a+n1,n2);
// merge(a,n1,n2);
// }
//
//}
//////////////////////////////////////////////////////////////////////////
// the quicksort sort5
//////////////////////////////////////////////////////////////////////////
//template<class T>void quicksort(T* a,int low,int high)
//{
// if(low>=high)return;
// T pivot=a[high];
// int i=low-1;
// int j=high;
// while(i<j)
// {
// while(a[++i]<pivot);
// while(j>=0&&a[--j]>pivot);
// if(i<j)swap(a[i],a[j]);
// }
// swap(a[i],a[high]);
// quicksort(a,low,i-1);
// quicksort(a,i+1,high);
//
//}
//template<class T>void sort(T* a,int n)
//{
// quicksort(a,0,n-1);
//}
//////////////////////////////////////////////////////////////////////////
// the heapsort sort6
//////////////////////////////////////////////////////////////////////////
//template<class T>void heapsort(T* a,int k,int n)
//{
// T t=a[k];
// while (k<n/2)
// {
// int j=2*k+1;
// if(j+1<n&&a[j]<a[j+1])
// ++j;
// if(t>a[j])break;
// a[k]=a[j];
// k=j;
// }
// a[k]=t;
//}
//template<class T>void sort(T* a,int n)
//{
// for(int i=n/2-1;i>=0;i--)
// heapsort(a,i,n);
// for (int i=n-1;i>0;i--)
// {
// swap(a[0],a[i]);
// heapsort(a,0,i);
// }
//}
////////////////////////////////////////////////////////////////////////
// the shellsort sort7
////////////////////////////////////////////////////////////////////////
template<class T>void shellsort(T* a,int n)
{
int d=1,j;
while (d<n/9)
d=3*d+1;
while (d>0)
{
for (int i=d;i<n;i++)
{
T t=a[i];
j=i;
while (j>=d&&a[j-d]>t)
{
a[j]=a[j-d];
j-=d;
}
a[j]=t;
}
d/=3;
}
}
template<class T>void sort(T* a,int n)
{
shellsort(a,n);
}
template<class T>
void print(T* a,int n)
{
//cout<<a[0];
for (int i=0;i<n;i++)
{
cout<<" "<<a[i];
}
cout<<endl;
}
int main()
{
int a[]={77,44,99,66,33,55,88,22,44};
print(a,9);
sort(a,9);
print(a,9);
getch();
}
#include <iostream>
using namespace std;
#include <conio.h>
#include <algorithm>
//////////////////////////////////////////////////////////////////////////
// the bubble sort1
//////////////////////////////////////////////////////////////////////////
//template<class T>void sort(T* a,int n)
//{
// for (int i=1;i<n;i++)
// for(int j=1;j<=n-i;j++)
// if(a[j-1]>=a[j])
// swap(a[j-1],a[j]);
//
//}
//////////////////////////////////////////////////////////////////////////
// the selection sort2
//////////////////////////////////////////////////////////////////////////
//template<class T>void sort(T* a,int n)
//{
// for (int i=0;i<n-1;i++)
// {
// int min=i;
// for(int j=i+1;j<n;j++)
// if(a[j]<a[min])
// min=j;
// swap(a[min],a[i]);
// }
//}
//////////////////////////////////////////////////////////////////////////
// the insertion sort3
//////////////////////////////////////////////////////////////////////////
//template<class T>void sort(T* a,int n)
//{
// for (int i=1;i<n;i++)
// {
// T temp,j;
// temp=a[i];
// for(j=i;j>0&&a[j-1]>temp;j--)
// {
// a[j]=a[j-1];
// }
// a[j]=temp;
// }
//}
//////////////////////////////////////////////////////////////////////////
// the Merge sort4
//////////////////////////////////////////////////////////////////////////
//template<class T>void merge(T* a,int n1,int n2)
//{
// T* temp=new T[n1+n2];
// int i=0,j1=0,j2=0;
// while(j1<n1&&j2<n2)
// temp[i++]=(a[j1]<=a[n1+j2]?a[j1++]:a[n1+j2++]);
// while(j1<n1)
// temp[i++]=a[j1++];
// while(j2<n2)
// temp[i++]=(a+n1)[j2++];
// for(i=0;i<n1+n2;i++)
// a[i]=temp[i];
// delete []temp;
//}
//template<class T>void sort(T* a,int n)
//{
// if (n>1)
// {
// int n1=n/2;
// int n2=n-n1;
// sort(a,n1);
// sort(a+n1,n2);
// merge(a,n1,n2);
// }
//
//}
//////////////////////////////////////////////////////////////////////////
// the quicksort sort5
//////////////////////////////////////////////////////////////////////////
//template<class T>void quicksort(T* a,int low,int high)
//{
// if(low>=high)return;
// T pivot=a[high];
// int i=low-1;
// int j=high;
// while(i<j)
// {
// while(a[++i]<pivot);
// while(j>=0&&a[--j]>pivot);
// if(i<j)swap(a[i],a[j]);
// }
// swap(a[i],a[high]);
// quicksort(a,low,i-1);
// quicksort(a,i+1,high);
//
//}
//template<class T>void sort(T* a,int n)
//{
// quicksort(a,0,n-1);
//}
//////////////////////////////////////////////////////////////////////////
// the heapsort sort6
//////////////////////////////////////////////////////////////////////////
//template<class T>void heapsort(T* a,int k,int n)
//{
// T t=a[k];
// while (k<n/2)
// {
// int j=2*k+1;
// if(j+1<n&&a[j]<a[j+1])
// ++j;
// if(t>a[j])break;
// a[k]=a[j];
// k=j;
// }
// a[k]=t;
//}
//template<class T>void sort(T* a,int n)
//{
// for(int i=n/2-1;i>=0;i--)
// heapsort(a,i,n);
// for (int i=n-1;i>0;i--)
// {
// swap(a[0],a[i]);
// heapsort(a,0,i);
// }
//}
////////////////////////////////////////////////////////////////////////
// the shellsort sort7
////////////////////////////////////////////////////////////////////////
template<class T>void shellsort(T* a,int n)
{
int d=1,j;
while (d<n/9)
d=3*d+1;
while (d>0)
{
for (int i=d;i<n;i++)
{
T t=a[i];
j=i;
while (j>=d&&a[j-d]>t)
{
a[j]=a[j-d];
j-=d;
}
a[j]=t;
}
d/=3;
}
}
template<class T>void sort(T* a,int n)
{
shellsort(a,n);
}
template<class T>
void print(T* a,int n)
{
//cout<<a[0];
for (int i=0;i<n;i++)
{
cout<<" "<<a[i];
}
cout<<endl;
}
int main()
{
int a[]={77,44,99,66,33,55,88,22,44};
print(a,9);
sort(a,9);
print(a,9);
getch();
}
相关文章推荐
- cacti模板制作, Cacti监控一台Web服务器上多个Tomcat端口的实现
- [C++模板]Clang3.9使用变参模拟实现CheckerFn和Checker
- Spring MVC + JPA + Hibernate + GlassFish V3 + MySQL 实现J2EE应用的常用配置模板详解
- 【模板小程序】快速排序算法实现(递归+非递归)
- 实现医生工作站的检查模板功能的代码(十四)
- C# 配合 Easyui DataGrid 实现增删改查 通用模板
- SilverLight的MVVM模式中模板实现无限级树形结构(TreeView)
- 用 Flask 来写个轻博客 (14) — M(V)C_实现项目首页的模板
- 修改VS2008代码重构模板简化接口属性成员实现
- C++类模板实现链栈
- 数据离散化模板(用STL实现)
- c++ 模板 声明与实现分开编写(error LNK2001: unresolved external symbol...)
- 集合的模拟实现(函数模板)
- velocity基础教程--2自定义ResourceLoader实现字符模板
- <C/C++数据结构>双向链表(C++模板实现)
- 分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility (续2篇-模板导出综合示例)
- 事件驱动自动生成静态页面模板解决方法(一)——利用静态类和FileSystemWatcher实现模板快速索引和读取
- 递归模板实现单例模式
- asp.net实现phpcms模板功能初步研究
- C#实现通过模板自动创建Word文档的方法