"Dahua Data Structure" 9th Chapter 9.2 sorting Basic concepts and Classification _ Dahua data structure

Source: Internet
Author: User
9.2 Basic concepts and classifications of sorting

Definition of 9.2.1 Sorting
Sorting is a problem that we often face in our lives. Students in the exercise will be in accordance with from short to high arrangement; When the teacher check the attendance of the class, it will be named according to the student number, and the college entrance Examination will be accepted in descending order of the score. What is the strict definition of that sort?
Suppose that the sequence containing n records is {R1,R2,......, rn}, and its corresponding keyword is {k1,k2,......, kn}, you need to determine an arrangement of 1,2,......,n P1,P2,......, PN so that its corresponding keyword satisfies kp1≤kp2≤ ... ≤KPN (non-descending or non-ascending) relationships, even if the sequence becomes a sequential sequence of keywords {rp1,rp2,......, RPN}, such operations are called sorting.
Note that we usually refer to data elements as records in a sort problem. Obviously we're typing a collection of records, and the output is a collection of records, so it's possible to think of a sort as an operation of a linear table.
Sorting is based on the size relationship between keywords, so you can get different sequences for the same set of records, for different keywords.
Here the keyword Ki can be the primary key for record R, or it can be a secondary keyword, or even a combination of several data items. For example, some of our universities, in order to select a better student in the subject, require all students to be ranked in descending order for all subjects, and in descending order of the total number of words in the same total score. This is the combination of total score and the total number of words outside the two-time keyword combinations of the order. As the figure 9-2-1, for the combination of the problem of sorting, of course, you can first sort the total score, if the total score is equal to the case, reorder the number of words outside the total score, but this is the method of comparison soil. We can also apply a technique to achieve a sort that completes a combinatorial sort problem, for example, the total score and the number of words are linked together as a string (note that the number of words outside the total score if the number of digits is not enough three in front of the 0), it is easy to get the Fox Chong "753229" is less than Zhang Mowgli's "753236",         So Zhang Mowgli lined up in the front of the Fox Rush. As you can see from this example, the ordering of multiple keywords can eventually be translated into a single keyword sort, so we're talking about sorting the individual keywords here.

Stability of 9.2.2 sorting
Also precisely because sorting is not only for primary keywords, then for the secondary keyword, because there may be two or more keywords equal records in the sequence of records to be sorted, the sorting result may have a unique situation, we give the definition of stable and unstable sort.
Suppose Ki=kj (1≤i≤n,1≤j≤n,i≠j), and in the sequence before the sort, RI is ahead of RJ (that is, i<j). If the RI is still ahead of RJ after sorting, the sorting method is said to be stable, and conversely, if the sorted sequence is likely to lead to the RI, the ordering method used is not stable. As shown in the figure 9-2-2, after the total score in descending order, the total score high in the forefront. At this time for the Fox and Zhang Mowgli, not sorted is to make Fox rush in front, then their total score of the order, the score is equal to Fox Chong should still be in the former, so that is a stable sort, if they are reversed, then this sort is unstable. This sort method can be considered unstable as long as there is a similar occurrence of a set of keyword instances. Whether the sorting algorithm is stable or not can be obtained through analysis.


9.2.3 sorting and sorting in the outside
Depending on whether the records to be sorted in the sort process are all placed in memory, the sort is divided into: inner and outer sort.
The inner sort is in the whole process of sorting, all the records to be sorted are placed in memory. The sorting is due to the number of records sorted and cannot be placed in memory at the same time, the whole sort process needs to be exchanged between inside and outside for multiple data. We are here mainly on the introduction of a variety of methods of sorting.
For internal sorting, the performance of the sorting algorithm is mainly affected by three aspects
1) Time performance. Sorting is a kind of operation often carried out in data processing, which often belongs to the core of the system, so the time cost of sorting algorithm is the most important sign of its quality. In the inner sort, there are two main types of operations: comparison and movement. Comparisons refer to the comparison between keywords, which is the minimum operation to do a sort. Moving refers to a record moving from one location to another, in fact, the move can be avoided by changing the way records are stored (which we'll talk about when we explain the specific algorithm). In short, efficient internal sorting algorithms should have as few keyword comparisons and as few records to move as possible.
2) Auxiliary space. Another major criterion of the evaluation ranking algorithm is the secondary storage space needed to perform the algorithm. Secondary storage space is the additional storage space required to perform an algorithm in addition to the storage space occupied by the backlog.
3 The complexity of the algorithm. Note that this refers to the complexity of the algorithm itself, not the time complexity of the algorithm. Obviously the algorithm is too complex and can affect the performance of sorting.
According to the main operation of the sorting process, we divide the inner sort into: insert sort, exchange sort, select sort and merge sort. It can be said that these are more mature sorting technology, has been widely used in many programming languages or databases, and even they have been encapsulated on the sorting algorithm implementation code. Therefore, the reason why we learn these sorting algorithms is not so much to do in the reality of the sorting algorithm, but by learning to improve our ability to write algorithms, so as to solve more complex and flexible application problems.
In this chapter, we will explain seven sorts of algorithms, according to the complexity of the algorithm divided into two categories, bubble sort, simple selection and direct insertion of the order is a simple algorithm, and hill sorting, heap sorting, merge sorting, fast sorting is an improved algorithm. Later we will explain in turn.

Structures and functions used in 9.2.4 sorting
To clarify the code for the sorting algorithm, I first provide a sequential table structure for sorting, which will also be used for all the sorting algorithms that we want to talk about later.

#define MAXSIZE    /* is used to order the maximum number of arrays, can be modified on demand
/typedef struct
{
 int r[maxsize+1];  /* Used to store the array to be sorted, r[0] as Sentinel or temporary variable/
 int length;   /* Used to record the length of the order table * * *
}sqlist;

In addition, because sorting is most commonly used to the operation is an array of two elements of the exchange, we will write it as a function, in the subsequent explanation will be used in large quantities.

/* Swap L the subscript for array R is 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, having said so much, let's look at the first sort algorithm.

Source: http://www.cnblogs.com/cj723/archive/2011/04/14/2016029.html

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.