Qsort Inductive Learning (C language version)

Source: Internet
Author: User

Reference: https://www.cnblogs.com/ForeverJoker/archive/2013/05/25/qsort-sort.html

Included in <stdlib.h>

Int (*CMP) (const void *,const void *);

Qsort (*s, N, sizeof (s[0]), CMP);

Contains 4 parameters, the first incoming position, the second sort amount, the third individual size, and the fourth sort method

Sorting an array

int type :

< Span class= "hljs-reserved" > int CMP ( const void *a, const void *b) {

< Span class= "hljs-reserved" ><      Span class= "Hljs-keyword" > return * (int*) A-* (int*) b; < Span class= "hljs-reserved" >< Span class= "Hljs-keyword" >< Span class= "Hljs-keyword" >//from small to large sort

       return * (int *) b-* (int *) A; Sort from large to small}

The return value of the CMP function, <0 (no permutation), >0 (for displacement), 0 (no permutation).

Double type:

    INT cmp (const void * A, const void * b) {

< Span class= "hljs-reserved" >< Span class= "Hljs-keyword" > Return ((* (* (double*) A-* (double*) b> 0)? 1:-< span class= "Hljs-number" >1);}

< Span class= "hljs-reserved" >< Span class= "Hljs-keyword" >< Span class= "Hljs-keyword" >char:

< Span class= "hljs-reserved" >< Span class= "Hljs-keyword" >< Span class= "Hljs-keyword" > Return (* (char *) A-* (char *) b)

< Span class= "hljs-reserved" >< Span class= "Hljs-keyword" >< Span class= "Hljs-keyword" >struct type:

< Span class= "hljs-reserved" >< Span class= "Hljs-keyword" >< Span class= "Hljs-keyword" > TBD

< Span class= "hljs-reserved" >< Span class= "Hljs-keyword" >< Span class= "Hljs-keyword" >char s[][] Type:

< Span class= "hljs-reserved" >< Span class= "Hljs-keyword" >< Span class= "Hljs-keyword" > int cmp (const void *a, const void *b) {

< Span class= "Hljs-keyword" >< Span class= "Hljs-keyword" >< Span class= "Hljs-number" > return (strcmp ((char*) A, (char*) b)); }

Qsort Inductive Learning (C language version)

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.