算法时间比较

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <time.h>
  4 #define maxn 100000
  5 
  6     typedef struct {
  7         int data;
  8         struct node*pri,*next;
  9     }node;
 10 
 11 void swap(node *p,node*q);
 12 void bubbleSort(int num[]);
 13 node* createDulNode(int *a,int n);
 14 void  quickSort(node *left,node *right);
 15 void  outPutLink(node *left,node * right);
 16 void outPutArr(int a[],int len);
 17 
 18 
 19 clock_t start,stop;
 20 
 21 int main() {
 22     srand((unsigned)time(NULL))  ;
 23     int a[maxn];
 24 
 25     for (int i = 0; i < maxn; ++i) {
 26         a[i]=rand();
 27     }
 28 
 29 
 30     node *head=createDulNode(a,maxn);
 31   //  printf("before quicksort");
 32     //outPutLink(head->next,head->pri);
 33     start=clock();
 34     quickSort(head->next,head->pri);
 35     stop=clock();
 36    // printf("after quicksort");
 37    // outPutLink(head->next,head->pri);
 38     printf("span time is %fs\n",(double)(stop-start)/CLK_TCK);
 39     outPutLink(head->next,head->pri);
 40 
 41 
 42 //   printf("before bubblesort");
 43 //   outPutArr(a,maxn);
 44 //    start=clock();
 45 //    bubbleSort(a);
 46 //    stop=clock();
 47 //    //printf("span time is %fs\n",(double)(stop-start)/CLK_TCK);
 48 //    printf("after bubblesort");
 49 //    outPutArr(a,maxn);
 50 
 51 
 52 
 53 
 54     return 0;
 55 }
 56 //one:bubbleSort
 57 //o(n^2)
 58 void bubbleSort(int num[]) {
 59     for (int j = 0; j < maxn; ++j) {
 60         for (int i = j+1; i < maxn; ++i) {
 61             if(num[i]<num[j]){
 62                 int tmp=num[i];
 63                 num[i]=num[j];
 64                 num[j]=tmp;
 65             }
 66         }
 67     }
 68 }
 69 
 70 //two:quickSort,o(nlgn)
 71 void  quickSort(node *left,node *right){
 72  if(left->pri!=right){
 73      node *tmp=right;
 74      node *p=left->pri;
 75      node *q=left;
 76      while(q!=right){
 77 
 78          if(q->data<tmp->data){
 79              p=p->next;
 80              swap(p,q);
 81 
 82          }
 83          q=q->next;
 84      }
 85      p=p->next;
 86      swap(p,tmp);
 87      quickSort(left,p->pri);
 88      quickSort(p->next,right);
 89  }
 90 }
 91 node* createDulNode(int *a,int n){
 92     node *head=(node*)malloc(sizeof(node));
 93     head->next=NULL;
 94     head->pri=NULL;
 95 
 96     node *p=head;
 97     int i=0;
 98     node *q=NULL;
 99     while(i<n){
100         q= (node*)malloc(sizeof(node));
101         q->data=a[i];
102         q->next=p->next;
103         q->pri=p;
104         p->next=q;
105         p=q;
106         i++;
107     }
108     q->next=head;
109     head->pri=q;
110     return head;
111 }
112 void swap(node *p,node*q){
113     node tmp=*p;
114     tmp.next=q->next;
115     tmp.pri=q->pri;
116     q->next=p->next;
117     q->pri=p->pri;
118     *p=*q;
119     *q=tmp;
120 }
121 void  outPutLink(node *left,node * right){
122     node *p=left;
123     while(p!=right){
124         printf("%d\n",p->data);
125         p=p->next;
126     }
127     printf("%d",right->data);
128 }
129 void outPutArr(int a[],int len){
130     int cnt=0;
131 
132     while(cnt<len){
133         printf("%d\n",a[cnt]);
134         cnt++;
135     }
136 }
View Code

linkTest:

maxn=100000 

 

maxn=10000

 

maxn=1000

 

arrSort:maxn=1000

 

 

 

maxn=10000

 

maxn=100000

 

posted @ 2020-09-24 21:39  ethon-wang  阅读(178)  评论(0编辑  收藏  举报