交换排序
void maopao(int * a,int n){
int i,j,temp;
for(i = 0; i < n-1; i++){
for(j = i+1;j < n; j++){
if(a[i]>a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
冒泡排序效果.gif(网络)
void kuaisuin(int * a,int start,int end){
int m=start,n=end,k = a[(start+end)/2];
int temp;
do{
while( m<end && a[m]<k){
m++;
}
while(n>start && a[n]>k){
n--;
}
if(m <= n){
temp = a[m];
a[m] = a[n];
a[n] = temp;
m++;
n--;
}
}while(m <= n);
if(m < end){
kuaisuin(a,m,end);
}
if(n > start){
kuaisuin(a,start,n);
}
}
快速排序效果.gif(网络)
插入排序
void charu(int * a,int n){
int i,j,temp;
for(i = 1; i< n; i++){
temp = a[i];
for(j = i-1;j >= 0 && a[j] > temp; j--){
a[j+1] = a[j];
}
a[j+1] = temp;
}
}
void shell(int *a,int n) {
int i,j,k,x;
k=n/2; /*间距值*/
while(k>=1) {
for(i = k; i < n; i++) {
x = a[i];
j = i-k;
while(j>=0 && x<a[j]) {
a[j+k] = a[j];
j -= k;
}
a[j+k] = x;
}
k /= 2; /*缩小间距值*/
}
}
Shell排序效果.gif(网络)
选择排序
void xuanze(int * a,int n){
int i,j,temp,k;
for(i = 0; i < n-1; i++){
k = i;
for(j = i+1;j < n; j++){
if(a[k]>a[j]){
k = j;
}
}
if(k != i){
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}
void HeapAdjust(int H[],int s, int length)
{
int tmp = H[s];
int child = 2*s+1;
while (child < length) {
if(child+1 <length && H[child]<H[child+1]) {
++child ;
}
if(H[s]<H[child]) {
H[s] = H[child];
s = child;
child = 2*s+1;
} else {
break;
}
H[s] = tmp;
}
}
void BuildingHeap(int H[], int length)
{
int i;
for (i = (length -2) / 2 ; i >= 0; --i)
HeapAdjust(H,i,length);
}
void HeapSort(int H[],int length)
{
int i,temp;
BuildingHeap(H, length);
for (i = length - 1; i > 0; --i)
{
temp = H[i]; H[i] = H[0]; H[0] = temp;
HeapAdjust(H,0,i);
}
}
堆排序效果.gif(网络)
网友评论