几种排序算法归类
来源:互联网 发布:hdmi传输数据 编辑:程序博客网 时间:2024/05/21 08:07
中午看了下腾讯实习生的笔试题,又考到了排序算法,虽然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 shuzufor(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) {希尔排序}
稳定性: 稳定排序 {冒泡排序,归并排序,插入排序}
非稳定排序 { 希尔排序,选择排序,堆排序,快速排序}
- 几种排序算法归类
- 排序算法归类
- 常见排序算法--实现(归类)
- 排序算法稳定性和不稳定性归类
- 几种排序算法
- 几种排序算法!
- 几种排序算法
- 几种排序算法
- 几种排序算法
- 几种排序算法
- 几种排序算法
- 几种排序算法
- 几种排序算法
- 几种排序算法
- 几种排序算法
- 几种排序算法
- 几种排序算法
- 几种排序算法
- Oracle用sql修改表名
- App.config链接Microsoft SQL Server 2005数据库
- Android自定义View实现转盘旋转的效果
- shell cut命令
- hdu2821 Pusher 深搜
- 几种排序算法归类
- test
- 骨牌覆盖
- android基础学习(2)-------修改TextView字体颜色和背景颜色
- Jquery UI -- MiniUI 资料
- cocos2d-x处理点击事件的机制
- win7硬盘安装过程图解
- 在不支持AirDrop的Mac上开启和使用AirDrop的方法
- Winform开发中手写签名的实现