C-language "quick sort" function notation

Source: Internet
Author: User

Code is: C language in the quick line of writing, to join the header file <stdlib.h>

Qsort (array name, length, data type size, comparison operator);

#include <stdio.h> #include <string.h> #include <stdlib.h>int cmp (const void *a, const void *b) {return * (int *) a-* (int *) b;      Sort from small to large   such as output: 1 2 3 4 5}int main () {int a[100];int n;int I, J;while (scanf ("%d", &n)!=eof) {for (i=0; i<n; i++) { scanf ("%d", &a[i]);} Qsort (A, n, sizeof (int), CMP), for (j=0; j<n; J + +) {printf ("%d%c", A[j], i==n-1? ' \ n ': ');}} return 0;}

  

#include <stdio.h> #include <string.h> #include <stdlib.h>int cmp (const void *a, const void *b) {return * (int *) b-* (int *) A;   Sort from large to small   such as output: 5 4 3 2 1}int main () {int a[100];int n;int I, J;while (scanf ("%d", &n)!=eof) {for (i=0; i<n; i++) { scanf ("%d", &a[i]);} Qsort (A, n, sizeof (int), CMP), for (j=0; j<n; J + +) {printf ("%d%c", A[j], i==n-1? ' \ n ': ');}} return 0;}

  

C-language "quick sort" function notation

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.