您的位置:首页 > 编程语言 > C语言/C++

插入排序/希尔排序/快速排序C语言实现

2015-06-16 16:28 363 查看
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void swap(int *a,int *b)
{
int tmp=*a;
*a=*b;
*b=tmp;
}

void insertSort(int *a,int n)
{
int tmp,i,j;
for(i=1;i<n;i++)
{
tmp=a[i];
for(j=i;j>0&&tmp<a[j-1];j--)//find the location to insert,the smaller
a[j]=a[j-1];
a[j]=tmp;
}
}

void shellSort(int *a,int n)
{
int i,j,incre,tmp;
for(incre=n/2;incre>0;incre/=2)
{
for(i=incre;i<n;i++)
{
tmp=a[i];
for(j=i;j>=incre;j-=incre)
{
if(tmp<a[j-incre])
a[j]=a[j-incre];
else
break;
}
a[j]=tmp;
}
}
}

int partition(int *a,int low,int high)
{
int key=a[low];
while(low<high)
{
while(low<high&&key<=a[high])
high--;
swap(&a[low],&a[high]);
while(low<high&&key>=a[low])
low++;
swap(&a[low],&a[high]);
a[low]=key;
}
return low;
}

void quickSort(int *a,int low,int high)
{
int inter;
if(low<high)
{
inter=partition(a,low,high);
quickSort(a,low,inter-1);
quickSort(a,inter+1,high);
}
}

int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int *nums=(int *)malloc(sizeof(int)*n);
for(int i=0;i<n;i++)
scanf("%d",&nums[i]);
adjustHeap(nums,n);
for(i=0;i<n;i++)
printf("%d ",nums[i]);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: