When I was reading the program today, I met the sort () function. I searched some information on the webpage and integrated it.

The sort () function is the sorting function in C ++. Its header file is: # include <algorithm> and qsort () is the sorting function in C. Its header file is: # include <stdlib. h>

First, let's talk about qsort (). It's easy to understand the searched materials.

Sorting method of Class 6 qsort

The qsort function is easy to use, but sometimes it is not suitable for sorting by struct Level 1, level 2, and string.

Function prototype: void qsort (void * base, size_t nelem, size_t width, INT (* fcmp) (const void *, const void *))

Input parameters:

Base: The number (length) of the number of nelem elements in the array to be sorted. width indicates the size of the storage space occupied by each element, fcmp is a pointer to a function used to compare array elements (this function must be written by itself). If the returned value is 1 or-1 (P1> P2,-1 is returned, p1 <P2 returns 1, P1 = P2 returns 0), size_t is int

Output Parameters: base is arranged in ascending order.

The specific classification and usage are as follows (if no specific description is provided, it is sorted in descending order ):

1. Sort one-dimensional arrays:

(Element_type is the data type stored in an array, which can be char, Int, float, double, ect)

Int comp (const void * P1, const void * P2)

{

Return * (element_type *) P2)> * (element_type *) P1 )? 1:-1;

}

Int main ()

{

Element_type list [Max];

Initial (list); // This is the initialization of the array list [Max]

Qsort (list, sizeof (list), sizeof (element_type), comp); // call the function qsort

Return 0;

}

2. Sort strings:

Int comp (const void * P1, const void * P2)

{

Return strcmp (char *) P2, (char *) P1 );

}

Int main ()

{

Char A [max1] [max2];

Initial ();

Qsort (A, lenth, sizeof (A [0]), comp );

// Lenth is the length of array

3. sort by a keyword in the struct (first-level sorting of the struct ):

Struct Node

{

Double data;

Int Other;

} S [100];

Int comp (const void * P1, const void * P2)

{

Return (* (node *) P2)-> DATA> (* (node *) P1)-> data? 1:-1;

}

Qsort (S, 100, sizeof (s [0]), comp );

4. sort by multiple keywords in the struct (multi-level sorting of the struct) [take Level 2 as an example]:

Struct Node

{

Int X;

Int y;

} S [100];

// Sort by X from small to large, and sort by Y when X is equal (the difference between 3 and 4)

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];

// Sort by the Lexicographic Order of the STR string in the struct.

Int comp (const void * P1, const void * P2)

{

Return strcmp (* (node *) P1). STR, (* (node *) P2). Str );

}

Qsort (S, 100, sizeof (s [0], comp );

6. Calculate the comp of convex hull in ry

Int comp (const void * P1, const void * P2) // The Key comp function sorts the rotation angles of all vertices except 1.

{

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 it is in a straight line, place the long line above

Return 1;

Else

Return-1;

}

The sort () function is a bit fuzzy (there is no summary of the comparison system)

The sort () function is used to sort the elements in the array of parameter integers from small to large. The parameter n indicates the number of elements stored in the array. For example, if there are 10 elements in the array, the sorting method is to first swap the minimum number of 10 elements with a [0; swap the minimum number from a [1] to a [9] with a [1 ,...., Until the sorting is completed.

This is the answer to the 1011 question I found on Baidu. I think it is the most representative of the sort () function.

# Include <iostream>

# Include <algorithm>

# Include <cstdio>

# Include <functional>

Using namespace STD;

Int stick [100], N;

Bool used [100];

// Unused: number of unused sticks

// Left: remaining length

// Len: the length of the current calculation.

Bool DFS (INT unused, int left, int Len)

{

// All the sticks have been used and have no remaining length. They meet the search criteria.

If (unused = 0 & left = 0)

Return true;

Int I;

// If there is no remaining one, a new stick is created.

If (Left = 0)

Left = Len;

// Search for unused sticks

For (I = 0; I <n; ++ I)

{

// Find unused ones, and the length is smaller than the left value (can be entered)

If (! Used & Stick <= left)

{

// Use the current stick

Used = true;

// If the correct answer can be extended in the current situation, return

If (DFS (unused-1, left-stick, Len ))

// Search successfully and return

Return true;

// Otherwise, the current stick is not used.

Used = false;

// If stick cannot be used to expand the correct result, if stick and left are of the same length, Len cannot be the correct answer.

// If left and Len are long, there is no way to expand them.

If (stick = left | left = Len)

Break;

}

}

// If no correct answer is obtained after a round of search, false is returned.

Return false;

}

Int main ()

{

Int I, sum;

While (scanf ("% d", & N )! = EOF & N)

{

Sum = 0;

For (I = 0; I <n; ++ I)

{

Scanf ("% d", & Stick );

Used = false;

Sum + = stick;

}

// Sort the data in ascending order.

Sort (stick, stick + N, greater <int> ());

// Search from small to large Based on the question Conditions

For (I = stick [0]; I <= sum; ++ I)

{

// Search for the total length of the stick only after I excludes it; otherwise, it is useless.

If (sum % I = 0)

{

If (DFS (n, 0, I ))

{

Printf ("% d/N", I );

Break;

}

}

}

}

Return 0;

}