排序结构

Sorting a structure

我想用 C 语言用冒泡排序之类的方法解决这个问题...任何人都可以提供帮助

示例:

  // 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}

OP 要求 C 解决方案,所以你去:

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;
            } 
        }
    }
}

另外,这是我从中得到的:here

如果要排序结构,还是要分解成比较数值类型。考虑到这一点,让我们以您的例子为例:

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 函数(我只推荐你有多种类型你想排序,比如 Points,Lines 等)你必须定义两个回调(ComparePointsSwapPoints

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;
}