The use of C language qsort

Source: Internet
Author: User
Tags date1 strcmp

Turn! Http://blog.sina.com.cn/s/blog_5155e8d401009145.html

Qsort, contained in the Stdlib.h header file, has a total of four parameters and no return value. A typical qsort is written as follows

Qsort (S,n,sizeof (s[0]), CMP);

The first parameter is the name of the array that participates in the sort (or it can be understood as the address to begin sorting, because it can be written &s[i]
Such an expression, which is described below); The second parameter is the number of elements participating in the sorting; The third three number is
The size of a single element, it is recommended to use an expression such as sizeof (S[0]), the following is also described:); A fourth parameter is
Many people feel very confused about the comparison function, about this function, but also to say the more troublesome ...

Let's talk about the CMP comparison function (written in CMP is my personal preference, you can write anything, like qcmp what
). A typical CMP is defined as

int cmp (const void *a,const void *b);

The return value must be int, the type of the two parameter must be const void *, and that A, B is my casual writing, personal preference.
If you sort an int, if it is ascending, then if a is larger than B, it returns a positive value, a negative value, and an equal return
0, and so on, followed by examples to illustrate how different types are sorted.

The correct return value will not be obtained until A/b is cast in the function body, and different types have different processing
Method. Please refer to the following example for details.

* * Some small questions about the fast line * *

1. The fast line is unstable, and this instability is manifested in the time at which it is used is uncertain, preferably (O (n)) and most
The Bad (O (n^2)) gap is too large and we generally say O (Nlog (n)) refers to its average time.

2. The fast-line is unstable, and this instability is manifested in the case that if the same comparison elements may be different in order, suppose we have
Such a sequence, 3,3,3, but these three 3 are distinguished, we mark as 3a,3b,3c, and the result of the quick row is not necessarily
is the arrangement of 3a,3b,3c, so we have to use the structure to stabilize it on certain occasions (No.6 's example
is to illustrate the problem)

3. The two parameters of the comparison function for the fast line must be const void *, and this should be a special note that writing A and B is just my
Personal preferences, CMP is just my personal preference. It is recommended to redefine two pointers in CMP to force type conversions,
Especially when it comes to sorting structures.

4. The third parameter of the qsort, that sizeof, is recommended to use sizeof (S[0]), especially for structs,
Often self-defined 2*sizeof (int) Such a problem, with sizeof (S[0) is both convenient and safe

5. If you want to partially sort the array, such as a s[n] arrays of the M elements starting from s[i], you only need to
Make some modifications on the first and second parameters: Qsort (&s[i],m,sizeof (S[i]), CMP);

* * Standard Process, example * *

The quicksort. Manual implementation
#include <stdio.h>

int a[100],n,temp;

void QuickSort (int h,int t)
{
     if (h>=t) return;
     int mid= (h+t)/2,i=h,j=t,x;
     x=a[mid];
     while (1)
     {
          while (a[i]<x) i++;
         while (a[j]>x) j--;
         if (i>=j) break;
         temp=a[i];
         a[i]=a[j];
         a[j]=temp;
     }
     a[mid]=a[j];
     a[j]=x;
     quicksort (h,j-1);
     quicksort (j+1,t);
     return;
}

int main ()
{
int i;
scanf ("%d", &n);
for (i=0;i<n;i++) scanf ("%d", &a[i]);
QuickSort (0,n-1);
for (i=0;i<n;i++) printf ("%d", a[i]);

return (0);
}

No.2. The most common, sort of an int array
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int s[10000],n,i;

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

int main ()
{
scanf ("%d", &n);
for (i=0;i<n;i++) scanf ("%d", &s[i]);

Qsort (S,n,sizeof (s[0]), CMP);

for (i=0;i<n;i++) printf ("%d", s[i]);

return (0);
}

No.3. Ordering a double array, with the same principle as int

Here to make a comment, originally because to judge if A==b return 0, but strictly speaking, two double number is not equal, can only say Fabs (A-B) <1e-20 such to judge, so here only return 1 and 1
#include <stdio.h>
#include <stdlib.h>

Double s[1000];
int i,n;

int CMP (const void * A, const void * b)
{
Return ((* (double*) a-* (double*) b>0)? 1:-1);
}

int main ()
{
scanf ("%d", &n);
for (i=0;i<n;i++) scanf ("%lf", &s[i]);

Qsort (S,n,sizeof (s[0]), CMP);

for (i=0;i<n;i++) printf ("%lf", S[i]);

return (0);
}

No.4. Sorting on a character array. Principle with int
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

Char s[10000],i,n;

int cmp (const void *a,const void *b)
{0
Return (* (char *) a-* (char *) b);
}

int main ()
{
scanf ("%s", s);
N=strlen (s);
Qsort (S,n,sizeof (s[0]), CMP);

printf ("%s", s);
return (0);
}

No.5. Sorting structures

Comment. Most of the time we would sort the structure, like the cherry blossom in the school qualifier, which is usually
The CMP function first cast the type, do not go inside the return, I do not understand why, but this program will
Clearer, and absolutely right. Also note here that double returns 0 problem

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

struct node
{
Double date1;
int no;
} s[100];

int i,n;

int cmp (const void *a,const void *b)
{
struct node *aa= (node *) A;
struct node *bb= (node *) b;
Return ((Aa->date1 > (bb->date1)) 1:-1);
}

int main ()
{
scanf ("%d", &n);
for (i=0;i<n;i++)
{
s[i].no=i+1;
scanf ("%lf", &s[i].date1);
}
Qsort (S,n,sizeof (s[0]), CMP);

for (i=0;i<n;i++) printf ("%d%lf\n", s[i].no,s[i].date1);

return (0);
}

No.6. Sorting structures. Add No to stabilize it (i.e. the data value is equal to the original order)

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

struct node
{
Double date1;
int no;
} s[100];

int i,n;

int cmp (const void *a,const void *b)
{
struct node *aa= (node *) A;
struct node *bb= (node *) b;
if (aa->date1!=bb->date1)
Return ((Aa->date1 > (bb->date1)) 1:-1);
Else
Return ((Aa->no)-(bb->no));
}

int main ()
{
scanf ("%d", &n);
for (i=0;i<n;i++)
{
s[i].no=i+1;
scanf ("%lf", &s[i].date1);
}
Qsort (S,n,sizeof (s[0]), CMP);

for (i=0;i<n;i++) printf ("%d%lf\n", s[i].no,s[i].date1);

return (0);
}

No.7. Ordering of string arrays (char s[][] type)

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

Char s[100][100];
int i,n;

int cmp (const void *a,const void *b)
{
Return (strcmp ((char*) A, (char*) b));
}

int main ()
{
scanf ("%d", &n);
for (i=0;i<n;i++) scanf ("%s", S[i]);


Qsort (S,n,sizeof (s[0]), CMP);

for (i=0;i<n;i++) printf ("%s\n", S[i]);

return (0);
}

No.8. Sorting string Array (char *s[] type)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

Char *s[100];
int i,n;

int cmp (const void *a,const void *b)

{
Return (strcmp (* (char**) a,* (char**) b));
}

int main ()
{
scanf ("%d", &n);
for (i=0;i<n;i++)
{
s[i]= (char*) malloc (sizeof (char*));
scanf ("%s", S[i]);
}

Qsort (S,n,sizeof (s[0]), CMP);

for (i=0;i<n;i++) printf ("%s\n", S[i]);

return (0);
}

The use of C language qsort

Related Article

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.