Write your own quick-row templates and use the C + + quick-run library function

Source: Internet
Author: User
Tags strcmp

Write your own quick-row templates and use the C + + quick-run library function


1. Write your own quick-line template

I understand that the idea of a quick sort principle is:

Assume that the range of arrays to be sorted is 0~n
1. Find a number in an array as the center point m (you can use a fixed position number, or you can randomly take the number in the array)
2. Compare all the numbers in the array with the center point. The number less than the center point moves to the left of the center point, and the number greater than the center point moves to the right of the center point.
3, through the above two steps can be divided by the M-point of two arrays (an array is less than equal to M, one is greater than or equal to M), and then the two arrays recursive 1, 2 steps, know the partition size of the array is 0;

The main difficulty in the realization of the fast-line thought lies in the realization of the 2nd step:
1, the center point is taken on the left, so the position of the right J to start judging, I current value of
2, if the j>=m (the value of the center point here is Base_num), then J moves to the left, conversely if j<m the array subscript i is assigned to the number of J, and then proceed to the 3rd step
3, I moves to the next position, if the value of the array array[i] is less than or equal to M, then I continues to move to the right until array[i]>m, assigning the value of Array[j] to Array[i]
4, repeat 3, 4 steps until I>j



Perhaps my own expression is not very clear, the approximate idea and implementation is this, you can refer to the following articles and code to understand.
http://blog.csdn.net/morewindows/article/details/6684558

Nine degrees OJ 1196 score ranking Address: http://ac.jobdu.com/problem.php?pid=1196

#include <stdio.h>typedef struct{int num;int grade;} student_info_t; student_info_t student[101];//declares a comp type function pointer that can be directly used with comp to define the function pointer typedef int (*COMP) (const student_info_t *, const STUD ent_info_t *); The comparison function A is greater than B (a after B) returns 1 otherwise returns 0 int qcompare (const student_info_t *a, const student_info_t *b) {if (A->grade = = B->gr ADE) {return (A->num-b->num) >0? ( 1):(0);} else {return (A->grade-b->grade) >0? (  1):(0);} }//Quick-line function template void Quick_sort (student_info_t *p, int l, int r, Comp comp) {if (L < r) {int i=l, j=r;         student_info_t base_num = P[l];while (i < J) {while (I<j && Comp (&p[j],&base_num))//PJ large than px        j--; if (I < j) {p[i++] = p[j];} while (I<j &&!) Comp (&p[i],&base_num))//pi small than pxi++;if (I < J) {p[j--] = P[i];}} P[i] = Base_num;quick_sort (P, L, i-1, comp); Quick_sort (P, i+1, R, comp);}} int main (int argc, char **argv) {int Student_count;int i;while (scanf ("%d", &student_count)!=eof) {for(i=0; i<student_count; i++) {scanf ("%d%d", &student[i].num,&student[i].grade);} Quick_sort (student, 0, student_count-1, qcompare); for (i=0; i<student_count; i++) {printf ("%d%d\n", Student[i].num,student[i].grade);}}  return (0);}


Nine degrees OJ 1061 results sort address: http://ac.jobdu.com/problem.php?pid=1061

#include <stdio.h>typedef Struct{char name[101];int age;int grade;} student_info_t; student_info_t student[1001];//declares a comp type function pointer that can be directly used with comp to define the function pointer typedef int (*COMP) (const student_info_t *, const STU dent_info_t *);//strcmp function is consistent with int my_strcmp (const char *STR1, const char *STR2) {while (*str1 = *str2) {if (*str1 = = ') r Eturn (0); str1++;str2++;} Return (*STR1-*STR2);} The comparison function A is greater than B (a after B) returns 1 otherwise returns 0 int qcompare (const student_info_t *a, const student_info_t *b) {if (A->grade = = B->gr ADE) {if (my_strcmp (a->name,b->name) = = 0) {return (a->age-b->age) >0? ( 1):(0);} else {return my_strcmp (a->name,b->name) >0? ( 1):(0);}} else {return (A->grade-b->grade) >0? (  1):(0);} }//Quick-line function template void Quick_sort (student_info_t *p, int l, int r, Comp comp) {if (L < r) {int i=l, j=r;         student_info_t base_num = P[l];while (i < J) {while (I<j && Comp (&p[j],&base_num))//PJ large than px        j--; if (I < j) {p[i++] = p[j];} while (i<j &&! Comp (&p[i],&base_num))//pi small than pxi++;if (I < J) {p[j--] = P[i];}} P[i] = Base_num;quick_sort (P, L, i-1, comp); Quick_sort (P, i+1, R, comp);}} int main (int argc, char **argv) {int Student_count;int i;while (scanf ("%d", &student_count)!=eof) {for (i=0; i< Student_count; i++) {scanf ("%s%d%d", student[i].name,&student[i].age,&student[i].grade);} Quick_sort (student, 0, student_count-1, qcompare); for (i=0; i<student_count; i++) {printf ("%s%d%d\n", Student[i].name,student[i].age,student[i].grade);}}  return (0);}


2, C + + Fast Library function use (special case using a custom comparison function)

There is a fast-sorting standard library function, qsort , declared in C + +, in stdlib.h , with the following prototype:
void Qsort (void *base, int nelem, unsigned int width, int (* pfcompare) (const void *, const void*));
Base is the starting address of the array to be sorted
Nelem is the number of elements of the array to be sorted
Width is the size (in bytes) of each element of the array to be sorted
Pfcompare is a function pointer that points to a "comparison function."


