test

经典排序算法+文件操作~c语言实现

  1  #include <malloc.h>
  2  #include <stdio.h>
  3  #include <time.h>
  4  #include <stdlib.h>
  5  #define MAXSIZE 10
  6 typedef struct {
  7      int L[MAXSIZE+1];
  8      int length;
  9  
 10  }List,*list;
 11  void swap(List &L,int low,int high){
 12      int temp;
 13      temp=L.L[low];
 14      L.L[low] = L.L[high];
 15      L.L[high]=temp;
 16  }
 17  /**************************************
 18   *                 快速排序           *
 19   * ************************************/
 20  
 21  int Partition(List &L,int low,int high){
 22      int pro;
 23      pro=L.L[low];
 24      while(low<high){
 25  
 26          while(L.L[high]>=pro&&low<high){
 27                  high--;
 28          }
 29          swap(L,low,high);
 30          while(L.L[low]<=pro&&low<high){
 31              low++;
 32          }
 33  
 34  
 35          swap(L,low,high);
 36  
 37      }
 38      //L.L[low]=L.L[0];
 39      return low;
 40  
 41  }
 42  void Qsort(List &list,int low,int high){
 43      int pivot;
 44      if(low<high){
 45          pivot = Partition(list,low,high);
 46          Qsort(list,low,pivot-1);
 47          Qsort(list,pivot+1,high);
 48      }
 49  
 50  }
 51  
 52  
 53  /****************************
 54   *             冒泡排序            *
 55   * **************************/
 56   void bubbleSort(List &list){
 57       int i,j;
 58       bool flag=1;
 59       for(i=1;i<=list.length&&flag;i++){
 60           flag=0;
 61           for(j=list.length-1;j>=i;j--){
 62               if(list.L[j]>list.L[j+1]){
 63                   swap(list,j,j+1);
 64                   flag=1;
 65               }
 66           }
 67       }
 68  
 69   }
 70  /***************************
 71   *        简单选择排序      *
 72   ***************************/
 73   void selectSort(List &list){
 74       int i,j,min;
 75       for(i=0;i<=list.length;++i){
 76           min=i;
 77           for(j=i+1;j<=list.length;++j){
 78               if(list.L[min]>list.L[j]){
 79                   //swap(list,min,j);
 80                   min=j;
 81               }
 82           }
 83          if(i!=min){
 84               swap(list,min,i);
 85           }
 86       }
 87  
 88   }
 89   /*************************
 90    *     直接插入排序          *
 91    *************************/
 92   void insertSort(List &list){
 93       int i,j;
 94       for(i=2;i<=list.length;++i){
 95           if(list.L[i]<list.L[i-1]){
 96               list.L[0]=list.L[i];
 97               for(j=i-1;list.L[j]>list.L[0];--j){
 98                   list.L[j+1]=list.L[j];
 99               }
100              list.L[j+1]=list.L[0];
101           }
102       }
103   }
104  /***********************
105   *         希尔排序    *
106   ***********************/
107   void sheelSort(List &list){
108       int i,j;
109       int increment=list.length;
110  
111       do{
112           increment = increment/3+1;
113           for(i=increment+1;i<=list.length;++i){
114               if(list.L[i]<list.L[i-increment]){
115                  list.L[0]=list.L[i];
116                   for(j=i-increment;list.L[0]<list.L[j]&&j>0;j-=increment){
117                       list.L[j+increment]=list.L[j];
118                   }
119                   list.L[j+increment] = list.L[0];
120               }
121           }
122       }while(increment!=1);
123  
124   }
125    /***********************
126  2   *      归并排序       *
127  3   ***********************/
128  
129  void merge(int SR[],int TR[],int i,int m,int n){
130        int j,k,l;
131        for(j=m+1,k=i;i<=m&&j<=n;k++){
132            if(SR[i]<SR[j]){
133                TR[k]=SR[i++];
134            }else{
135                TR[k]=SR[j++];
136            }
137        }
138          if(i<=m){
139                for(l=0;l<=m-i;l++){
140                   TR[k+l]=SR[i+l];
141                }
142            }
143            if(j<=n){
144                for(l=0;l<=n-j;l++){
145                    TR[k+l]=SR[j+l];
146                }
147            }
148  
149  
150    }
151  
152   void mergePass(int SR[],int TR[],int s,int n){
153       int i=1;
154       int j;
155       while(i<=n-(2*s)+1){
156           merge(SR,TR,i,i+s-1,i+(2*s)-1);
157           i=i+(2*s);
158  
159       }
160       if(i<n-s+1){
161           merge(SR,TR,i,i+s-1,n);
162       }else{
163           for(j=i;j<=n;j++){
164               TR[j]=SR[j];
165           }
166       }
167  
168  
169   }
170  void mergeSort2(List* list){
171       int *TR = (int*)malloc((list->length+1)*sizeof(int));
172       int k=1;
173      while(k<list->length){
174           mergePass(list->L,TR,k,list->length);
175           k=2*k;
176           mergePass(TR,list->L,k,list->length);
177           k=2*k;
178       }
179  
180   }
181   void mSort(int SR[],int TR1[],int s,int t){
182       int m;
183       int TR2[MAXSIZE+1];
184       if(s==t){
185           TR1[s]=SR[s];
186       }else{
187           m=(s+t)/2;
188           mSort(SR,TR2,s,m);
189           mSort(SR,TR2,m+1,t);
190           merge(TR2,TR1,s,m,t);
191       }
192   }
193   void mergeSort(List* list){
194      mSort(list->L,list->L,1,list->length);
195   }
196  
197 /****************** 
198  *      文件操作    * 
199  ******************/
200 int createSourceFile(){
201     FILE *fp;
202       if((fp = fopen("source.txt","w+"))==NULL){
203           printf("can not open");
204            return 1;
205            //exit(0);
206       }
207        srand(time(NULL));
208        int tag;
209    for(tag=1;tag<=MAXSIZE;tag++){
210          
211      //    list.L[tag] = rand()%2000;
212          fprintf(fp,"%d ",rand()%2000);
213      }
214      fclose(fp);
215      return 0;
216 }  
217 void readSourceFile(List & list){
218     FILE *fp;
219       if((fp = fopen("source.txt","r"))==NULL){
220           printf("can not open");
221            exit(0);
222       }
223       int tag;
224    for(tag=1;tag<=MAXSIZE;tag++){
225          
226      //    list.L[tag] = rand()%2000;
227          fscanf(fp,"%d ",&list.L[tag]);
228          //printf("%d ",list.L[tag]);
229      }
230      fclose(fp);
231 }
232 void createToFile(List &list){
233     FILE *fp;
234       if((fp = fopen("to.txt","w+"))==NULL){
235           printf("can not open");
236            exit(0);
237       }
238        srand(time(NULL));
239        int tag;
240    for(tag=1;tag<=MAXSIZE;tag++){
241          
242      //    list.L[tag] = rand()%2000;
243          fprintf(fp,"%d ",list.L[tag]);
244          if(tag%5==0){
245          fprintf(fp,"\n");
246          }
247      }
248      fclose(fp);
249 }
250 /********************
251  *        查看list    *
252  ********************/
253 void show(List &list){
254     int i;
255     for(i=1;i<MAXSIZE+1;i++){
256     printf("%d,",list.L[i]);
257     }
258     printf("\n");
259 }
260 
261 /******************
262  *        菜单     * 
263  *****************/
264  
265 void mune(){
266     printf("         ********************\n\
267      *     排序大集合   *\n\
268      ********************\n\
269     ----------------------\n\
270     |  1.生成随机数文件\n\
271     |  2.快速排序\n\
272     |  3.冒泡排序\n\
273     |  4.简单选择排序\n\
274     |  5.直接插入排序\n\
275     |  6.希尔排序\n\
276     |  7.归并排序(递归)\n\
277     |  8.归并排序(非递归)\n\
278     |  9.保存至文件\n\
279     |  #.查看数据\n\
280     |  0.退出\n\
281     ----------------------\n\
282     请输入您的指令\n");
283 
284      //return 
285  }
286 
287  int main(){
288 
289      List list={{0},MAXSIZE};
290      //Qsort(list,0,9);    //快速排序
291      //bubbleSort(list);
292      //selectSort(list);
293      //insertSort(list);
294     
295      //createSourceFile();
296      //readSourceFile(list);
297      mune();
298      char num='1';
299     for(num;num!='0';){
300         
301          scanf("%c",&num); 
302          getchar();
303           switch(num){
304         case '1':
305         if(createSourceFile()){
306             printf("file error\n");
307         }else{
308             printf("file create success~~\n");
309             readSourceFile(list);
310         }
311             break;
312         case '2':
313             readSourceFile(list);
314             Qsort(list,0,MAXSIZE);
315             printf("操作成功~~\n"); 
316             break;
317         case '3':
318             readSourceFile(list);
319             bubbleSort(list);
320             printf("操作成功~~\n");
321             break;
322         case '4':
323             readSourceFile(list);
324             selectSort(list);
325             printf("操作成功~~\n");
326             break;
327         case '5':
328             readSourceFile(list);
329             insertSort(list);
330             printf("操作成功~~\n");
331             break;
332         case '6':
333             readSourceFile(list);
334             sheelSort(list);
335             printf("操作成功~~\n");
336             break;
337         case '7':
338             readSourceFile(list);
339             mergeSort(&list);
340             printf("操作成功~~\n");
341             break;
342         case '8':
343             readSourceFile(list);
344             mergeSort2(&list);
345             printf("操作成功~~\n");
346             break;
347         case '9':
348             createToFile(list);
349             printf("操作成功~~\n");
350             break;
351         case '#':
352             
353             show(list);
354             break;
355         default :
356             printf("输入有误,请重输\n");
357     }
358         
359     }
360 
361      return 0;
362  }
363  

 

友情链接:http://happyrxk.cn
posted @ 2012-12-10 23:45  フ天天好心情  阅读(305)  评论(0编辑  收藏  举报