65,211
社区成员
发帖
与我相关
我的任务
分享
// 看看这个方法吧,那里再开辟空间啊
template <typename T>
void Sort<T>::Sift(T a[],int k,int m) // 建立堆
{
int i,j;
T temp;
if(k==m) // 叶子不需要建立堆
return ;
i=k,j=2*i; // 从右下开始建立堆
while(j<=m)
{
if(j<m&&a[j]<a[j+1]) // 从左右孩子找出最小的元素
j++;
if(a[i]>a[j])
break;
else // 实现父与子的交换
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
i=j; // 建立上一层的堆
j=2*i;
}
}
}
template <typename T>
void Sort<T>::HeapSort(T a[],int size) // 堆排序
{
T temp;
int i=size/2;
for(;i>=0;i--) // 建立堆
Sift(a,i,size-1);
for(i=0;i<size;i++) // 交换堆的根与最末一个元素
{
temp=a[0];
a[0]=a[size-i-1];
a[size-i-1]=temp;
Sift(a,0,size-i-2); // 进行下次的建立堆
}
}#ifndef _Sort_h
#define _Sort_h
template <typename T>
class Sort // 排序类
{
public: // 静态方法
static void InsertSort(T a[], const int size);// 插入排序
static void ShellSort (T a[], const int size);// 希尔排序
static void BubbleSort(T a[], const int size);// 冒泡排序
static void QuickSort (T a[], const int first, const int end);
// 快速排序
static void SelectSort(T a[], const int size);// 选择排序
static void HeapSort (T a[], const int size);// 堆排序
static void MergeSort (T a[], const int size);// 归并排序
private: // 划分区域
static int Divide_Part (T a[], int first, int end);
// 一下是归并排序的内部方法
static void Sift (T a[], int k , int m );
static void MergeSort_In (T a[], T ta[], int low, int high);
static void Merge (T a[], T ta[], int low, int mid, int high);
};
template <typename T>
void Sort<T>::InsertSort(T a[],const int size)
{
int i;
int j; // 定义局部变量
T temp;
for(i=1;i<size;i++) // 插入排序
{
temp=a[i]; // 哨兵
for(j=i-1;a[j]>temp&&j>=0;j--)
a[j+1]=a[j];
a[j+1]=temp; // 插入到合适的位置
}
}
template <typename T>
void Sort<T>::ShellSort(T a[],const int size)
{
int d; // 定义局部变量
int i;
T temp;
int j;
for(d=size/2;d>=0;d--) // 分组进行插入排序
{
for(i=d;i<size;i++) // 插入排序,注意步长为d
{
temp=a[i];
for(j=i-d;a[j]>temp&&j>=0;j=j-d)
a[j+d]=a[j];
a[j+d]=temp; // 将元素插入合适的位置
}
}
}
template <typename T>
void Sort<T>::BubbleSort(T a[],const int size)
{
int i,j; // 定义局部变量
T temp;
for(i=0;i<size;i++) // 采用冒泡排序
{
for(j=i+1;j<size;j++)
{
if(a[i]>a[j]) // 交换元素
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
template <typename T>
int Sort<T>::Divide_Part(T a[],int first,int end)
{
int i=first,j=end-1; // 快排,采用划分区域,找到元素合适的位置
T temp;
while(i<j)
{
while(i<j&&a[i]<a[j]) // 右指针左移,直到小于左指针的元素
j--;
if(i<j) // 交换元素
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
i++; // 左指针右移动
}
while(i<j&&a[i]<a[j]) // 左指针右移,直到大于右指针的元素
i++;
if(i<j) // 交换元素
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
j--; // 右指针左移
}
}
return i; // 返回元素找到的合适的位置
}
template <typename T>
void Sort<T>::QuickSort(T a[], const int first,const int end)
{
int mid;
if(first<end) // 进行快速排序
{
mid=Divide_Part(a,first,end); // 第一次划分区域
QuickSort(a,first,mid-1); // 递归快排左区域
QuickSort(a,mid+1,end); // 递归快排右区域
}
}
template <typename T>
void Sort<T>::SelectSort(T a[],const int size)
{
int i,j;
T temp;
for(i=0;i<size;i++) // 每次选择最小的元素放到最前面
{
for(j=i;j<size;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
template <typename T>
void Sort<T>::Sift(T a[],int k,int m) // 建立堆
{
int i,j;
T temp;
if(k==m) // 叶子不需要建立堆
return ;
i=k,j=2*i; // 从右下开始建立堆
while(j<=m)
{
if(j<m&&a[j]<a[j+1]) // 从左右孩子找出最小的元素
j++;
if(a[i]>a[j])
break;
else // 实现父与子的交换
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
i=j; // 建立上一层的堆
j=2*i;
}
}
}
template <typename T>
void Sort<T>::HeapSort(T a[],int size) // 堆排序
{
T temp;
int i=size/2;
for(;i>=0;i--) // 建立堆
Sift(a,i,size-1);
for(i=0;i<size;i++) // 交换堆的根与最末一个元素
{
temp=a[0];
a[0]=a[size-i-1];
a[size-i-1]=temp;
Sift(a,0,size-i-2); // 进行下次的建立堆
}
}
template<class T>
void Sort<T>::MergeSort(T a[], const int size) // 归并排序
{
T *ta;
ta=new T[size]; // 开辟同样的空间
MergeSort_In(a, ta, 0, size-1); // 堆排序
delete []ta; // 回收空间
}
template<class T>
void Sort<T>::MergeSort_In(T a[], T ta[], int low, int high)
{
int mid;
if (low<high) // 开始归并排序
{
mid=(low+high)/2; // 取中间值
MergeSort_In(a, ta, low, mid); // 递归左区域
MergeSort_In(a, ta, mid+1, high); // 递归右区域
Merge(a, ta, low, mid, high); // 合并左右区域
}
}
template<class T>
void Sort<T>::Merge(T a[], T ta[], int low, int mid, int high)
{
int i=low, j=mid+1, k=low;
while (i<=mid && j<=high)
{
if (a[i]<=a[j])
ta[k++]=a[i++];
else
ta[k++]=a[j++];
}
if (i>mid)
{
while (j<=high)
ta[k++]=a[j++];
}
else
{
while (i<=mid)
ta[k++]=a[i++];
}
for (k=low; k<=high; ++k)
a[k]=ta[k];
}
#endif __Sort_h
// 送给你本人的多年的积累吧,贴出了你需要的部分了
#include <iostream>
#include "Sort.h"
#include "time.h"
using namespace std;
void test_Sort();
int main()
{
//test_linkList();
//test_Seqstack();
/*test_Seqlist();*/
/*test_Linkstack();*/
/*test_Seqqueue();*/
//test_Linkqueue();
/*test_Cirlist();*/
//test_Binarytree();
test_Sort();
}
void test_Sort()
{
int a[1000];
srand(NULL);
for(int i=0;i<sizeof(a)/sizeof(*a);i++)
{
a[i]=rand()%1000000;
}
clock_t begin,end;
begin=clock();
Sort<int>::HeapSort(a,sizeof(a)/sizeof(*a));
end=clock();
//Sort<int>::SelectSort(a,sizeof(a)/sizeof(*a));
//Sort<int>::QuickSort(a,0,sizeof(a)/sizeof(*a));
/*Sort<int>::BubbleSort(a,sizeof(a)/sizeof(*a));*/
/*Sort<int>::ShellSort(a,sizeof(a)/sizeof(*a));*/
/*Sort<int>::InsertSort(a,sizeof(a)/sizeof(*a));*/
/*Sort<int>::MergeSort(a,sizeof(a)/sizeof(*a));*/
for(int i=0;i<sizeof(a)/sizeof(*a);i++)
std::cout<<a[i]<<" ";
std::cout<<std::endl;
cout<<end-begin<<endl;
}