简体   繁体   English

C编程中如何使用排序function

[英]How to use the sort function in C programming

Hello guys i have a situation here am trying to sort out numbers in C language but i seem to struggle to put a sort function can you please help me with this following souce code that prints out number and supose to sort them but i cant...please help:大家好,我有一种情况,我正在尝试用 C 语言对数字进行排序,但我似乎很难排序 function 你能帮我用下面的源代码打印出数字并假设对它们进行排序,但我不能。 。请帮忙:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1 = 8, num2 = 6, num3 = 2, num4 = 4, num5 = 1;
    printf("%d %d %d %d %d", num1, num2, num3, num4, num5);
    // qsort();  THIS IS WHAT I STRUGGLE WITH AT THE MOMENT
    return 0;
}  // THIS CODE PRINTS OUT NUMBERS BUT ARE NOT SORTED...SO I NEED TO SORT THEM PLEASE
   // YOUR HELP WILL BE MUCH APPRECIATED
   // I NEED TO KNOW HOW TO USE THE SORT(qsort) FUNCTION

qsort() does one thing and it does it exceptionally well, it sorts arrays, and does so very efficiently. qsort()做了一件事,而且做得非常好,它对 arrays 进行排序,而且效率很高。 As noted in the comments, before you can use qsort() you will need to collect your values in an array rather than separate variables.如评论中所述,在使用qsort()之前,您需要将值收集在数组中,而不是单独的变量中。 Once you have an array, using qsort() is trivial, your only responsibility using qsort is to write the compare() function.一旦你有了一个数组,使用qsort()是微不足道的,你使用 qsort 的唯一责任就是编写compare() function。

New users of qsort() usually have their eyes roll back in their heads when they see the declaration for the function:当看到 function 的声明时, qsort()的新用户通常会回头看:

int compare (const void *a, const void *b) { ... }

It's actually quite simple.其实很简单。 a and b are simply pointers to elements of the array to compare. ab只是指向要比较的数组元素的指针。 Since qsort() can handle any type array, the parameter type are void pointers.由于qsort()可以处理任何类型的数组,因此参数类型是void指针。 If you have an array of int , a and b are just pointers int (eg int* ).如果您有一个int数组,则ab只是指针int (例如int* )。 You simply need to write your compare function to cast a and b to int* and dereference to compare the int values with each other.您只需编写您的比较 function 以将ab转换为int*并取消引用以相互比较int值。

The return of compare() is either less than zero ( a sorts before b ), zero ( a and b are equal) or greater than zero ( b sorts before a ). compare()的返回值要么小于零ab之前排序)、ab相等)或大于零ba之前排序)。 So a niave compare() can be as simple as:所以一个天真的compare()可以很简单:

int compare (const void *a, const void *b)
{
    int x = *(int *)a,
        y = *(int *)b;
    
    return x - y;
}

However, there is always a potential for x - y to overflow with a large negative x and large y or large x and large negative y .但是, x - y总是有可能溢出大的负x和大的y或大的x和大的负y So you generally try and use the differnce between two comparisons to eliminate the potential for overflow, eg因此,您通常会尝试使用两次比较之间的差异来消除溢出的可能性,例如

int compare (const void *a, const void *b)
{
    int x = *(int *)a,
        y = *(int *)b;
    
    return (x > y) - (x < y);
}

Now if you take any value of a and b the return will either be -1 , 0 or 1 providing the sort information for qsort() without chance of overflow.现在,如果您采用ab的任何值,则返回将是-101 ,为qsort()提供排序信息,而不会溢出。

A short example using your values could be written as:使用您的值的简短示例可以写为:

#include <stdio.h>
#include <stdlib.h>

int compare (const void *a, const void *b)
{
    int x = *(int *)a,
        y = *(int *)b;
    
    return (x > y) - (x < y);
}

void prn_arr (int *arr, size_t nmemb)
{
    for (size_t i = 0; i < nmemb; i++)
        printf (i ? ", %d" : "%d", arr[i]);
    
    putchar ('\n');
}

int main()
{
    int num[] = {8, 6, 2, 4, 1};
    size_t nmemb = sizeof num / sizeof *num;
    
    puts ("array before sort:\n");
    prn_arr (num, nmemb);
    
    qsort (num, nmemb, sizeof *num, compare);
    
    puts ("\narray after sort:\n");
    prn_arr (num, nmemb);
}

Example Use/Output示例使用/输出

$ ./bin/qsortnum
array before sort:

8, 6, 2, 4, 1

array after sort:

1, 2, 4, 6, 8

Look things over and let me know if you have further questions.如果您还有其他问题,请仔细查看并告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM