C quicksort qsort()

Original post from:

http://www.cnblogs.com/syxchina/archive/2010/07/29/2197382.html

https://blog.csdn.net/b2utyyomi/article/details/51506001

qsort   function: sort using quicksort routine   

用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *));   

Parameters: 1 The first address of the array to be sorted 2 The number of elements to be sorted in the array 3 The size of the space occupied by each element 4 The pointer to the function

There are many kinds of sorting methods used to determine the sorting order, such as selection sort, bubble sort, merge sort, quick sort, etc. As you can see from the name, quick sort is currently recognized as a better sorting algorithm (I have never heard that the speed is faster than this, except for special occasions), which is faster than selection sort and bubble sort. This is because it is very fast, so the system also implements this algorithm in the library for our convenience. This is qsort.

qsort requires a comparison function in order to be more general . For example, you don't just want to sort a number, maybe you want to sort several numbers, for example, there is a structure struct num { int a; int b; }; Then I have an array of type num, num dddd[100 ]; I want to sort the dddd array, what should I do? I want the largest num element of a + b to be at the top of the array, so what? This can all be done by defining a comparison function. The function of the comparison function is to indicate to qsort how the size of the elements is compared. A comparison function like this inline int MyCmp(const void* a, const void* b) takes two elements as parameters and returns an int value. If the comparison function returns greater than 0 , qsort considers a>b, if the comparison function returns a>b If the return is equal to 0 , qsort considers the two elements a and b to be equal, and if it returns less than zero, qsort considers ab), but if your comparison function returns a -1 (less than zero), then qsort thinks that a<The sorting in this article is from small To the big sort>

    qsort, included in the stdlib.h header file, the function 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 participating in the sorting (or it can also be understood as the address to start sorting, because you can write &s[i]  
    Such an expression, this question is explained below); the second parameter is the number of elements participating in the sorting; the third three numbers are  
    The size of a single element, it is recommended to use an expression such as sizeof(s[0]), which is also explained below:) ; the fourth parameter is  
    Many people feel very confused about the comparison function. About this function, it is more troublesome to say...  
      
    Let's discuss the comparison function cmp (written as cmp is my personal preference, you can write whatever you want, such as qcmp or something  
    ). A typical cmp definition is  
      
    int cmp(const void *a,const void *b);  

First, sort an array of type int

int num[100];

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

{ return *(int *)a - *(int *)b; }

qsort(num,100,sizeof(num[0]),cmp);

Second, sort the char type array (same as the int type)

char word[100];

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

{ return *(char *)a - *(int *)b; }

qsort(word,100,sizeof(word[0]),cmp);

3. Sort the double type array (pay special attention)

double in[100];

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

{ return *(double *)a > *(double *)b ? 1 : -1; }

qsort(in,100,sizeof(in[0]),cmp);

Fourth, the first-level sorting of the structure

struct In { double data; int other; }s[100]

// Sort the structure according to the value of data from small to large. There are many types of data for the key data in the structure.

//Refer to the above example to write

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

{ return (*(In *)a).data > (*(In *)b).data ? 1 : -1; }

qsort(s,100,sizeof(s[0]),cmp);

五、对结构体二级排序

struct In { int x; int y; }s[100];

//按照x从小到大排序,当x相等时按照y从大到小排序

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

{

struct In *c = (In *)a; struct In *d = (In *)b;

if(c->x != d->x)

          return c->x - d->x;

else

          return d->y - c->y;

}

qsort(s,100,sizeof(s[0]),cmp);

六、对字符串进行排序

struct In { int data; char str[100]; }s[100];

//按照结构体中字符串str的字典顺序排序

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

{

return strcmp( (*(In *)a)->str , (*(In *)b)->str );

}

qsort(s,100,sizeof(s[0]),cmp);

七、计算几何中求凸包的cmp

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

//重点cmp函数,把除了1点外的所有点,旋转角度排序

{

struct point *c=(point *)a;

struct point *d=(point *)b;

if( calc(*c,*d,p[1]) < 0)

         return 1;

else if( !calc(*c,*d,p[1]) && dis(c->x,c->y,p[1].x,p[1].y) < dis(d->x,d->y,p[1].x,p[1].y))

//如果在一条直线上,则把远的放在前面

        return 1;

else return -1;

}

PS: 其中的qsort函数包含在的头文件里,strcmp包含在的头文件里

练习:

/*
#include <cstdio>
#include <cstring>
#include <cstdlib>

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);
}
*/
/*
#include <cstdio>
#include <cstdlib>

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);
     return ((node*)a)->date1 - ((node*)b)->date1;
}

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.对结构体排序.加入no来使其稳定(即data值相等的情况下按原来的顺序排)
/*
#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);
         
    if(((node*)a)->date1!=((node*)b)->date1)
         return(((((node*)a)->date1)>(((node*)b)->date1))?1:-1);
     else
         return((((node*)a)->no)-(((node*)b)->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);
}
*/
/*
5
1.1 2.2 1.1 3.3 0.0
5   0.000000
1   1.100000
3   1.100000
2   2.200000
4   3.300000

Process returned 0 (0x0)   execution time : 19.668 s
Press any key to continue.
*/
///对字符串数组的排序(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]);
    
     return(0);
}

///对字符串数组排序(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]);

     return(0);
}
*/


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325213650&siteId=291194637