Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如果从int更改为double,插入排序算法是否会受到影响?_C_Sorting - Fatal编程技术网

C 如果从int更改为double,插入排序算法是否会受到影响?

C 如果从int更改为double,插入排序算法是否会受到影响?,c,sorting,C,Sorting,所以我的代码有问题,也不明白为什么…我认为从double改为int可能会改变函数,因为它可以很好地处理整数 代码: 请不要在意独特的功能,因为它目前已损坏。但是我不明白为什么排序数组不工作 PS我有充分的信心,代码的其余部分是好的和工作的,除非直到排序数组 谢谢大家! 下面的代码编译得很干净 正确实现插入排序 同样的算法,通过改变类型,可以用来排序int等 #include <stdio.h> struct csv_t { int nrows; double lab

所以我的代码有问题,也不明白为什么…我认为从double改为int可能会改变函数,因为它可以很好地处理整数

代码:

请不要在意独特的功能,因为它目前已损坏。但是我不明白为什么排序数组不工作

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 array\n");

    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
#包括
结构csv\t
{
int nrows;
双实验室[10];
};
void sort\u double\u数组(double*A,int n);
结构csv_t D={10,200.0,100.0,50.0,25.0,12.0,6.0,3.0,2.0,1.0,0.0};
内部主(空)
{
排序双数组(D.labs,D.nrows);
返回(0);
}//结束函数:main
无效排序双数组(双*A,整数n)
{
int i,j;
/*假设[0]到[n-1]具有有效值*/
双温;
对于(i=1;i=0))
{//那么条目需要交换
A[j+1]=A[j];
j=j-1;
}//结束时
A[j+1]=温度;
}//结束
printf(“--BReak--”);
printf(“排序数组\n”);

对于(i=0;i您在排序算法中使用int\u swap。使用对double有效的等效方法。除此之外,函数sort\u int\u数组似乎是正确的。

潜在的问题可能是使用==比较double。对于double==应该用fabs(A[x]-B[x]替换)=
进行比较?@hellowurf您能提供正在排序的实际值吗?实际上,排序似乎工作正常:@PaulRoub ups我查看了错误的函数…但排序算法有一行:a[j+1]@doqtor:这不是排序的问题。你必须确保
a[i]是“有趣的”。我在发布之前运行了它,并且它在运行UbuntuLinux 14.04的x64系统上正常工作。哦,好吧。感谢你发现它并没有在所有系统上正常运行。
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
#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 array\n");

    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