数据结构---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)
排序算法总结
选择法排序:
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;
}
一插入排序InsertionSort1基本思想每次将一个待排序的数据元素插入到前面已经排好序的数列中的适当位置使数列依然有序直到待…
数据结构各种排序算法总结计算机排序与人进行排序的不同计算机程序不能象人一样通览所有的数据只能根据计算机的quot比较quot原理在…
数据结构与算法总结姓名:**学号:**班级:12计本(2)班这个学期在老师的带领下我们学习了数据结构与算法这门课程。在本次数据结构…
排序总结排序方法平均时间最坏时间辅助存储简单排序O(n2)O(n2)O(1)快速排序O(nlogn)O(n2)O(logn)堆排序…
数据结构实验课程之手工执行一下排序算法写出每一趟排序结束时的关键码状态includeltstdiohgtincludeltstdl…
冒泡排序(BubbleSort)是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换…
排序算法总结选择法排序:for(i=0;i9;i++){max=i;for(j=i+1;j10;j++)if(a[max]a[j]…
总结C语言循环结构相应知识点1、什么叫循环结构?当条件成立的时候,执行循环体的代码,当条件不成立的时候,跳出循环,执行循环结构后面…
C语言程序设计教学基本知识点第一章C语言基本知识1C源程序的框架尽管各个C源程序的功能千变万化但框架是不变的主要有编译预处理主函数…
C语言复习资料到循环总体上必须清楚的1程序结构是三种三个循环结构择结构if和switch2读程序都要从main入口然后从最上面顺序…