几种排序算法归类

中午看了下腾讯实习生的笔试题,又考到了排序算法,虽然STL已经有Sort API,还是自己写了几种排序方法。。。

源代码:

#include<iostream>
#include<stdio.h>
using namespace std;
int num[5]={1,3,2,4,2};
int temp[5]={1,3,2,4,2};
int insert(int *p,int len);
void ShellInsert(int *p,int len,int delta);
void ShellSort(int *p,int len);
void BubbleSort(int *p,int len);
void SelectSort(int *p,int len);
void qsort(int *num,int low,int high);
int onestep(int *num,int low,int high);
int main()
{
	insert(num,5);
	for(int i=0;i<5;i++)
		printf("%d",num[i]);
	printf("\n");
	for(int i=0;i<5;i++)
		num[i]=temp[i];
	ShellSort(num,5);
	for(int i=0;i<5;i++)
		printf("%d",num[i]);
	printf("\n");
	for(int i=0;i<5;i++)
		num[i]=temp[i];
	BubbleSort(num,5);
	for(int i=0;i<5;i++)
		printf("%d",num[i]);
	printf("\n");
	for(int i=0;i<5;i++)
		num[i]=temp[i];
	SelectSort(num,5);
	for(int i=0;i<5;i++)
		printf("%d",num[i]);
	printf("\n");
	for(int i=0;i<5;i++)
		num[i]=temp[i];
	qsort(num,0,4);
	for(int i=0;i<5;i++)
		printf("%d",num[i]);
	printf("\n");
	for(int i=0;i<5;i++)
		num[i]=temp[i];
}
int insert(int *p,int len)//insertsort
{
	for(int i=1;i<len;i++)
	{
		for(int j=0;j<i;j++)
		{
			if(p[j]>p[i])
			{
				int temp=p[i];
				for(int k=i;k>j;k--)
					p[k]=p[k-1];
				p[j]=temp;
				break;
			}   
		}
	}
	return 1;
}
void ShellInsert(int *p,int len,int delta)
{
	for(int i=delta;i<len;i=i++)
	{
		int first=i%delta;
		for(int j=first;j<i;j=j+delta)
		{
			if(p[j]>p[i])
			{
				int temp=p[i];
				for(int k=i;k>j;k-=delta)
					p[k]=p[k-delta];
				p[j]=temp;
				break;
			}
		}
	}
}
void ShellSort(int *p,int len)//xi'er  
{
	int delta[]={4,2,1};   //zengliang shuzu
	for(int i=0;i<3;i++)
		ShellInsert(p,len,delta[i]);
}
void BubbleSort(int *p,int len)
{
	for(int i=0;i<len;i++)
		for(int j=1;j<len-i;j++)
			if(p[j]<p[j-1])
			{
				int temp;
				temp=p[j-1];
				p[j-1]=p[j];
				p[j]=temp;
			}
}
void SelectSort(int *p,int len)
{
	for(int i=0;i<len-1;i++)
		for(int j=i+1;j<len;j++)
			if(p[j]<p[i])
			{
				int temp;
				temp=p[j];
				p[j]=p[i];
				p[i]=temp;
			}
}
void qsort(int *num,int low,int high)
{
	if(low<high)
	{
		int post=onestep(num,low,high);
		qsort(num,low,post-1);
		qsort(num,post+1,high);
	}

}
int onestep(int *num,int low,int high)
{
	int y=num[low];
	while(low<high)
	{
		while(low<high&&num[high]>y)
			high--;
		if(low<high)
		{
			num[low]=num[high];
			low++;
		}
		while(low<high&&num[low]<y)
			low++;
		if(low<high)
		{
			num[high]=num[low];
			high--;
		}
	}
	num[low]=y;
	return low;
}

堆排序:

#include "iostream"   
using namespace std;  
int heap_sort(int *a,int n);  
int jianchudui(int *a,int n);  
int shan_x(int *a,int i,int n);  
int main()  
{  
    int n;  
    int a[10000];  
    cin>>n;  
    for(int i=1;i<=n;i++)  
        cin>>a[i];  
    heap_sort(a,n);  
    for(int i=1;i<=n;i++)  
        cout<<a[i]<<" ";  
}  
int heap_sort(int *a,int n)  
{  
    jianchudui(a,n);          // 建初堆(最大堆)   
    for(int i=n;i>=2;i--)  
    {  
        int temp;  
        temp=a[1];  
        a[1]=a[i];  
        a[i]=temp;  
        shan_x(a,1,i-1);     // 把a[1]元素放到最后,再在i-1个元素间将最大的数放到a[1];   
    }  
    return 0;  
}  
int jianchudui(int *a,int n)  
{  
    for(int i=n/2;i>=1;i--)  
    {  
        shan_x(a,i,n);  //从n/2到第1个元素依次筛选   
    }  
    return 0;  
}  
int shan_x(int *a,int i,int n)  
{  
    int x=a[i];  
    int flag=1;  
    while(flag!=0)  
    {  
        if(i*2<n)         //如果i节点有左孩子和有孩子   
        {  
            if(a[i*2]>x||a[i*2+1]>x)  
            {  
                if(a[i*2]>=a[i*2+1])  
                {  
                    a[i]=a[i*2];  
                    i=i*2;  
                }  
                else   
                {  
                    a[i]=a[i*2+1];  
                    i=i*2+1;  
                }  
            }  
            else   
            {  
                flag=0;  
            }  
        }  
        else if(i*2==n)   //如果树形结构有一个节点只有左孩子,那么i就是该节点   
        {  
            if(a[i*2]>x)  
            {  
                a[i]=a[i*2];  
                i=i*2;  
            }  
            else flag=0;  
        }  
        else   
            flag=0;  
    }  
    a[i]=x;  
    return 0;  
}


 

复杂度:               O(nlogn) {归并排序,快速排序,堆排序}

                            O(n^2)        {选择排序,插入排序,冒泡排序}

                            O(n^1.5)     {希尔排序}

稳定性:            稳定排序      {冒泡排序,归并排序,插入排序}

                            非稳定排序 { 希尔排序,选择排序,堆排序,快速排序}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值