Big talk data structure Chapter 1 sorting 9th basic concepts and classifications of sorting

Source: Internet
Author: User


9.2 basic concepts and classifications of sorting

9.2.1 definition of sorting
Sorting is a common problem in our lives. Students are arranged from short to high during their exercises. When the teacher checks the attendance of the class, the students are named in the order of student IDs. During the college entrance examination, the students are admitted in descending order based on the total score. What is the strict definition of sorting?
Assume that the sequence Containing N records is {R1, R2 ,......, Rn}, whose corresponding keywords are {K1, K2 ,......, KN}, You Need To determine 1, 2 ,......, An arrangement of N: P1, P2 ,......, Pn, so that the corresponding keyword satisfies kp1 ≤ kp2 ≤ ...... ≤Kpn (non-decreasing or non-increasing) relationship, even if the sequence is obtained into a sequence ordered by keywords {rp1, RP2 ,......, RPN}, such an operation is called sorting.
Note: In sorting, data elements are usually called records. Obviously, we input a set of records and output a set of records. Therefore, we can regard sorting as an operation of a linear table.
The sorting is based on the size relationship between keywords. Therefore, different sequences can be obtained for sorting different keywords in the same record set.
Here, the key word ki can be the primary keyword of record R, the secondary keyword, or even a combination of several data items. For example, in order to select better students in the main subject, some of our universities require a descending ranking of the total scores of all students, in addition, when the same total score is used, the total score outside the number of languages is ranked in descending order. This is the combination of the total score and the total score. 9-2-1, for the problem of composite sorting, of course, you can sort the total score first. If the total score is equal, then the total score outside the number of words will be sorted, but this is a relatively good way. We can also apply a technique to achieve a sorting to complete the composite Sorting Problem, for example, the total score and the number of words are connected together as the first and end of the string (Note that if the total number of words is not three, you must add the preceding zeros ), it is easy to see that the "753229" of Ling Hu Chong is smaller than Zhang Wuji's "753236", so Zhang Wuji is at the top of Ling Hu Chong.

From this example, we can see that the sorting of multiple keywords can be converted to the sorting of a single keyword. Therefore, we mainly discuss the sorting of a single keyword.

 

9.2.2 stability of sorting
It is precisely because sorting is not only for the primary keyword, so for the secondary keyword, because there may be two or more records with the same keywords in the record sequence to be sorted, the sorting result may be not unique. We have defined the stable and unstable sorting.
Assume that ki = kJ (1 ≤ I ≤ n, 1 ≤ j ≤ n, I ≤ j), and RI leads RJ (I <j) in the sequence before sorting ). If the sorted Ri is still ahead of RJ, the sorting method used is stable. Otherwise, RJ may lead the sorted sequence to ri, the sorting method used is unstable. 9-2-2. After sorting the total score in descending order, the highest total score is in the forefront. At this time, for both Linghu Chong and Zhang Wuji, if the order is not sorted, The Linghu Chong with equal scores should still be in the top position after their total order, so that it can be regarded as a stable sorting, if the two are reversed, the sorting is unstable. If a group of keyword instances are similar, the sorting method is considered unstable. Whether the Sorting Algorithm is stable can be obtained only after analysis.

 

9.2.3 inner and outer sorting
Based on whether all the records to be sorted are stored in the memory during the sorting process, the sorting is divided into: inner sorting and out-of-memory sorting.
Inner sorting means that all records to be sorted are stored in the memory during the entire sorting process. External sorting is because the number of sorting records is too large and cannot be stored in the memory at the same time. The entire sorting process requires data exchange between internal and external storage for further processing. Here we will mainly introduce multiple methods of inner sorting.
For inner sorting, the performance of the Sorting Algorithm is mainly affected by three factors.
1) time performance. Sorting is a type of operation that is often executed in data processing and often belongs to the core of the system. Therefore, the time overhead of sorting algorithms is the most important indicator to measure the performance of sorting algorithms. In the internal sorting, two types of operations are performed: Comparison and moving. Comparison refers to the comparison between keywords, which is the minimum operation for sorting. Moving refers to moving a record from one location to another. In fact, moving can be avoided by changing it to the storage method of the record (we will discuss the specific algorithm here ). In short, an efficient inner sorting algorithm should have as few keywords as possible for comparison and as few records as possible for moving.
2) auxiliary space. Another major criterion for evaluating sorting algorithms is the auxiliary storage space required for Algorithm Execution. The secondary storage space is the storage space required for Algorithm Execution in addition to the storage space occupied by sorting.
3) Complexity of algorithms. Note that this refers to the complexity of the algorithm, not the time complexity of the algorithm. Obviously, the algorithm is too complex and affects the sorting performance.
Based on the main operations used in the sorting process, we divide the inner sorting into insert sorting, exchange sorting, selection sorting, and merge sorting. It can be said that these are mature sorting technologies that have been widely used in many programming languages or databases, and even they have encapsulated implementation code about sorting algorithms. Therefore, the purpose of learning these sort algorithms is not to program the sort algorithms in reality, but to improve our ability to write algorithms through learning, in order to solve more complex and flexible application problems.
This chapter describes seven sorting algorithms. The algorithms are classified into two categories based on their complexity. Bubble sorting, simple selection sorting, and direct insertion sorting are simple algorithms, hill sorting, heap sorting, Merge Sorting, and quick sorting are improved algorithms. Next we will explain in sequence.

9.2.4 structures and functions used for sorting
To clarify the code of the Sorting Algorithm, I first provide a sequence table structure for sorting. This structure will also be used for all the sorting algorithms we will talk about later.

# Define maxsize 10/* is used to sort the maximum number of arrays, which can be modified as needed */
Typedef struct
{
Int R [maxsize + 1];/* is used to store the array to be sorted. R [0] is used as a sentry or temporary variable */
Int length;/* used to record the length of an ordered table */
} Sqlist;

 

In addition, since sorting is most often used to exchange two elements of an array, We will write it as a function, which will be widely used in subsequent explanations.

/* Exchange the subscript of array R in L as the value of I and j */
Void swap (sqlist * l, int I, Int J)
{
Int temp = L-> r [I];
L-> r [I] = L-> r [J];
L-> r [J] = temp;
}

Well, let's look at the first sorting algorithm.

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.