数据结构之排序
学了很多算法,一直不知道怎么使用,这里就总结了一下常见了排序算法。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
//-------------------------插入排序----------------------------------
void Insert_sort(int A[],int N)
{
int i,p,temp;
for(p=1;p<N;p++){
temp=A[p];
for(i=p;i>0&&A[i-1]>temp;i--)
A[i]=A[i-1];
A[i] = temp;
}
}
//--------------------------冒泡排序----------------------------------------------
void Bubble_Sort(int A[],int N)
{
int i,j;
for(i=0;i<N-1;i++)
for(j=0;j<N-i-1;j++)
if(A[j]>A[j+1])
{
int t=A[j];
A[j]=A[j+1];
A[j+1]=t;
}
}
//-------------------------希尔排序-----------------------------------------------
void Shell_Sort(int A[],int N)
{
int D,P,i;
for(D=N/2;D>0;D/=2)
for(P=D;P<N;P++)
{
int tmp=A[P];
for(i=P;i>=D&&A[i-D]>tmp;i-=D)
A[i]=A[i-D];
A[i]=tmp;
}
}
//-------------------------归并排序 ----------------------------------------------
void merge_two(int *a,int s1,int e1,int s2,int e2,int* m1,int* m2){
int len1=e1-s1+1;
int len2=e2-s2+1;
int p1=0;
int p2=0;
int p=s1;
memcpy(m1,a+s1,sizeof(int)*len1);
memcpy(m2,a+s2,sizeof(int)*len2);
while (p1<len1&&p2<len2) {
if(m1[p1]<m2[p2]){
a[p++]=m1[p1++];
}
else{
a[p++]=m2[p2++];
}
}
while (p1<len1) {
a[p++]=m1[p1++];
}
while(p2<len2){
a[p++]=m2[p2++];
}
}
void Merge_Sort(int a[],int len){
int step=1;
int *m1=(int *)malloc(len*sizeof(int));
int *m2=(int *)malloc(len*sizeof(int));
while (step<len) {
int i,s1,e1,s2,e2;
for(i=0;(i+step-1)<(len-1);i+=2*step){
s1=i;
e1=i+step-1;
s2=e1+1;
(i+2*step-1)<(len-1)?(e2=i+2*step-1):(e2=len-1);
merge_two(a,s1,e1,s2,e2,m1,m2);
}
step=step<<1;
}
}
//--------------------------------堆排序----------------------
int heapsort(int *a,int len){
build_heap(a,len);
int tmp;
int newlen=len;
while(newlen>1){
//swap head with last
tmp=a[0];
a[0]=a[newlen-1];
a[newlen-1]=tmp;
newlen--;
//heapify new heap
heaplify(a,newlen,0);
}
return 0;
}
int build_heap(int *a,int len){
int i;
for (i=len-1; i>=0; i--) {
if(2*i+1>len-1) continue;
heaplify(a,len,i);
}
return 0;
}
int heaplify(int *a,int len, int index){
int left=2*index+1;
int right=2*index+2;
int tmp;
if (left>len-1) {
return 0;
}
else if(left==len-1){
if(a[index]<a[left]){
//swap
tmp=a[index];
a[index]=a[left];
a[left]=tmp;
}
return 0;
}
else{
if (a[index]<a[left]||a[index]<a[right]) {
if (a[left]<a[right]) {
//swap right with parent
tmp=a[index];
a[index]=a[right];
a[right]=tmp;
heaplify(a,len,right);
}
else{
//swap left with parent
tmp=a[index];
a[index]=a[left];
a[left]=tmp;
heaplify(a,len,left);
}
}
}
}
//--------------快速排序---------------------
void swap(int *a,int *b)
{
int t=*a;
*a=*b;
*b=t;
}
int Partition(int A[],int low,int high)
{
int pivot=A[low];
while(low<high){
while(low<high && A[high]>=pivot)
high--;
swap(&A[low],&A[high]);
while(low<high && A[low]<=pivot)
low++;
swap(&A[low],&A[high]);
}
return low;
}
void Qsort(int A[],int low,int high)
{
int pivot;
if(low<high){
pivot = Partition(A,low,high);
Qsort(A,low,pivot-1);
Qsort(A,pivot+1,high);
}
}
void quickSort(int A[],int N)
{
Qsort(A,0,N-1);
}
//----------使用系统的快速排序------------------
int cmp(const void *a,const void*b)
{
return *((int*)a)-*((int*)b);
}
void QuickSort(int A[],int N)
{
qsort(A,N,sizeof(A[0]),cmp);
}
void print(int A[],int N)
{
int i;
for(i=0;i<5;i++)
printf("%d ",A[i]);
printf("\n");
}
#define N 65535
int main()
{
//int A[]={3,2,6,4,5},i;
//Insert_sort(A,5);
int A[65535],i;
for(i=0;i<65535;i++)
A[i]=(int)(rand()*1000);
double start,end,cost;
start=clock();
Shell_Sort(A,N);
end=clock();
cost=end-start;
printf("%lf",cost/3600);
return 0;
}