排序算法---选择排序(简单插入排序、堆排序)
#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); } }
相关推荐
Jasmineyaoyao 2020-06-16
Joymine 2020-06-16
清溪算法君老号 2020-06-06
清溪算法君老号 2020-06-01
randy0 2020-11-17
lixiaotao 2020-10-07
美丽的泡沫 2020-09-08
nongfusanquan0 2020-08-18
hang0 2020-08-16
earthhouge 2020-08-15
算法改变人生 2020-07-28
troysps 2020-07-19
Broadview 2020-07-19
chenfei0 2020-07-18
风吹夏天 2020-07-07
yangjingdong00 2020-07-05
数据与算法之美 2020-07-05
shawsun 2020-07-04