堆排序:
#include <iostream>
#include <stdlib.h>
#include <ctime>
#define MAXN 100
using namespace std;
int a[MAXN];
void MinHeapFixdown(int a[],int i,int n) //建立最小堆//从i节点开始调整,n为节点总数,从0开始计算i节点的子节点为:2*i+1,2*i+2
{
int j,temp;
temp=a[i];
j=2*i+1;
while(j<n)
{
if(j+1<n&&a[j+1]<a[j]) //在左右孩子中找最小的
j++;
if(a[j]>=temp)
break;
a[i]=a[j]; //把较小的子节点往上移动,替换它的父结点
i=j;
j=2*i+1;
}
a[i]=temp;
}
void MakeMinHeap(int a[],int n) //建立最小堆
{
for(int i=n/2-1;i>=0;i--)
MinHeapFixdown(a,i,n);
}
void Minheapsort(int a[],int n)
{
for(int i=n/1;i>=1;i--)
{
swap(a[i],a[0]);
MinHeapFixdown(a,0,i);
}
}
int main()
{
int i;
srand(time(0));
for(i=0;i<MAXN;++i)
a[i]=rand()%100;
MakeMinHeap(a,MAXN);
Minheapsort(a,MAXN);
for(i=0;i<MAXN;++i)
cout<<a[i]<<endl;
return 0;
}
插入排序:
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100
int vec[MAX];
void insert_sort(int *a,int first,int last)
{
int i,j;
int temp;
for(i=first+1;i<=last;++i)
{
temp=a[i];
j=i-1; //与已排序的数注意比较,大于temp时,该数向后移//当first=0,j循环到-1,由于【短路求值】,不会运算array【-1】
while(j>=first&&(a[j]>temp))
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp; //被排序数放到正确的位置
}
}
int main()
{
int a[MAX];
srand((int)time(0));
for(int i=0;i<MAX;++i)
a[i]=rand()%100;
insert_sort(a,0,MAX);
for(int i=0;i<MAX;++i)
printf("%d\n",a[i]);
return 0;
}
直接选择排序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 100
void select_sort(int a[],int first,int last)
{
int i,j,min,temp;
for(i=0;i<last-1;++i)
{
min=i;
for(j=i+1;j<last;++j)
{
if(a[min]>a[j])
min=j;
}
if(min!=i)
{
temp=a[min];
a[min]=a[i];
a[i]=temp;
}
}
}
int main()
{
int a[MAX];
srand((int)time(0));
for(int i=0;i<MAX;++i)
a[i]=rand()%100;
select_sort(a,0,MAX);
for(int i=0;i<MAX;++i)
printf("%d\n",a[i]);
return 0;
}
快排:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 100
void quick_sort(int a[],int first,int last)
{
if(first>=last)
return ;
int i,j,key;
i=first,j=last,key=a[i];
while(i<j)
{
while(i<j&&a[j]>key)
j--;
if(i<j)
a[i++]=a[j];
while(i<j&&a[i]<key)
++i;
if(i<j)
a[j--]=a[i];
}
a[i]=key;
if(first<i-1)
quick_sort(a,first,i-1);
if(i+1<last)
quick_sort(a,i+1,last);
}
int main()
{
int a[MAX];
srand((int)time(0));
for(int i=0;i<MAX;++i)
a[i]=rand()%100;
quick_sort(a,0,MAX);
for(int i=0;i<MAX;++i)
printf("%d\n",a[i]);
return 0;
}
归并排序:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 100
void merge_sort(int a[],int T[],int first,int last)
{
if(last-first>1)
{
int m=first+(last-first)/2;
int p=first,q=m,i=first;
merge_sort(a,T,first,m);
merge_sort(a,T,m,last);
while(p<m||q<last)
{
if(q>=last||(p<m&&a[p]<=a[q]))
T[i++]=a[p++];
else
T[i++]=a[q++];
}
for(i=first;i<last;++i)
a[i]=T[i];
}
}
int main()
{
int a[MAX],b[MAX];
srand((int)time(0));
for(int i=0;i<MAX;++i)
a[i]=rand()%100;
merge_sort(a,b,0,MAX);
for(int i=0;i<MAX;++i)
printf("%d\n",a[i]);
return 0;
}
冒泡排序:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 100
void bubble_sort(int a[],int n)
{
int i=n,j;
int temp;
while(i>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;
}
}
i--;
}
}
int main()
{
int a[MAX];
srand((int)time(0));
for(int i=0;i<MAX;++i)
a[i]=rand()%100;
bubble_sort(a,MAX);
for(int i=0;i<MAX;++i)
printf("%d\n",a[i]);
return 0;
}
#include <iostream>
#include <stdlib.h>
#include <ctime>
#define MAXN 100
using namespace std;
int a[MAXN];
void MinHeapFixdown(int a[],int i,int n) //建立最小堆//从i节点开始调整,n为节点总数,从0开始计算i节点的子节点为:2*i+1,2*i+2
{
int j,temp;
temp=a[i];
j=2*i+1;
while(j<n)
{
if(j+1<n&&a[j+1]<a[j]) //在左右孩子中找最小的
j++;
if(a[j]>=temp)
break;
a[i]=a[j]; //把较小的子节点往上移动,替换它的父结点
i=j;
j=2*i+1;
}
a[i]=temp;
}
void MakeMinHeap(int a[],int n) //建立最小堆
{
for(int i=n/2-1;i>=0;i--)
MinHeapFixdown(a,i,n);
}
void Minheapsort(int a[],int n)
{
for(int i=n/1;i>=1;i--)
{
swap(a[i],a[0]);
MinHeapFixdown(a,0,i);
}
}
int main()
{
int i;
srand(time(0));
for(i=0;i<MAXN;++i)
a[i]=rand()%100;
MakeMinHeap(a,MAXN);
Minheapsort(a,MAXN);
for(i=0;i<MAXN;++i)
cout<<a[i]<<endl;
return 0;
}
插入排序:
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100
int vec[MAX];
void insert_sort(int *a,int first,int last)
{
int i,j;
int temp;
for(i=first+1;i<=last;++i)
{
temp=a[i];
j=i-1; //与已排序的数注意比较,大于temp时,该数向后移//当first=0,j循环到-1,由于【短路求值】,不会运算array【-1】
while(j>=first&&(a[j]>temp))
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp; //被排序数放到正确的位置
}
}
int main()
{
int a[MAX];
srand((int)time(0));
for(int i=0;i<MAX;++i)
a[i]=rand()%100;
insert_sort(a,0,MAX);
for(int i=0;i<MAX;++i)
printf("%d\n",a[i]);
return 0;
}
直接选择排序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 100
void select_sort(int a[],int first,int last)
{
int i,j,min,temp;
for(i=0;i<last-1;++i)
{
min=i;
for(j=i+1;j<last;++j)
{
if(a[min]>a[j])
min=j;
}
if(min!=i)
{
temp=a[min];
a[min]=a[i];
a[i]=temp;
}
}
}
int main()
{
int a[MAX];
srand((int)time(0));
for(int i=0;i<MAX;++i)
a[i]=rand()%100;
select_sort(a,0,MAX);
for(int i=0;i<MAX;++i)
printf("%d\n",a[i]);
return 0;
}
快排:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 100
void quick_sort(int a[],int first,int last)
{
if(first>=last)
return ;
int i,j,key;
i=first,j=last,key=a[i];
while(i<j)
{
while(i<j&&a[j]>key)
j--;
if(i<j)
a[i++]=a[j];
while(i<j&&a[i]<key)
++i;
if(i<j)
a[j--]=a[i];
}
a[i]=key;
if(first<i-1)
quick_sort(a,first,i-1);
if(i+1<last)
quick_sort(a,i+1,last);
}
int main()
{
int a[MAX];
srand((int)time(0));
for(int i=0;i<MAX;++i)
a[i]=rand()%100;
quick_sort(a,0,MAX);
for(int i=0;i<MAX;++i)
printf("%d\n",a[i]);
return 0;
}
归并排序:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 100
void merge_sort(int a[],int T[],int first,int last)
{
if(last-first>1)
{
int m=first+(last-first)/2;
int p=first,q=m,i=first;
merge_sort(a,T,first,m);
merge_sort(a,T,m,last);
while(p<m||q<last)
{
if(q>=last||(p<m&&a[p]<=a[q]))
T[i++]=a[p++];
else
T[i++]=a[q++];
}
for(i=first;i<last;++i)
a[i]=T[i];
}
}
int main()
{
int a[MAX],b[MAX];
srand((int)time(0));
for(int i=0;i<MAX;++i)
a[i]=rand()%100;
merge_sort(a,b,0,MAX);
for(int i=0;i<MAX;++i)
printf("%d\n",a[i]);
return 0;
}
冒泡排序:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 100
void bubble_sort(int a[],int n)
{
int i=n,j;
int temp;
while(i>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;
}
}
i--;
}
}
int main()
{
int a[MAX];
srand((int)time(0));
for(int i=0;i<MAX;++i)
a[i]=rand()%100;
bubble_sort(a,MAX);
for(int i=0;i<MAX;++i)
printf("%d\n",a[i]);
return 0;
}