LIB C function __ function

Source: Internet
Author: User
function 1:qsort function

Http://www.cnblogs.com/sooner/archive/2012/04/18/2455011.html
No.1, manual implementation quicksort:

The basic idea is to divide the data into two separate parts by a sort of sorting a part of all the data is smaller than the other part of all the data, and then the two parts of the data for the rapid sorting, the entire sequencing process can be recursive, so as to achieve the entire data into an ordered sequence.

#include <stdio.h>
#include <stdlib.h>
void QuickSort (int *a,int left,int right)
{
    if ( Left>=right) return;
    int x=a[(left+right) >>1],low=left,high=right;
    while (Low
Or:

#include <stdio.h> #include <stdlib.h> int qkpass (int r[],int left,int right) {int low,high,x;
    x=r[left];//Select benchmark record low=left;
    High=right;
        while (Low 


No.2, the most common, sort of int array:

#include <stdio.h> 
#include <stdlib.h>
int s[10000],n,i;
int cmp (const void *a, const void *b) 
{return 
     (* (int *) a-* (int *) b);  Ascending 
     //return (* (int *) b-* (int *) a);//Descending 
}
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]);
     printf ("\ n");
     System ("pause");
     return 0; 
}


No.3, sort the double array, the same principle as int:
Here is 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 as to judge, so here only returned 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 ("%.2lf", S[i]); 
     printf ("\ n");
     System ("pause");   
     return (0); 
}


No.4, sorting 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) 
{return 
     (* (char *) a-* (char *) b);
int main () 
{ 
     scanf ("%s", s); 
     N=strlen (s); 
     Qsort (S,n,sizeof (s[0]), CMP);     
     printf ("%s", s); 
     printf ("\ n");
     System ("pause");
     return 0; 
}


No.5, sorting structure (First order): Most of the time we will be sorting the structure, such as the 2010 school game based on a number of parameters, generally this time in the CMP function to force the conversion of the type, do not change in return, I do not know why, But the program will be clearer and absolutely correct. Also here please note the double return 0 question:

#include <stdio.h> 
#include <stdlib.h>
struct node 
{ 
     double data; 
     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->data) > (bb->data)? 1:-1);
int main () 
{ 
     scanf ("%d", &n); 
     for (i=0;i<n;i++) 
     { 
         s[i].no=i+1; 
         scanf ("%lf", &s[i].data); 
     } 
     Qsort (S,n,sizeof (s[0]), CMP); 
    
     for (i=0;i<n;i++)
         printf ("%d%lf\n", s[i].no,s[i].data); 
     System ("pause");
     return 0; 
}


No.6, sorting the structure (level two).

Add No to stabilize it (that is, the data values are equal in the original order):

#include <stdio.h> 
#include <stdlib.h>
struct node 
{ 
     double data; 
     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->data!=bb->data) return 
         ((aa->data) > (bb->data)? 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].data); 
     } 
     Qsort (S,n,sizeof (s[0]), CMP);
     for (i=0;i<n;i++)
         printf ("%d%lf\n", s[i].no,s[i].data);
     printf ("\ n");
     System ("pause");
     return 0; 
}
If there is a string, write it like this:
int cmp (const void *a,const void *b) 
{ 
     struct node *aa= (node *) A; 
     struct node *bb= (node *) b; 
     if (Aa->data!=bb->data) return 
         ((aa->data) > (bb->data)? 1:-1); 
     else return 
         ((aa->no)-(Bb->no));
     else return 
         strcmp (AA.STR,BB.STR);
         Return strcmp (AA->STR,BB->STR);
         Sort by the dictionary order of string Str in the structure body


No.7, ordering of string arrays (char s[][):

#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]); 
     printf ("\ n");
     System ("pause");
     return 0; 
}


No.8, sorting string array (char *s[):

#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]);
     printf ("\ n");
     System ("pause");
     return 0; 
}



Char *s = (char *) malloc (n);//where n is the size of the space to be opened
Char s[n]; A pointer to an array is defined, and the subscript operation of the array can be performed.


Char s[10]= "Hello";
S[0] s[1] etc are char
&s[0] is equivalent to S, is the char* type,


Formal type is equivalent to cout special treatment
s char* &s[0] Hello
S+1 char* &s[0]+1 Ello
*s Char S[0] H
&s 0x0012ff78
S[0] Char H
S[1] Char E
&s[0] char* Hello
&AMP;S[1] char* Ello



Char s2[10][20];
S2[0] s2[1] etc are char[20]
&s2[0] is equivalent to S2, is the char[20] type, and is equivalent to the char* type,


CHAR*S2[10];
S2[0] s2[1] etc are all char*
*s2[0] *s2[1] is char and is the first character of the string that the s2[0] s2[1 points to
S2 is char**, equal to &s2[0]


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.