The use of the Qsort function stipulates that the prototype of the "comparison function" should be:
int functionname (const void * elem1, const void * elem2);
Elem1 and Elem, pointing to the two elements to compare.
* Elem1 and * elem2 are the two elements to compare.
The function must have the following behavior:
1) If * elem1 should be in front of * elem2, then the function return value is a negative integer (any negative integer line).
2) if * elem1 and * elem2 which row is in front, then the function returns 0
3) If * elem1 should be followed by * elem2, then the function return value is a positive integer (any positive integer is a row).


Here are a few conditions that must be used for C + + to use the fast-running library function:
1)Key Library of functions:
#include <stdlib.h>
2)the key comparison function (below is a simple example):

int Comp (const void *p1,const void *P2)

{

Return (* ((int*) p1))-(* ((int*) P2 ));
}
3)the key Call function library---quick sort:

Qsort (nums,max_num,sizeof (int), Comp);


The following is a comparison function template for a variety of special cases:

1) to sort one-dimensional arrays:

(element_type is a data type stored in an array, which can be char, int, float, double, etc)

int Comp (const void *p1,const void *P2) {    return * ((Element_type *) p2)-* ((Element_type *) p1);} int main () {    element_type List[max];    Initial (list);    Qsort (list, sizeof (list)/sizeof (element_type), sizeof (Element_type), Comp);    or qsort (list, max,sizeof (Element_type), Comp);    return 0;}


2) sort the string:

int Comp (const void *p1,const void *P2) {    return strcmp ((char *) p2), (char *) p1);} int main () {    char a[max1][max2];                Here I use dynamic array char =new ... No,    initial. (a);    Qsort (A,lenth,sizeof (a[0]), Comp);  Lenth the length of an array A}


3) Sort by one of the keywords in the structure (sort at the struct level):

struct node{    double data;    int other;} S[100];int Comp (const void *p1,const void *P2) {    return (* (node *) p2)->data-(* (node *) p1)->data;} Qsort (S,100,sizeof (S[0]), Comp);

4) Sort by multiple keywords in the structure (multi-level sorting of structures) [take level Two for example]:

struct node{    int x;    int y;} s[100];//according to X from small to large, when x is equal, press Y from large to small to sort int Comp (const void *p1,const void *p2) {    struct Node *c = (Node *) p1;    struct Node *d = (node *) P2;    if (c->x! = d->x) return c->x-d->x;    else return d->y-c->y;}

5) sort the strings in the struct:

struct node{    int data;    Char str[100];} s[100];//sorts int Comp (const void *p1,const void *P2) {    return strcmp ((* (node *) p1)->str in the dictionary order of the string Str in the struct, (* (node *) ) (p2)->str);   } Qsort (s,100,sizeof (S[0],comp);

6) Calculate the convex hull comp in the geometry:

Here's what I copied from someone else, and for the time being. int comp (const void *p1,const void *P2)    //Key Comp function to sort all point rotation angles except 1 points {    struct point *c= ( Point *) P1;    struct Point *d= (point *) P2;    if (CAcl (*c, *d,p[1]) < 0)         return 1;    else if (!cacl (*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))  //If in a straight line, put the far in front        return 1;    else         return-1;}


Example code:

#include <stdio.h> #include <stdlib.h> #include <string.h>struct data{int num;     Char name[9]; int grade;} STUDENT[100009]; int compnum (const void *c,const void *d) {return (* (data *) c). num-(* (data *) d). Num;    int compname (const void *p1,const void *P2) {struct Data *c= (data *) P1;    struct Data *d= (data *) P2;   if (strcmp (* (data *) c). Name, (* (data *) d). Name)!=0) return strcmp ((* (data *) c). Name, (* (data *) d). Name); Note the parentheses else return (* (data *) c). num-(* (data *) d). Num;  int Compgrade (const void *p1,const void *P2) {struct Data *c= (data *) P1;  struct Data *d= (data *) P2;  if ((* (data *) c). grade!= (* (data *) d). grade) Return (* (data *) c). grade-(* (data *) d). grade; else Return (* (data *) c). num-(* (data *) d). Num; void Print (struct data student1[],int x) {for (int i=0;i<x;i++) printf ("%06d%s%d\n", Student1[i].num,student1[i].nam E,student1[i].grade);} void Numorder (struct data student2[],int N) {qsort (student,n,sizeof (student[0]), Compnum); Print (student,n);}     void Nameorder (struct data student2[],int N) {qsort (student,n,sizeof (student[0]), compname); Print (student,n);}     void Gradeorder (struct data student2[],int N) {qsort (student,n,sizeof (student[0]), compgrade); Print (student,n);} int main () {int n,c,count=1; while (scanf ("%d%d", &n,&c) ==2&&n) {for (int i=0;i<n;i++) scanf ("%d%s%d  ", &student[i].num,&student[i].name,&student[i].grade);  printf ("Case%d:\n", count++);  if (c==1) Numorder (student,n);  else if (c==2) Nameorder (student,n);  else if (c==3) Gradeorder (student,n); for (i=0;i<n;i++)//printf ("%06d%s%d\n", Student[i].num,student[i].name,student[i].grade); }return 0;}


Write your own quick-row templates and use the C + + quick-run library function

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.