排序结构

问题描述 投票:0回答:2

我想用气泡排序之类的东西在C语言中解决此问题...任何人都可以提供帮助

  • 用5个struct Point来实现列表(在此点上是w / X,y);
  • 对5个结构点进行排序(先评估x,然后评估y)。

示例:

  // The points 
  p[0]={2,3}
  p[1]={4,5}
  p[2]={1,5}
  p[3]={4,3}
  p[4]={1,2}

  // Should become

  p[0]={1,2}
  p[1]={1,5}
  p[2]={2,3}
  p[3]={4,3}
  p[4]={4,5}
c arrays structure bubble-sort
2个回答
0
投票

如果要对结构进行排序,则仍然必须将其分解为比较数字类型。考虑到这一点,让我们以以下几点为例:

struct tagPoint
{
    int x;
    int y;
};
typedef struct tagPoint Point;

现在,假设您有一个Point数组,并且希望对其进行排序。您可以采取两种方法:

1。对数组进行排序的简单函数:

只需使函数对数组进行排序:

void SortPointArray(Point* Points, unsigned int n)
{
    /* This will sort the points with priority on the x and then the y value in ascending order. */
    for(unsigned int i = 0; i < n-1; i++)
        for(unsigned int j = i+1; j < n; j++)
        {
            if (Points[i].x > Points[j].x)
            {
                Point aux = Points[i];
                Points[i] = Points[j];
                Points[j] = aux;
            }
            else if ((Points[i].x == Points[j].x) && (Points[i].y > Points[j].y))
            {
                Point aux = Points[i];
                Points[i] = Points[j];
                Points[j] = aux;
            }
        }
}

2。将算法包装在通用函数中,并对要排序的每种类型使用回调:

这有点复杂,但是如果您经常使用它可以节省一些时间。在此,此函数使用与上面相同的算法,但是可以对任何类型进行排序。

void Sort(void* lpArray, unsigned int n, size_t cbSize, int (*Cmp)(void*, void*), void (*Swap)(void*, void*))
{
    for(unsigned int i = 0; i < n-1; i++)
        for(unsigned int j = i+1; j < n; j++)
            /* Cast void* to char* to get rid of warning with pointer arithmetic... */
            if ( Cmp( ((char*)lpArray) + i*cbSize, ((char*)lpArray) + j*cbSize) )
                Swap( ((char*)lpArray) + i*cbSize, ((char*)lpArray) + j*cbSize );
}

如您所见,它还需要另外2个作为参数传递的函数。如果要使此Sort函数知道如何对Point数组进行排序,则必须定义Comparrison函数和Swapping函数,并告诉Sort函数使用它们。

这是我实现它们的方式:

/** This function return 1 if p1 should be swapped with p2. */
int ComparePoints(void* vp1, void* vp2)
{
    Point *p1, *p2;
    p1 = vp1;
    p2 = vp2;

    if (p1->x > p2->x)
        return 1;
    else if ((p1->x == p2->x) && (p1->y > p2->y))
        return 1;

    return 0;
}



/** This will swap 2 points. */
void SwapPoints(void* vp1, void* vp2)
{
    Point p = *(Point*)vp1;
    *(Point*)vp1 = *(Point*)vp2;
    *(Point*)vp2 = p;
}

您如何使用它们?

如果只想使用第一个SortPointArray功能,就足够了:

int main()
{
    Point Array[10];

    /* Read the points. */
    for(unsigned int i = 0; i < 10; i++)
        scanf("%d %d", &Array[i].x, &Array[i].y);

    SortPointArray(Array, 10);

    /*Print the points.*/
    for(unsigned int i = 0; i < 10; i++)
        printf("%d %d\n", Array[i].x, Array[i].y);

    return 0;
}

但是如果要使用通用的Sort函数(仅当您要对多个类型进行排序,例如PointLine等时,才建议使用该函数),您必须包括两个回调([C0 ]和ComparePoints

SwapPoints

-1
投票

OP要求使用C解决方案,所以您去了:

int main()
{
    Point Array[10];

    /* Read the points. */
    for(unsigned int i = 0; i < 10; i++)
        scanf("%d %d", &Array[i].x, &Array[i].y);

    Sort(Array, 10, sizeof(Point), ComparePoints, SwapPoints);

    /*Print the points.*/
    for(unsigned int i = 0; i < 10; i++)
        printf("%d %d\n", Array[i].x, Array[i].y);

    return 0;
}

也是,这是我从void bsortDesc(struct yourStruct list[80], int s) { int i, j; struct yourStruct temp; for (i = 0; i < s - 1; i++) { for (j = 0; j < (s - 1-i); j++) { if (list[j].marks < list[j + 1].marks) { temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } } 中得到的。

© www.soinside.com 2019 - 2024. All rights reserved.