排序集合

#include<cstdio>

#include<cstdlib>

#include<cmath>

#include<ctime>

void c_sort(int *a,int n,int *t);//选择排序

void ins_sort(int *a,int n,int *t);//插入排序

void quick_sort(int *a,int n,int *t);//快速排序1

void p_quick_sort(int *a,int i,int j);

void heap_sort(int *f,int n);//堆排序

void build_heap(int *f,int i,int n);

void c_sort(int *a,int n,int *t)

{

    int i,j,temp,temp1,start,end;

    start=clock();

    for(i=0;i<n;i++)

    {

        temp=i;

        for(j=i+1;j<n;j++)

        {

            if(a[j]<a[temp])

            {

                temp=j;

            }

        }

        temp1=a[temp];

        a[temp]=a[i];

        a[i]=temp1;

    }

    *t=clock()-start;

}

void ins_sort(int *a,int n,int *t)

{

    int i,j,temp,start;

    start=clock();

    for(i=1;i<n;i++)

    {

        temp=a[i];

        for(j=i;j>=1;j--)

        {

            if(a[j-1]>temp)

            {

                a[j]=a[j-1];

 

            }

            else break;

        }

 

        a[j]=temp;

    }

    *t=clock()-start;

}

void quick_sort(int *a,int n,int *t)

{

    int start=clock();

    p_quick_sort(a,0,n-1);

    *t=clock()-start;

 

}

void p_quick_sort(int *a,int i,int j)

{

    int temp,index,start=i,end=j;  if(i>=j)return ;

    temp=a[i];

 

    while(i<j)

    {

        while(a[j]>=temp&&i<j)

        {

            j--;

        }

        if(i<j)

        {

            a[i]=a[j];

 

        }

        while(a[i]<temp&&i<j)

        {

            i++;

        }

        if(i<j)

        {

            a[j]=a[i];

        }

 

    }

a[i]=temp;

    p_quick_sort(a,start,i-1);

    p_quick_sort(a,i+1,end);

 

}

}
void build_heap(int *f,int i,int n)
{
int cur;
bool flag;
int temp;

flag=true;
while(flag)
{cur=i;
if(2*i+1<n&&f[2*i+1]>f[i])
{cur=2*i+1;}
if(2*i+2<n&&f[2*i+2]>f[cur])
{
cur=2*i+2;
}
flag=false;
if(cur!=i)
{
temp=f[cur];
f[cur]=f[i];
f[i]=temp;
i=cur;
flag=true;

 

}
}


}
void heap_sort(int *f,int n)
{
int i;
for(i=(n-2)/2;i>=0;i--)
build_heap(f,i,n);
int j=n,temp;
for(i=0;i<j;i++)
{
build_heap(f,0,n);
temp=f[n-1];
f[n-1]=f[0];
f[0]=temp;
n--;


}

}

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(排序)