C语言数据结构_排序算法大全

数据结构---C语言排序算法大全

#include <stdio.h>

#include <stdlib.h>

#define M 100

void InsertionSort( int a[ ],int length )//插入排序

{

}

void BInsertSort(int a[ ],int length)//折半插入排序

{

}

void Choice(int a[ ],int length)//选择排序

{

int i,j,temp,index; int i,j,mid,temp; int low,high; for(i = 1;i < length;i++) { } low = 0; high = i-1; temp = a[i]; while(low <= high) { } for(j = i-1;j > high;j--) a[j+1] = a[j]; a[high+1] = temp; mid = (low+high)/2; if(temp < a[mid]) high = mid-1; low = mid+1; else int i,j,temp; for( i = 1;i < length;i++) { } temp = a[i]; j = i-1; while( temp < a[j] && j >= 0) { } a[j+1] = temp; a[j+1] = a[j]; j--;

{

index = i;

for(j = i + 1; j < length; j++)

if(a[j] < a[index])

index = j; temp = a[index]; a[index] = a[i]; a[i] = temp;

} }

void Effervesce(int a[ ],int length)//冒泡排序

{

{

int i,j,temp; if(start>=stop) return; i=start; j=stop; temp=a[i]; while(i<j) { while((i<j)&&(a[j])>temp) j--; if(i<j) { int i,j,temp; for(i = 1;i<length;i++) /* for(i = 1;i<length;i++) } for(j = length-1;j > i-1;j--) if(a[j]<a[j-1]) { }*/ temp = a[j]; a[j] = a[j-1]; a[j-1] = temp; for(j = 0;j<length-i;j++) if(a[j]>a[j+1]) { } temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; void QuickSort(int a[ ],int start,int stop)//快速排序

} } } i++; while((i<j)&&(a[i]<temp)) i++; if(i<j) { } a[j]=a[i]; j--; a[i]=temp; QuickSort(a,start,i-1); QuickSort(a,i+1,stop);

void Marge(int src[],int dest[],int low,int mid,int high)//归并排序 第一步 {

}

void MergeProcess(int src[],int dest[],int n,int blocksize)//归并排序 第二步 {

int i,low,mid,high; low=0; while(low+2*blocksize<=n) { } mid=low+blocksize; high=low+2*blocksize; Marge(src,dest,low,mid,high); low+=2*blocksize; int i,j,k; i=low; j=mid; k=low; while(i<mid&&j<high) { } if(i<mid) while(i!=mid) dest[k++]=src[i++]; if(j<high) while(j!=high) dest[k++]=src[j++]; else if(src[i]<src[j]) dest[k++]=src[i++]; dest[k++]=src[j++]; else

} { } else { } i=low; while(i < n ) dest[i]=src[i++]; mid = low + blocksize; high = n; Marge(src,dest,low,mid,high);

void MergeSort(int src[],int n)//归并排序▂第三步 {

}

int main( )

{

} int N,i,*p; printf("输入数组长度: \nN="); scanf("%d", &N); { } printf("输入 %d 个数簓元素:\n",N); for (i = 0; i < N; i++) scanf("%d", p+i); MergeSort(p,N); for(int i = 0;i < N;i++) printf("%d ",*(p+i)); printf("Not able to allocate memory. \n"); exit(1); int *dest=(int *)malloc(n*sizeof(int)); int blocksize=1; while(blocksize < n) { } MergeProcess(src,dest,n,blocksize); blocksize *= 2; MergeProcess(dest,src,n,blocksize); blocksize *= 2; free(dest); if ((p = (int *) malloc (N*sizeof(int))) == NULL)

 

第二篇:c语言 排序算法总结

排序算法总结

选择法排序:

for(i=0;i<9;i++)

{ max=i;

for(j=i+1;j<10;j++) if (a[max]<a[j])

max=j;

/* max为查找范围最大数所在元素下标 */ if (max != i) (if语句可省略) { temp=a[i];

a[i]=a[max];

a[max]=temp;}

}

直接排序法:

for(i=0;i<9;i++)

for( j=i+1;j<10;j++)

/* 当前元素与后续各元素逐个比较交换 if (a[i]<a[j])

{ temp=a[i];

a[i]=a[j];

a[j]=temp;

} */

冒泡法排序:

for(i=9;i>=1;i--)

{ k=i;

/* k为每轮比较范围的终止元素下标 */ for(j=0;j<=k-1;j++) if (a[j]>a[j+1])

{ temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;}

}

冒泡法排序的改进:

for(i=9;i>=1;i--)

{ swap=0;

for(j=0;j<=i-1;j++) if (a[j]>a[j+1])

{ temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

swap=1; }

if(swap==0) break;

}

相关推荐