C语言 如果将插入排序算法从整型改为双精度,是否会受到影响?



所以我有麻烦与我的代码,不知道为什么…我认为从double到int可能会改变函数,因为它可以很好地处理整数。

代码:

 void
    do_catavg(csv_t *D, int cat, int col) {
        int i,j,k;
        int count=0,nvalues[MAXROWS];
        double category[MAXROWS],category_red[MAXROWS];
        double average[MAXROWS];
        double sum;
        printf("%s %sn",D->labs[cat-1],D->labs[col-1]);
        for(i=0;i<D->nrows;i++){ 
            category[i]=D->vals[i][cat-1];
            printf("%f ",category[i]);
        }
        printf("n");
        sort_int_array(category,D->nrows);
        printf("n");
        distinct(category,category_red,D->nrows);
        printf("%dn",distinct(category,category_red,D->nrows));
        for(i=0;i<distinct(category,category_red,D->nrows);i++){
            for(j=0;j<D->nrows;j++){
                sum=0;
                count=0;
                if(category_red[i]==category[j]){
                    sum+=D->vals[j][col-1];
                    count++;
                }
                average[i]=sum/count;
                nvalues[i]=count;
            }
            printf("%f  %dn",average[i],nvalues[i]);
        }
        return; 
    }

功能:

void
sort_int_array(double A[], int n) {
    int i, j;
    /* assume that A[0] to A[n-1] have valid values */
    for (i=1; i<n; i++) {
        /* swap A[i] left into correct position */
        for (j=i-1; j>=0 && A[j+1]<A[j]; j--) {
            /* not there yet */
            int_swap(&A[j], &A[j+1]);
        }
        printf("%f ",A[i]);
    }
    printf("--BReak--");
    printf("The first value is %f ",A[0]);
    /* and that's all there is to it! */
}
int distinct(double A[],double B[], int n){
    int i;
    int new=0;
    for(i=0;i<n;i++){
        if(A[i]!=A[i+1]){
            B[new]=A[i+1];
            new++;
        printf("%f ",B[i]);
        }
    }
    return new;
}
/* exchange the values of the two variables indicated 
    by the arguments */
void
int_swap(double *p1, double *p2) {
    double tmp;
    tmp = *p1;
    *p1 = *p2;
    *p2 = tmp;
}
输出:

location mintemp
18.000000 22.000000 18.000000 22.000000 18.000000 22.000000 18.000000 22.000000
18.000000 22.000000
18.000000 18.000000 18.000000 18.000000 18.000000 18.000000 18.000000 18.000000
18.000000 --BReak--The first value is 22.000000
0.000000 0.000000 0.000000 0.000000 2
0.000000 0.000000 14.200000     1
0.000000 0.000000 -1.#IND00     0
0.000000 0.000000 > k 5 6

请不要关心distinct函数,因为它目前是坏的。但是我不明白为什么排序数组不工作…

PS我有充分的信心,其余的代码都很好,工作,除了排序数组。

谢谢!

下面的代码编译得很干净,

正确地实现了插入排序,

同样的算法,通过改变类型,可以用来排序int,等等

#include <stdio.h>
struct csv_t
{
    int nrows;
    double labs[10] ;
};
void sort_double_array(double *A, int n);
struct csv_t D = {10, {200.0, 100.0, 50.0, 25.0, 12.0, 6.0, 3.0, 2.0, 1.0, 0.0}};
int main( void )
{
    sort_double_array( D.labs, D.nrows );
    return(0);
} // end function: main

void sort_double_array(double *A, int n)
{
    int i, j;
    /* assume that A[0] to A[n-1] have valid values */
    double temp;
    for(i=1;i<n;i++)
    {
        temp=A[i];
        j=i-1;
        while( (temp < A[j]) && (j>=0) )
        { // then entries need swapping 
            A[j+1] = A[j];
            j=j-1;
        } // end while
        A[j+1]=temp;
    } // end for
    printf("--BReak--");
    printf("sorted arrayn");
    for( i=0; i<n; i++ )
    {
        printf( "%f ", A[i]);
    } // end for
    /* and that's all there is to it! */
} // end function: sort_double_array

您在排序算法中使用了int_swap。使用对double有效的等效方法。除此之外,函数sort_int_array似乎是正确的。

最新更新