[C / C ++ syntax] -qsort and sort function

C and C ++ languages, different use of the sort function. C language does not pre-sort function, if the c language, to call the sort function, you need to customize a sorting function for, or use qsort function c own language, its header file stdlib.h. The qsort in C ++ and the use of either sort function

qsort function

qsort header file <stdlib.h>, the C language function
using the basic method of quick sort, the time complexity is O (n * logn), qsort function to find out how to use the pair of
function prototypes: void qsort( void *base, size_t num, size_t width, int (__cdecl *compare );
Compare function prototypes : int compare (const void *elem1, const void *elem2 ) );
Definition:

  1. The first address of the array to be compared
  2. Comparison of the number of elements to be
  3. The size of each element to be compared
  4. Pointing to the comparison function pointer (qsort function necessarily need to write)
    Parameter Meaning comparison function:
  5. The first address of the array to be compared
  6. End address of the array to be compared

Ascending array of type int

int num[105];
int cmp(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;//(int *)a是将指针转为int类型,而*(int *)a是取出指针a中的值,升序排列
}
qsort(num, 100, sizeof(num[0]), cmp);

If cmp function returns a positive number, then the first parameter in the parameter after the second cmp function returns a value if equal to 0, a, b does not move, if the return value is negative, then the first parameter placed in front of the second parameter
To descending order, thereturn *(int *)b - *(int *)a;

Char type array in ascending order of

char a[105];
int cmp(const void *a, const void *b)
{
    return *(char *a) - *(char *b);
}
qsort(a, 100, sizeof(a[0]), cmp);

An array of double type in ascending order (particular attention)

double a[105];
int cmp(const void *a, const void *b)
{
    return *(double *)a > *(double *b) ? 1:-1;
}
qsort(a, 100, sizeof(a[0]), cmp);

If *(double *)a - *(double *b)more than 0 to less than 1 decimal, 0 is returned, then the position will not move

Sort of structure

struct node{
    int num;
    char name;
    double sum;
}a[105];

To num ascending order of the first, in descending order of the name, the arrangement in ascending order of sum

struct node{
    int num;
    char name;
    double sum;
}a[105];
int cmp(struct node *a, struct node *b)
{
    if(a->num != b->num) return a->num - b->num;
    else if(a->name != b->name) return b->name - a->name;
    else if(a->sum != b->sum) return a->sum > b->sum ? 1 : -1;
}
qsort(a, 100, sizeof(a[0]), cmp)

Struct -> operator representative of the structure of a pointer variable value removed
and the operator representative of the structure of a variable removed.

The sort function and the difference in the sort function cmp function

name Return Type Statement
qsort( c ) int return a - b
sort (c ++) bool return a > b

The sort function

sort as a function in C ++ to give sort specify when used: using namespace std; To this is added #include <algorithm>
sort function is an upgraded version of qsort function, the sort function can be possible with the sort function, the sort function requires only 2 -3 argument
first argument is the address of the start element of
the second parameter is the address of the last element of the
third parameter to a custom comparison function, if there is no third parameter, the default ascending sort

Sort of an int array Descending

int a[105];
bool cmp(int a, int b)
{
    return a > b //sort中使用 > 而不是 - 
}
sort(a, a+100, cmp);

cmp returns a value of 1, then the first parameter in front of the second parameter
if it is 0, then the first parameter in the second rear parameter is not used -
the first parameter when the return value to write simple notation first, if it is after> descending the second parameter (the latter number is greater than the number in front of the same 5> 4> 3> 2> 1), <compared with the number in ascending order (greater than the number in front of the latter with 1 <2 <3 <4 <5)

Type arrays in descending order of char

char a[105];
bool cmp(char a, char b)
{
    return a > b //sort中使用 > 而不是 - 
}
sort(a, a+100, cmp);

Descending order of the double type

double a[105];
bool cmp(double a, double b)
{
    return a > b //sort中使用 > 而不是 - 
}
sort(a, a+100, cmp);

In descending order of string

char str[105][105];
bool cmp(char a, char b)
{
    return a > b;
}
sort(str, str+100, cmp);

Examples of structure

First of num descending order, in descending order the name

struct node{
    int num;
    char name;
}a[105];
bool cmp(struct node a, struct node b)
{
    if(node a.num != node b.num) return a.num > b.num;
    if(node a.name != node b.name) return a.name < b.name;
}
sort(a, a+100, cmp)

Example two parameters

#include <iostream>
#include <algorithm>
using namespace std;

int main(void)
{
    int a[105] = {2,4,1,23,5,76,0,43,24,65};
    for(int i = 0; i < 10; i++)
        cout<<a[i]<<endl;
    sort(a, a+10);
    for(int i = 0; i < 10; i++)
        cout<<a[i]<<endl;
    return 0;
}

The output is:

For example three parameters

A pair of ascending, descending order of B, in descending order of c

struct node{
    int a;
    int b;
    double c;
}arr[105];
bool cmp(node x, node y)
{
     if(x.a != y.a)  return x.a < y.a;
     if(x.b != y.b)  return x.b > y.b;
     return x.c > y.c;
}
sort(arr, arr+100, cmp);

Examples hdu 2037

Title Description

"This summer without AC?"
"Yes." "What
are you doing?"
"Yeah watching the World Cup, stupid!"
"@ # $% ^ & *% ..."

Indeed, the World Cup came to a fan festival also estimated that many ACMer will put aside the computer, toward the television.
As fans, we want to see as much of the full game, of course, as a new era of good young people, you must also look at some other programs, such as news network (Never forget that concerned about national affairs), is 6 + 7, Super girls, and Wang Xiaoya of "happy dictionary" and so on, assuming you already know you like to watch the broadcast schedule of all TV programs, you'll arrange it? (Goal is to see as much of the full program)

Input

Input data comprising a plurality of test example, the first line of each test case only one integer n (n <= 100), represents the total number of programs you like, then n rows, each row comprising two data Ti_s, Ti_e (1 <= i <= n), represent the i-th program start and end times, in order to simplify the problem, each time with a positive integer. n = 0 represents the input end without processing.

Output

For each test case, the number of outputs can see the complete TV program, the output of each test case in a separate line.

Sample Input

12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0

Sample Output

5

Problem-solving ideas

Define a variable structure, respectively, defining the start time, end time
first end time in ascending order, if the end of the same time, press the start time, in descending order.
A write cycle, if a program before the end of the time or less after a program start time, then watch the program or the next program.
Code

#include <stdio.h>
#include <algorithm>
using namespace std;

struct node{
    int start;//开始时间
    int t;//结束时间
}a[105];
bool cmp(node a, node b)
{
    if(a.t == b.t) //结束时间相同时
        return a.start > b.start; //开始时间降序排列
    return a.t < b.t;//结束时间不同时,结束时间升序排列
}
int main(void)
{
    int n, ans, num;
    while(~scanf("%d", &n))
    {
        if(n == 0) break;
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d", &a[i].start, &a[i].t);
        }
        ans = 0;//统计节目个数
        sort(a, a+n, cmp);
        num = a[0].t;
        for(int i = 1; i < n; i++)
        {
            if(a[i].start >= num)
            {
                ans++;
                num = a[i].t;
            }
        }
        printf("%d\n", ans+1);
    }
}

(Personal learning to use)

发布了20 篇原创文章 · 获赞 2 · 访问量 943

Guess you like

Origin blog.csdn.net/zhbbbbbb/article/details/103455817