zoukankan      html  css  js  c++  java
  • 堆排序结合C语言文件操作

    完整代码:

    View Code
      1 #include<stdio.h>
      2 #include<malloc.h>
      3 #define SORT_NUM_COUNT 50
      4 enum result{success,fail};
      5 //构造整型数组,并返回
      6 int* createArray(char *filePath);
      7 //对整型数组的内容合并排序
      8 result heapSort(int *heapArray);
      9 //使用自底向上算法,从给定数组的元素中构造一个堆
     10 result heapBottom(int *heapArray,int n);
     11 //输出整型数组的内容
     12 void output(int *intArray);
     13 //生成输出文件
     14 result foutput(int *intArray,char *fileName);
     15 //交换
     16 void swap(int *intArray,int index1,int index2);
     17 int main(int arg,char **argv){
     18     int *intArray=NULL;
     19     char *inputFileName=NULL;
     20     char *outputFileName=NULL;
     21     if(arg>=3){
     22         inputFileName=argv[1];
     23         outputFileName=argv[2];
     24     }else{
     25         inputFileName=(char *)malloc(FILENAME_MAX*sizeof(char));
     26         outputFileName=(char *)malloc(FILENAME_MAX*sizeof(char));
     27         printf("请输入要排序的文件名:");
     28         scanf("%s",inputFileName);
     29         getchar();
     30         printf("请输入生成排序文件的名字:");
     31         scanf("%s",outputFileName);
     32     }
     33     //构造数组
     34     intArray=createArray(inputFileName);
     35     if(intArray==NULL){//数组构造失败
     36         printf("数组构造失败:");
     37         return 0;
     38     }
     39     printf("排序的数字: ");
     40     output(intArray);
     41     //排序
     42     result r=heapSort(intArray);
     43     if(r==fail){
     44         printf("排序失败:");
     45     }else{
     46         printf("排序成功\n");
     47         if(foutput(intArray,outputFileName)==success){
     48             printf("生成排序文件成功\n");
     49         }else{
     50             printf("生成排序文件失败\n");
     51         }
     52         
     53     }
     54     getchar();
     55     getchar();
     56     return 0;
     57 }
     58 int* createArray(char *filePath){
     59     int *intArray=NULL;
     60     intArray=(int *)malloc(SORT_NUM_COUNT*sizeof(int));
     61     intArray[0]=0;
     62     FILE *fp=fopen(filePath,"r");
     63     if(fp==NULL){//无法打开
     64         return NULL;
     65     }
     66     int i=0;
     67     while(!feof(fp)){
     68         if(i>=SORT_NUM_COUNT){//超过数组空间
     69             fclose(fp);
     70             return NULL;
     71         }
     72         fscanf(fp,"%d",intArray+i+1);
     73         i++;
     74     }
     75     fclose(fp);
     76     intArray[0]=i;//将要排序的元素的数目
     77     return intArray;
     78 }
     79 result heapSort(int *heapArray){
     80     //构造堆
     81     for(int i=heapArray[0];i>1;i--){//对数组前i个元素进行构造堆。每遍历一次,最后都会把最大元素与第i个元素交换
     82         heapBottom(heapArray,i);
     83         swap(heapArray,1,i);//交换
     84         printf("第%d次更新堆:",heapArray[0]-i+1);
     85         output(heapArray);
     86     }
     87     return success;
     88 }
     89 //使用自底向上算法,从给定数组的元素中构造一个堆
     90 result heapBottom(int *heapArray,int n){
     91     if(heapArray==NULL||n<1){
     92         return fail;
     93     }
     94     for(int i=n/2;i>=1;i--){//i从当前堆最后子节点的父节点所在下标开始
     95         int k=i;
     96         int v=heapArray[i];
     97         bool heap=false;//父节点是否大于子节点的值
     98         while(!heap&&2*k<=n){//坐标为k的节点及它的所有子节点中,父节点的值都要大于子节点
     99             int j=2*k;//j存放左右孩子节点中最大值下标
    100             if(j<n){//存在两个子女
    101                 if(heapArray[j]<heapArray[j+1]){
    102                     j=j+1;
    103                 }
    104             }
    105             if(v>heapArray[j]){//父节点值都大于子节点
    106                 heap=true;
    107             }else{//父节点与孩子节点交换
    108                 heapArray[k]=heapArray[j];//值交换
    109                 heapArray[j]=v;
    110                 k=j;//k为子节点下标
    111             }
    112         }
    113 
    114     }
    115     return success;
    116 
    117 }
    118 
    119 void output(int *intArray){
    120     if(intArray==NULL){
    121         return ;
    122     }
    123     for(int i=1;i<intArray[0]+1;i++){
    124         printf("%d ",*(intArray+i));
    125     }
    126     printf("\n");
    127 }
    128 
    129 result foutput(int *intArray,char *fileName){
    130     if(intArray==NULL){
    131         return fail;
    132     }
    133     FILE *fp=fopen(fileName,"w+");
    134     if(fp==NULL){
    135         return fail;
    136     }
    137     for(int i=1;i<intArray[0]+1;i++){
    138         fprintf(fp,"%d ",*(intArray+i));
    139     }
    140     fclose(fp);
    141     return success;
    142 }
    143 
    144 void swap(int *intArray,int index1,int index2){
    145     int temp=intArray[index1];
    146     intArray[index1]=intArray[index2];
    147     intArray[index2]=temp;
    148 }



  • 相关阅读:
    mysql优化之索引优化
    mysqld --debug-sync
    mysql.cnf 配制文件详解
    my.cnf 中字符集设置
    tcp_tw_reuse 与 net.ipv4.tcp_tw_recycle
    mysql init_connect 参数的其他用处
    监控mysql索引使用效率的脚本
    mysql 源代码学习 博客 [lock..]
    mysqld with valgrind
    思维导图软件
  • 原文地址:https://www.cnblogs.com/dann/p/2747297.html
Copyright © 2011-2022 走看看