#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#define KeyType int
#define DataType int
#define MaxSize 50000
typedef struct entry{
KeyType key;
DataType data;
}Entry;
typedef struct list{
int n;
Entry D[MaxSize];
}List;
//简单选择排序
int FindMin(List list,int startIndex){
int i,minIndex=startIndex;
for(i=startIndex+1;i<list.n;i++){
if(list.D[i].key<list.D[minIndex].key){
minIndex=i;
// break;
}
}
return minIndex;
}
void Swap(Entry *D,int i,int j){
Entry temp;
if(i==j)return;
temp=*(D+i);
*(D+i)=*(D+j);
*(D+j)=temp;
}
void SelectSort(List* list){
int minIndex,startIndex=0;
while(startIndex<list->n-1){
minIndex=FindMin(*list,startIndex);
Swap(list->D,startIndex,minIndex);
startIndex++;
}
}
//直接插入排序
void InsertSort(List *list){
int i,j;
Entry insertItem;
for(i=1;i<list->n;i++){
insertItem=list->D[i];
for(j=i-1;j>=0;j--){
if(insertItem.key<list->D[j].key){
list->D[j+1]=list->D[j];
break;}
}
list->D[j+1]=insertItem;
}
}
//冒泡排序
void BubbleSort(List *list){
int i,j;
for(i=list->n-1;i>0;i--){
for(j=0;j<i;j++){
if(list->D[j].key>list->D[j+1].key){
Swap(list->D,j,j+1);
}
}
}
}
//快速排序
int Partition(List *list,int low,int high){
int i=low,j=high+1;
Entry pivot=list->D[low];
do{
do i++;while(i<=high&&list->D[i].key<pivot.key);
do j--;while(list->D[j].key>pivot.key);
if(i<j)Swap(list->D,i,j);
}while(i<j);
Swap(list->D,low,j);
return j;
}
void QuickSort(List *list,int low,int high){//调用QuickSort(list,0,list->n-1);
int k;
if(low<high){
k=Partition(list,low,high);
QuickSort(list,low,k-1);
QuickSort(list,k+1,high);
}
}
//序列两路合并排序
void Merge(List *list,Entry *temp,int low,int n1,int n2){
int i=low,j=low+n1;
while(i<=low+n1-1&&j<=low+n1+n2-1){
if(list->D[i].key<=list->D[j].key)
*temp++=list->D[i++];
else *temp++=list->D[j++];
}
while(i<=low+n1-1)
*temp++=list->D[i++];
while(j<=low+n1+n2-1)
*temp++=list->D[j++];
}
void MergeSort (List *list){
Entry temp [MaxSize];
int low, n1, n2, i, size = 1;
while (size < list->n){
low =0;
while(low+size<list->n){
n1 = size;
if(low + size*2 <list->n)
n2 = size;
else
n2 = list->n-low-size;
Merge(list, temp+low, low, n1, n2);
low += n1 + n2;
}
for(i=0;i<low; i++)
list->D[i] = temp[i];
size *= 2;
}
}
//堆排序
typedef struct maxheap{
int n;
Entry D[MaxSize];
}MaxHeap;
//向下调整
void AdjustDown(MaxHeap *heap,int current,int border){
int p=current;
int minChild;
Entry temp=heap->D[current];
while(2*p+1<=border){//p不是叶子节点,执行调整
if((2*p+2)<=border&&heap->D[2*p+1].key>heap->D[2*p+2].key)
minChild=2*p+2;//右孩子存在且较小,则minChild指向p的右孩子
else
minChild=2*p+1;//左孩子存在且较小,则minChild指向p的左孩子
if(heap->D[p].key<=heap->D[minChild].key)
break;//若当前结点不大于其最小的孩子,则调整结束
else{//否则将p与其最小孩子交换
temp=heap->D[p];
heap->D[p]=heap->D[minChild];
heap->D[minChild]=temp;
p=minChild;//设置下轮循环待考察的元素的位置(即当前下移元素的位置)
}
}
}
void HeapSort(MaxHeap *hp){
int i;
Entry temp;
for(i=(hp->n-2)/2;i>=0; i--)
AdjustDown(hp, i, hp->n-1);
for(i=hp->n-1;i>0;i--) {
Swap(hp->D, 0, i);
AdjustDown(hp,0, i-1);}
}
void create(int n){
int i;
FILE *fp=fopen("file.txt","w");
if(fp==NULL){
printf("ERROR");
}
srand(time(0));
int x;
// printf("排序前:");
for(i=0;i<n;i++){
x=rand()%10000;
fprintf(fp,"%d ",x);
// printf("%d ",x);
}
printf("\n");
fclose(fp);
}
int main(){
int i;
//向文件里写入随机生成的数据
List l;
l.n=10000;//修改此处
create(l.n);
FILE *fp=fopen("file.txt","r");
if(fp==NULL){
printf("ERROR");
}
clock_t start, end;
double interval;
//选择排序
for(i=0;i<l.n;i++){
fscanf(fp,"%d ",&l.D[i].key);
}
start=clock();
SelectSort(&l);
end=clock();
interval = (double)(end - start)*1000.0/CLOCKS_PER_SEC;
printf("选择排序所用时间: %lf ms\n", interval);
//直接插入排序
for(i=0;i<l.n;i++){
fscanf(fp,"%d ",&l.D[i].key);
}
start=clock();
InsertSort(&l);
end=clock();
interval = (double)(end - start)*1000.0/CLOCKS_PER_SEC;
printf("直接插入排序所用时间: %lf ms\n", interval);
//冒泡排序
for(i=0;i<l.n;i++){
fscanf(fp,"%d ",&l.D[i].key);
}
start=clock();
BubbleSort(&l);
end=clock();
interval = ((double)(end - start))*1000.0/CLOCKS_PER_SEC;
printf("冒泡排序所用时间: %lf ms\n", interval);
//快速排序
for(i=0;i<l.n;i++){
fscanf(fp,"%d ",&l.D[i].key);
}
start=clock();
QuickSort(&l,0,l.n-1);
end=clock();
interval = (double)(end - start)*1000.0/CLOCKS_PER_SEC;
printf("快速排序所用时间: %lf ms\n", interval);
//两路合并排序
for(i=0;i<l.n;i++){
fscanf(fp,"%d ",&l.D[i].key);
}
start=clock();
MergeSort(&l);
end=clock();
interval = (double)(end - start)*1000.0/CLOCKS_PER_SEC;
printf("两路合并排序所用时间: %lf ms\n", interval);
//堆排序
MaxHeap h;
h.n=l.n;
for(i=0;i<l.n;i++){
fscanf(fp,"%d ",&h.D[i].key);
}
start=clock();
HeapSort(&h);
end=clock();
interval = (double)(end- start)*1000.0/CLOCKS_PER_SEC;
printf("堆排序所用时间: %lf ms\n", interval);
return 0;
}