Fast sorting algorithm study notes --sort and provide qsort

  Here is stored in the author related study notes when learning algorithms and data structures, record the author learned through the Internet and books so the knowledge and skills to provide some for the needy at the same time for their own learning and reflection of ideas and help. 

 

  From start sorting

  The basic sorting algorithms include bubble sort, insertion sort, quick sort and selection sort algorithm to learn the principles from the data structure in the base course, beyond the scope of this article. This introduces the algorithm is in the design process can directly use the sort tool. C / C ++ provides the user with a standard quick-sort algorithm for use in practical algorithm design, the user can call the sort function to achieve a simple function. Specifically, C implements qsort function provides sorting, and C ++ provides sort for users.

 

  sort

  sort for the C ++ standard template library provides function template sort function to achieve. In use, the need to include the header file <algorithm>, and declare the standard C ++ namespace std.  

    #include <algorithm>
     the using  namespace std;         // use the sort need to include the corresponding header file, and use the namespace std

  In actual use sort, mainly to call sorting sort provided by the following two formal parameters. The time complexity of quick sort algorithm sort of realization is O (nlogn).

    Sort (start, end)         // data between the start to end sort, wherein the data processing comprises start and does not include end 
    Sort (start, end, cmp)      // use custom collation of the start and end cmp data sorting between

   Wherein the starting address of the first element to be sorted data start, end address next address data to be sorted is the last element ( note that the address is not the end of the element  ). When the first form, the default treatment ordered elements in ascending order.

  Example: int a [5] comprising a 5 int array data, calling sort (a, a + 5) a sort of array, the elements of the array a ascending order. Note here that the address of the first element of the sort of data is a, a last address element is a + 4, when calling the sort end parameters should be for the next one is a + 5 last element addresses.

  The first sort of invocation can sort c ++ support for basic data types. May also be invoked by the user in the form of a second sort, to sort the rules specified by cmp, thus, the custom data structure is sorted. cmp two functions using the data to be sorted as a parameter, and returns a value of type bool, when the return value is true, the parameter as the second data before the first data row. Cmp general structure shown below.

    BOOL CMP (type a, type b) 
    { 
     determination rule         // When the function returns true cmp, the data b of the row in front of a data        
    }

  Example: For a custom type of student data structures, which have ticket, the name and a fraction containing information required for the array of the sort student data, so the student by score in descending score consistent press ticket number from lowest to highest. Students defined structure shown below.

    typedef struct{
        int sno;
        int score;
        char name[20];
    }Student;

  According to the information of the collation and student data structure, a comparison rule can be written cmp sort of function definition. Note here that, C ++ comparison operator returns a logical value of True or False based on the comparison.

    BOOL CMP (a Student, Student b) 
    { 
        IF (a.score =! b.score)
                 return a.score <b.score;     // b When a score higher than, the return value is true, this time the row b in a front of 
        the else return a.sno> b.sno;     // b when the student number is lower than a, the return value is true, this time in front of the row b a 
    }

  On the array contains a student information Student 100 a [100] sorting, simply sort (a, a + 100, cmp) can be invoked via.

  NOTE: The first call is actually used in the form of sort <operator treats compare and rank ordered data (the small front), and therefore if the user defines a <operator for the custom data structure, may be used directly in the form of a first sort order.

 

  qsort

  Qsort quicksort algorithm interface standard for the C language library provides, including the need to use C or C ++ standard library stdio.h library cstdlib. compared to the sort provided by C ++, qsort pointer operation as it involves more complicated when using relative terms. Qsort function shown in function prototypes as follows.  

    void qsort ( void * Base , NUM size_t, size_t size,     // Base starting address comparison element, num is the number of elements to be compared, size occupied by a single element to be compared byte length 
                int (* The compar) ( const  void *, const  void *));     // The compar function pointer for a predetermined comparison rule element

  In use qsort function, until the starting position of the data base comparison, the number of data to be compared and a single num data size the number of bytes occupied collectively specify the data to be compared Area. And compar function pointer specifies how the elements of the above-described region compared.

  This introduces the writing down of the comparison function compar. As shown qsort function prototype, void * function compar two pointers as arguments, and therefore for comparison elements, the need for the hand type conversion to match the type of format of the original data in the internal comparison function, a function return value int when the type, the function returns a value less than 0, the first parameter is placed in front of the second argument, the value returned is greater than 0, the first parameter is placed behind the second parameter. Below to sort the sort required to achieve a specific ranking function required for use qsort. Note that the C language comparison operator returns 0 and 1, and the comparison functions required to return slightly different positive and negative values, can be used EXP1? exp2: simplified form procedures exp3 manner.

    int compareStudent( const void *a , const void *b )
    {
        if( ( Student * ) a -> socre != ( Student * ) b -> score )
            return ( ( Student * ) a -> socre > ( Student * ) b -> score ) ? 1 : 0 ;         
        else
            return ( ( Student * ) a -> sno < ( Student * ) b -> sno ) ? 1 : 0 ;
     }

  In the illustrated embodiment defined the comparison function to sort in the case of, for example, call does qsort function as follows.

    qsort (A, 100, sizeof (Student), compareStudent);         // use compar function of one hundred students sort the data

 

  Another note: While C and C ++ provides users with a convenient interface to quickly sort, but in the actual algorithm learning process, understanding the principles of the algorithm and enjoy the convenience brought by the algorithm as important. In addition to the above therefore learn to use interface, but also should understand and master the basic principles and concrete realization of the sorting algorithm, so that they do not make a will only use limited tools provided by others who encourage each other.

 

  reference:

  sort - C++ Reference

  qsort - C++ Reference

  Algorithm notes p235-242

  Wang forum computer test machine postgraduate Guide - ordering 2.1

Guess you like

Origin www.cnblogs.com/yhjoker/p/11161984.html