Summary of qsort sort functions

Source: Internet
Author: User


Qsort is included in the <stdlib.h> header file, which is sorted quickly by the comparison criteria you give, and by pointer movement. The result of sorting is still placed in the original array. Use the Qsort function to write a comparison function yourself.

Function Prototypes:


void Qsort (void * base, size_t num, size_t size, int (* comparator) (const void *, const void *));


Usage and parameter description:

Sorts the NUM elements of the array pointed by base, each element size bytes

Long, using the comparator function to determine the order.

The sorting algorithm used by this function compares pairs of values by calling the specified comparator function with a pointers to elements of the array.

The function does not return any value, but modifies the content of the array pointed by base reordering it elements to t He newly sorted order.


Base Pointer to the first element of the array to be sorted. (Array start address)

Num Number of elements in the array pointed by base. (number of array elements)

size Size in bytes of each element in the array. (size of each element)

Comparator Function that compares the elements. (function pointer, point to comparison function)

1, the function must accept the parameters that is pointers to elements, type-casted as void*. These parameters should is cast back to some data type and is compared.

2. The return value of this function should represent whether elem1 are considered less than, equal to, or greater than elem 2 by returning, respectively, a negative value, zero or a positive value.

return Value None (no return value)


>>>1. Sort the shaping data: (from small to large)

int Num[1010];qsort (Num, n, sizeof (int), CMP), int cmp (const void *a, const void *b) {    return * (int *) A-* (int *) b;}

From big to small:

int cmp (const void *a, const void *b) {    return * (int *) b-* (int *) A;}

>>>2. To sort char numbers:

Char str[1010];qsort (str, sizeof (str), sizeof (char), CMP), int cmp (const void *a, const void *b) {    return * (int *) A-* ( int *) b;}

>>>3. To sort double the numbers:

Double Num[1010];qsort (Num, N, sizeof (double), CMP), int cmp (const void *a, const void *b) {    return * (double *) a > * ( Double *) b? 1:-1;}

>>>4. To sort the structures by:

typedef struct node_{    int data;    int position;} Node; Node Num[1010];qsort (node, n, sizeof (node), CMP), int cmp (const void *a, const void *b) {    node *P1 = (node *) A;    Node *P2 = (node *) b;    if (p1->data! = p2->data) return p1->data-p2->data;    else return p1->position-p2->position;}

Example:

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 1010#    Define RST (n) memset (n, 0, sizeof (n)) using namespace std;typedef struct node_{char NAME[MAXN];    int data; int position;} Node;    Node N[maxn];int cmp (const void *a, const void *B)//For data from large to small, position and name from small to large sort {node *P1 = (node *) A;    Node *P2 = (node *) b;    if (p1->data! = p2->data) return p2->data-p1->data;    else if (p1->position! = p2->position) {return p1->position-p2->position; }else return strcmp (P1->name, p2->name);}    int main () {int n;            while (~SCANF ("%d", &n)) {for (int i=0; i<n; i++) {scanf ("%s%d%d", N[i].name, &n[i].data);        N[i].position = i;        } qsort (n, N, sizeof (Node), CMP);        printf ("------------------------------------\ n");        for (int i=0; i<n; i++) {printf ("%s%d%d\n", N[i].name, N[i].data, n[i].position); }    }    return 0;} 


Summary of qsort sort functions

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.