所有的排序c语言实现
2013-12-08 15:06
267 查看
排序主要有两种,一种是n平方复杂度的,一种是nlogn或者n根号n的。
n平方复杂度的主要是一个一个将有序区间缩小,而nlogn或者根号n则是分区间的增大有序序列。
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <math.h>
//在linux中,rand函数的最大值是32位字节,windows是16位字节
void swap(int32_t *a,int32_t *b)
{
int32_t temp = *a;
*a = *b;
*b = temp;
}
void print(int32_t a[],int32_t length)
{
int32_t i;
for(i=0;i<length;i++)
{
printf("%d\n",a[i]);
}
}
//重新给数组赋予随机值
void assign(int a[],int length)
{
int32_t i;
for(i=0;i<length;i++)
a[i] = rand();
}
void dubble(int32_t a[],int32_t length)
{
_Bool flags = 0;
int32_t i,j;
for(i=0;i<length-1&&!flags;i++)
{
flags = 1;
for(j=length-1;j>0;j--)
if(a[j]<a[j-1])
{
swap(&a[j],&a[j-1]);
flags = 0;
}
}
}
void select(int32_t a[],int32_t length)
{
int32_t i,j;
int32_t min;
for(i=0;i<length-1;i++)
{
min = i;
for(j=i+1;j<length;j++)
{
if(a[j]<a[min])
min = j;
}
if(min!=i)
swap(&a[i],&a[min]);
}
}
void insert(int32_t a[],int32_t length)
{
int32_t i,j,temp;
for(i=1;i<length;i++)
{
if(a[i]<a[i-1])
{
temp = a[i];
for(j=i-1;j>=0&&a[j]>temp;j--)
a[j+1] = a[j];
a[j+1] = temp;
}
}
}
void shell(int32_t a[],int32_t length)
{
int32_t increment=length/2;
int32_t i,j,temp;
while(increment>=1)
{
for(i=increment;i<length;i++)
{
if(a[i]<a[i-increment])
{
temp = a[i];
for(j=i-increment;j>=0&&a[j]>temp;j-=increment)
a[j+increment] = a[j];
a[j+increment] = temp;
}
}
increment/=2;
}
}
//由于堆排序涉及到*2,所以应当用1
void heapadjust(int32_t a[],int32_t low,int32_t high)
{
int32_t i,j,temp;
temp = a[low-1];
for(j=2*low;j<=high;j*=2)
{
if(j<high&&a[j-1]<a[j])
j++;
if(temp>a[j-1])
break;
a[low-1] = a[j-1];
low = j;
}
a[low-1] = temp;
}
void heap(int32_t a[],int32_t length)
{
int32_t i,j;
for(i=length/2;i>0;i--)
heapadjust(a,i,length);
for(i=length;i>1;i--)
{
swap(&a[0],&a[length-1]);
heapadjust(a,1,i-1);
}
}
void Merge(int32_t a[],int32_t low,int32_t mid,int32_t high)
{
int32_t i,j,k;
int32_t *temp;
temp = (int32_t *)malloc((high-low+1)*sizeof(int32_t));
i=low;
j=mid+1;
k=0;
while(i<=mid&&j<=high)
{
if(a[i]>a[j])
temp[k++] = a[j++];
else
temp[k++] = a[i++];
}
while(i<=mid)
temp[k++] = a[i++];
while(j<=high)
temp[k++] = a[j++];
for(i=low,j=0;j<k;j++,i++)
a[i++] = temp[j++];
free(temp);
}
void Msort(int32_t a[],int32_t low,int32_t high)
{
int32_t i,j,mid;
if(high>low)
{
mid = (high+low)/2;
Msort(a,low,mid);
Msort(a,mid+1,high);
Merge(a,low,mid,high);
}
}
void mergesort(int32_t a[],int32_t length)
{
Msort(a,0,length-1);
}
int32_t Partition(int32_t a[],int32_t low,int32_t high)
{
int32_t piv = a[low];
while(low<high)
{
while(high>low&&a[high]>piv)
high--;
a[low] = a[high];
while(low<high&&a[low]<piv)
low++;
a[high] = a[low];
}
a[low] = piv;
return low;
}
void Qsort(int32_t a[],int32_t low,int32_t high)
{
int32_t piv;
if(low<high)
{
piv = Partition(a,low,high);
Qsort(a,low,piv-1);
Qsort(a,piv+1,high);
}
}
void quicksort(int32_t a[],int length)
{
Qsort(a,0,length-1);
}
int main()
{
//init array
int32_t a[10]={0,4,6,8,2,3,5,9,7,1};
assign(a,10);
//print
printf("start:\n");
print(a,10);
//sort
dubble(a,10);
//print
printf("after sort:\n");
print(a,10);
return 0;
}
n平方复杂度的主要是一个一个将有序区间缩小,而nlogn或者根号n则是分区间的增大有序序列。
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <math.h>
//在linux中,rand函数的最大值是32位字节,windows是16位字节
void swap(int32_t *a,int32_t *b)
{
int32_t temp = *a;
*a = *b;
*b = temp;
}
void print(int32_t a[],int32_t length)
{
int32_t i;
for(i=0;i<length;i++)
{
printf("%d\n",a[i]);
}
}
//重新给数组赋予随机值
void assign(int a[],int length)
{
int32_t i;
for(i=0;i<length;i++)
a[i] = rand();
}
void dubble(int32_t a[],int32_t length)
{
_Bool flags = 0;
int32_t i,j;
for(i=0;i<length-1&&!flags;i++)
{
flags = 1;
for(j=length-1;j>0;j--)
if(a[j]<a[j-1])
{
swap(&a[j],&a[j-1]);
flags = 0;
}
}
}
void select(int32_t a[],int32_t length)
{
int32_t i,j;
int32_t min;
for(i=0;i<length-1;i++)
{
min = i;
for(j=i+1;j<length;j++)
{
if(a[j]<a[min])
min = j;
}
if(min!=i)
swap(&a[i],&a[min]);
}
}
void insert(int32_t a[],int32_t length)
{
int32_t i,j,temp;
for(i=1;i<length;i++)
{
if(a[i]<a[i-1])
{
temp = a[i];
for(j=i-1;j>=0&&a[j]>temp;j--)
a[j+1] = a[j];
a[j+1] = temp;
}
}
}
void shell(int32_t a[],int32_t length)
{
int32_t increment=length/2;
int32_t i,j,temp;
while(increment>=1)
{
for(i=increment;i<length;i++)
{
if(a[i]<a[i-increment])
{
temp = a[i];
for(j=i-increment;j>=0&&a[j]>temp;j-=increment)
a[j+increment] = a[j];
a[j+increment] = temp;
}
}
increment/=2;
}
}
//由于堆排序涉及到*2,所以应当用1
void heapadjust(int32_t a[],int32_t low,int32_t high)
{
int32_t i,j,temp;
temp = a[low-1];
for(j=2*low;j<=high;j*=2)
{
if(j<high&&a[j-1]<a[j])
j++;
if(temp>a[j-1])
break;
a[low-1] = a[j-1];
low = j;
}
a[low-1] = temp;
}
void heap(int32_t a[],int32_t length)
{
int32_t i,j;
for(i=length/2;i>0;i--)
heapadjust(a,i,length);
for(i=length;i>1;i--)
{
swap(&a[0],&a[length-1]);
heapadjust(a,1,i-1);
}
}
void Merge(int32_t a[],int32_t low,int32_t mid,int32_t high)
{
int32_t i,j,k;
int32_t *temp;
temp = (int32_t *)malloc((high-low+1)*sizeof(int32_t));
i=low;
j=mid+1;
k=0;
while(i<=mid&&j<=high)
{
if(a[i]>a[j])
temp[k++] = a[j++];
else
temp[k++] = a[i++];
}
while(i<=mid)
temp[k++] = a[i++];
while(j<=high)
temp[k++] = a[j++];
for(i=low,j=0;j<k;j++,i++)
a[i++] = temp[j++];
free(temp);
}
void Msort(int32_t a[],int32_t low,int32_t high)
{
int32_t i,j,mid;
if(high>low)
{
mid = (high+low)/2;
Msort(a,low,mid);
Msort(a,mid+1,high);
Merge(a,low,mid,high);
}
}
void mergesort(int32_t a[],int32_t length)
{
Msort(a,0,length-1);
}
int32_t Partition(int32_t a[],int32_t low,int32_t high)
{
int32_t piv = a[low];
while(low<high)
{
while(high>low&&a[high]>piv)
high--;
a[low] = a[high];
while(low<high&&a[low]<piv)
low++;
a[high] = a[low];
}
a[low] = piv;
return low;
}
void Qsort(int32_t a[],int32_t low,int32_t high)
{
int32_t piv;
if(low<high)
{
piv = Partition(a,low,high);
Qsort(a,low,piv-1);
Qsort(a,piv+1,high);
}
}
void quicksort(int32_t a[],int length)
{
Qsort(a,0,length-1);
}
int main()
{
//init array
int32_t a[10]={0,4,6,8,2,3,5,9,7,1};
assign(a,10);
printf("start:\n");
print(a,10);
//sort
dubble(a,10);
printf("after sort:\n");
print(a,10);
return 0;
}
相关文章推荐
- 运维入门
- 动态清空 nohup 输出文件
- install scrapy with pip and easy_install
- 堆排序
- Linux Shell常用技巧
- Shell编程的一些知识集合
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构揭秘一
- 关于C语言中参数的传值问题
- linux Shell学习笔记第五天
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- Perl 获取shell命令的执行结果
- 探讨C语言的那些小秘密之断言
- 深入探讨C语言中局部变量与全局变量在内存中的存放位置
- 基于C语言fflush()函数的使用详解
- 关于C语言除0引发的思考
- 深入分析C中不安全的sprintf与strcpy
- 探讨register关键字在c语言和c++中的差异
- C中实现矩阵乘法的一种高效的方法