The use of Qsort in Linux

Source: Internet
Author: User

Function prototypes

#include <stdlib.h>

void Qsort (void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *));

Parameter description:

Base: The starting address of the sorted array

NMEMB: The number of elements to sort

Size: A single element

Compar: User-defined method for comparing two element sizes.

Int (*compar) (const void *P1, const void *P2) function description

Ascending: Returns a positive number when P1 is greater than P2, returns 0 for equality, less than return negative

Descending: Returns a negative number when P1 is greater than P2, returns 0 for equality, less than the return positive number

eg

#include <stdio.h> #include <stdlib.h> #include <string.h>static intcmp (const void *P1, const void *P2) {return * (int*) P1-* (int*) P2;}  Intmain (int argc, char *argv[]) {int j;int s[10]={1,3,5,7,9,2,4,6,8,0};qsort (&s[0], ten, sizeof (S[0]), CMP); for (j = 0; J < 10; J + +) printf ("%d\n", S[j]); exit (exit_success);}

Run results



The use of Qsort in Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.