写了一下午,加一晚上,还没有写希尔排序、基数排序、冒泡排序,由于,这些排序算法在面试中,可能经常会问道,因此,觉得自己动手写写,虽然算法思想清楚,但是仍然会碰到很多处理问题,对堆排序研究了几小时终于写好了,自己写程序粗心了...
#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
typedef vector<int> HeapType;
typedef vector<int> SqList;
void HeapSort(HeapType &H);
void Mergesort(SqList &L,int low, int high);
void UnRecurMerge(SqList &L);
void QuickSort(SqList &L);
void SelectSort(SqList &L);
void InsertSort(SqList &L);
int main()
{
int a[]={0,8,7,6,5,4,3,2,1};
HeapType v(a,a + 9);
SqList l(a+1,a+9);
//堆排序:
cout<<"堆排序:"<<endl;
HeapSort(v);
//copy(v.begin()+1,v.end(),ostream_iterator<int,char>(cout,","));
cout<<endl;
//归并排序
cout<<"归并排序(递归):"<<endl;
Mergesort(l,0,l.size()-1);
copy(l.begin(),l.end(),ostream_iterator<int,char>(cout,","));
cout<<endl;
SqList UR(a+4,a+9);
UnRecurMerge(UR);
//快速排序
SqList q(a,a+6);
QuickSort(q);
//简单选择排序
SqList s(a,a+9);
SelectSort(s);
//直接插入排序
SqList isort(a+2,a+9);
InsertSort(isort);
}
//直接插入排序
void InsertSort(SqList &L)
{
for(int i=1;i<static_cast<int>(L.size());i++)
{
if(L[i]<L[i-1])
{
for(int j=i-1;j>=0;j--)
{
if(L[j+1]<L[j])
{
swap(L[j],L[j+1]);
}
else break;
}
}
}
cout<<"直接插入排序:"<<endl;
copy(L.begin(),L.end(),ostream_iterator<int,char>(cout,","));
cout<<endl;
}
//简单选择排序
int SelectMinKey(SqList &L,int i)
{
int j=i;
for(i=i+1;i<static_cast<int>( L.size());i++)
{
if(L[i]<L[j]) j=i;
}
return j;
}
void SelectSort(SqList &L)
{
cout<<"简单选择排序:"<<endl;
for(unsigned i=1;i<L.size();i++)
{
int j =SelectMinKey(L,i);
swap(L[i],L[j]);
}
copy(L.begin(),L.end(),ostream_iterator<int,char>(cout,","));
cout<<endl;
}
//快速排序
int Partion(SqList &L,int low,int high)
{
//L[0] = L[low];
int pivotkey =L[low];
while(low<high)
{
while(low<high&&L[high]>=pivotkey) --high;
L[low]=L[high];
while(low<high&&L[low]<=pivotkey) ++low;
L[high]=L[low];
}
L[low] = pivotkey;
return low;
}
void Qsort(SqList &L,int low,int high)
{
if(low<high)
{
int pivotloc =Partion(L,low,high);
Qsort(L,low,pivotloc-1);
Qsort(L,pivotloc+1,high);
}
}
void QuickSort(SqList &L)
{
cout<<"快速排序:"<<endl;
Qsort(L,0,L.size()-1);
copy(L.begin(),L.end(),ostream_iterator<int,char>(cout,","));
cout<<endl;
}
//非递归的归并排序
void Merge(SqList& L,int low,int mid, int high);
void UnRecurMerge(SqList &L)
{
int i =0,size = L.size()-1,step = 2;
cout<<"非递归的归并算法:"<<endl;
if(size>1)
{
while(step-1<=size)
{
for(i=0;i<=size;i+=step)
{
if(i+step-1>size) Merge(L,i,i+step/2,size);
else Merge(L,i,i+step/2-1,i+step-1);
}
step=step*2;
if(step-1 >=size)
{
Merge(L,0,step/2-1,size);
}
copy(L.begin(),L.end(),ostream_iterator<int,char>(cout,","));
cout<<endl;
}
}
}
void Merge(SqList& L,int low,int mid, int high)
{
SqList L1;
int i = low,j =mid+1;
while(i<=mid&&j<=high)
{
if(L[i]<=L[j])
{
L1.push_back(L[i]);
i++;
}
else
{
L1.push_back(L[j]);
j++;
}
}
while(i<=mid)
L1.push_back(L[i++]);
while(j<=high)
L1.push_back(L[j++]);
for(i=low;i<=high;i++)
L[i]=L1[i-low];
}
//归并排序,递归形式
void Mergesort(SqList &L,int low, int high)
{
if(low<high)
{
int mid = (low+high)/2;
Mergesort(L,low,mid);
Mergesort(L,mid+1,high);
Merge(L,low,mid,high);
}
}
//堆排序,思想是:先将无序堆建成大顶堆,堆顶元素最大,将堆顶元素与无序的堆的最后一个元素对换r[1],r[n]对换
//然后堆无序堆r[1]...r[n-1]构造大顶堆,依次类推,知道只有一个元素为止
void HeapAdjust(HeapType &H ,int s,int m)
{
int rc = H[s];
for(int j =2*s;j<=m;j*=2)
{
if(j<m &&j+1<m && (H[j]<H[j+1])) ++j;
if(H[s]>=H[j]) break;
H[s] = H[j]; s =j;
}
H[s] = rc;
copy(H.begin()+1,H.end(),ostream_iterator<int,char>(cout,","));
cout<<endl;
}
void HeapSort(HeapType &H)
{
cout<<"过程:\n";
for(int i=(H.size()-1)/2 ;i>0;--i)
HeapAdjust(H,i,H.size());
cout<<"大顶堆"<<endl;
for(int i = H.size()-1;i>1;--i)
{
cout<<"倒数第"<<i<<"次:"<<endl;
swap(H[1],H[i]);
HeapAdjust(H,1,i-1);
}
}