首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

排序算法-取舍排序(简单插入排序、堆排序)

2012-07-20 
排序算法---选择排序(简单插入排序、堆排序)#include stdio.hvoid simple_select(int a[], int n){int i,

排序算法---选择排序(简单插入排序、堆排序)

#include <stdio.h>void simple_select(int a[], int n){        int i,j,k,tmp;        for(i=0;i<n-1;i++)        {                k=i;                for(j=i+1;j<n;j++)                {                        if(a[j]<a[k])                                k=j;                }                if(k!=i)                {                        tmp=a[k];                        a[k]=a[i];                        a[i]=tmp;                }        }}void shift(int a[], int low, int high){        int i=low;        int j=2*i;        int tmp=a[i];        while(j<=high)        {                if(j<high && a[j]<a[j+1])                        j++;                if(tmp<a[j])                {                        a[i]=a[j];                        i=j;                        j=2*i;                }                else                        break;        }        a[i]=tmp;}/*the number of elements of a is n+1*//*a[0] is not used*/void heap_sort(int a[], int n){        int i,tmp;        for(i=n/2;i>=1;i--)                shift(a,i,n);        for(i=n;i>=2;i--)        {                tmp=a[i];                a[i]=a[1];                a[1]=tmp;                shift(a,1,n-1);        }}
?

热点排行