排序:
1.冒泡排序
#include <stdio.h>
void my_print(int *a,int len)
{
int i;
for(i = 0;i<len;i++)
{
printf("%-4d",a[i]);
}
printf("\n");
}
void my_swap(int *a,int i,int j)
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
void my_sort(int *a,int len)//冒泡排序
{
int i,j;
for(i=0;i<len-1;i++)//多次将最大的数沉底
{
for(j=0;j<len-1-i;j++)//将一个最大的数沉底,每次沉底完成将长度减一
{
if(a[j]>a[j+1])
{
my_swap(a,j,j+1);//如果前一个数比后一个数大两者顺序交换
}
}
}
}
void my_sort2(int *a,int len)//递归实现
{
int i,j;
if(1 == len)
return ;
for(j=0;j<len-1;j++)
{
if(a[j]>a[j+1])
{
my_swap(a,j,j+1);
}
}
my_sort2(a,len-1);
}
int main()
{
int a[]={1,3,5,0,2,6,8,7,9,4};
int len = sizeof(a)/sizeof(a[0]);
my_sort(a,len);
my_sort2(a,len);
my_print(a,len);
return 0;
}
2.冒泡排序改进—鸡尾酒排序
#include <stdio.h>
void my_print(int *a,int len)
{
int i;
for(i = 0;i<len;i++)
{
printf("%-4d",a[i]);
}
printf("\n");
}
void my_swap(int *a,int i,int j)
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
void my_sort(int *a,int len)//鸡尾酒排序
{
int i;
int left = 0;
int right = len-1;
while(left<right)
{
for(i=left;i<right;i++)
{
if(a[i]>a[i+1])
{
my_swap(a,i,i+1);
}
}
right--;
for(i=right;i>left;i--)
{
if(a[i]<a[i-1])
{
my_swap(a,i,i-1);
}
}
left++;
}
}
void my_sort2(int *a,int left,int right)//递归实现
{
if(left>=right)
return ;
int i;
for(i=left;i<right;i++)
{
if(a[i]>a[i+1])
{
my_swap(a,i,i+1);
}
}
right--;
for(i=right;i>left;i--)
{
if(a[i]<a[i-1])
{
my_swap(a,i,i-1);
}
}
left++;
my_sort2(a,left,right);
}
int main()
{
int a[]={1,3,5,0,2,6,8,7,9,4};
int len = sizeof(a)/sizeof(a[0]);
//my_sort(a,len);
my_sort2(a,0,len-1);
my_print(a,len);
return 0;
}
3.选择排序
#include <stdio.h>
void my_print(int *a,int len)
{
int i;
for(i = 0;i<len;i++)
{
printf("%-4d",a[i]);
}
printf("\n");
}
void my_swap(int *a,int i,int j)
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
void my_sort(int *a,int len)
{
int i,j;
for(i=0;i<len;i++)
{
int min = i;
for(j=i+1;j<len;j++)
{
if(a[min]>a[j])
{
min = j;
}
}
if(min != i)
{
my_swap(a,min,i);
}
}
}
int main()
{
int a[]={1,3,5,0,2,6,8,7,9,4};
int len = sizeof(a)/sizeof(a[0]);
my_sort(a,len);
my_print(a,len);
return 0;
}
4.插入排序
#include <stdio.h>
void my_print(int *a,int len)
{
int i;
for(i = 0;i<len;i++)
{
printf("%-4d",a[i]);
}
printf("\n");
}
void my_swap(int *a,int i,int j)
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
void my_sort(int *a,int len)//插入排序
{
int i,j;
for(i=1;i<len;i++)
{
int get = a[i];
int j =i-1;
while(j>=0&&get<a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1] = get;
}
}
void my_sort2(int *a,int len)//二分法插入排序
{
int i;
for(i=1;i<len;i++)
{
int get = a[i];
int left = 0;
int right = i-1;
while(left<=right)
{
int mid = (left+right)/2;
if(a[mid]>get)
right = mid-1;
else
left = mid+1;
}
int j;
for(j=i-1;j>=left;j--)
a[j+1]=a[j];
a[left] = get;
}
}
int main()
{
int a[]={1,3,5,0,2,6,8,7,9,4};
int len = sizeof(a)/sizeof(a[0]);
my_sort2(a,len);
my_print(a,len);
return 0;
}
5.二分法插入排序
#include <stdio.h>
void my_print(int *a,int len)
{
int i;
for(i = 0;i<len;i++)
{
printf("%-4d",a[i]);
}
printf("\n");
}
void my_swap(int *a,int i,int j)
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
void my_sort(int *a,int len)//插入排序
{
int i,j;
for(i=1;i<len;i++)
{
int left = 0;
int right = i-1;
int get = a[i];
while(left<=right)
{
int mid = (left+right)/2;
if(a[mid]>get)
right = mid-1;
else
left = mid +1;
}
int j;
for(j=i-1;j>=left;j--)
a[j+1] = a[j];
a[left] = get;
}
}
int main()
{
int a[]={1,3,5,0,2,6,8,7,9,4};
int len = sizeof(a)/sizeof(a[0]);
my_sort(a,len);
my_print(a,len);
return 0;
}
6.希尔排序
#include <stdio.h>
void my_print(int *a,int len)
{
int i;
for(i = 0;i<len;i++)
{
printf("%-4d",a[i]);
}
printf("\n");
}
void my_swap(int *a,int i,int j)
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
void my_sort(int *a,int len)//插入排序
{
int i,j;
int gap = 1;
while(gap<len)
{
gap = gap*3+1;
}
while(gap>0)
{
for(i=gap;i<len;i++)
{
int get = a[i];
int j =i-gap;
while(j>=0&&get<a[j])
{
a[j+gap]=a[j];
j-=gap;
}
a[j+gap] = get;
}
gap = gap/3;
}
}
int main()
{
int a[]={1,3,5,0,2,6,8,7,9,4};
int len = sizeof(a)/sizeof(a[0]);
my_sort(a,len);
my_print(a,len);
return 0;
}
7.堆排序
#include <stdio.h>
void my_print(int *a,int len)
{
int i;
for(i = 0;i<len;i++)
{
printf("%-4d",a[i]);
}
printf("\n");
}
void my_swap(int *a,int i,int j)
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
void herify(int *a,int index ,int len)
{
int lindex = 2*index+1;
int rindex = 2*index+2;
int max = index;
if(lindex<len&&a[max]<a[lindex])
max = lindex;
if(rindex<len&&a[max]<a[rindex])
max = rindex;
if(max != index)
{
my_swap(a,max,index);
herify(a,max,len);
}
}
void my_sort(int *a,int len)
{
int i;
for(i=len/2-1;i>=0;i--)
{
herify(a,i,len);
}
int count = len;
for(i=len-1;i>0;i--)
{
my_swap(a,0,i);
count--;
herify(a,0,count);
}
}
int main()
{
int a[]={1,3,5,0,2,6,8,7,9,4};
int len = sizeof(a)/sizeof(a[0]);
my_sort(a,len);
my_print(a,len);
return 0;
}
8.归并排序
#include <stdio.h>
void my_print(int *a,int len)
{
int i;
for(i = 0;i<len;i++)
{
printf("%-4d",a[i]);
}
printf("\n");
}
void my_swap(int *a,int i,int j)
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
void merge(int *a,int left,int mid,int right ,int *tmp)
{
int i = left;
int j = mid+1;
int k = 0;
while(i<=mid&&j<=right)
{
if(a[i]<a[j])
tmp[k++]=a[i++];
else
tmp[k++]=a[j++];
}
while(i<=mid)
{
tmp[k++]=a[i++];
}
while(j<=right)
{
tmp[k++]=a[j++];
}
int t;
for(t=0;t<k;t++)
{
a[left+t]=tmp[t];
}
}
void my_sort(int *a,int left,int right,int *tmp)
{
int mid = (left+right)/2;
if(left<right)
{
my_sort(a,left,mid,tmp);
my_sort(a,mid+1,right,tmp);
merge(a,left,mid,right,tmp);
}
}
int main()
{
int a[]={1,3,5,0,2,6,8,7,9,4};
int len = sizeof(a)/sizeof(a[0]);
int tmp[10];
my_sort(a,0,len-1,tmp);
my_print(a,len);
return 0;
}