(2) sorting Overview

Source: Internet
Author: User

P263

SortSorting is an important operation in computer programming. Its function is to rearrange any sequence of data elements (or records) into a sequence ordered by keywords.

To facilitate the discussion, we must first define the sorting accurately:

Assume that the sequence Containing n records is: {R1, R2 ,..., rn}, the corresponding keyword sequence is: {K1, K2 ,..., kn}, need to determine 1, 2 ,..., an array of n: p1, p2 ,..., pn, so that the corresponding keywords meet the following non-decreasing (or non-increasing) Relationship: Kp1 <= Kp2 <=... <= Kpn: Make the sequence into a keyword-ordered sequence: {Rp1, Rp2 ,..., rpn }. Such an operation is called sorting.

The keyword Ki in the preceding sorting definition can be record Ri (I = 1, 2 ,..., n) can also be the secondary keyword of Ri, or even a combination of several data items. Considering that there may be two or more records with the same keywords in the sequence to be sorted, assume Ki = Kj (1 <= I <= n,
1 <= j <= n, I! = J), and the Ri is ahead of Rj (I
<J ). If the Ri in the sorted sequence is still ahead of Rj,The sorting method is stable.Otherwise, if the Rj in the sorted sequence is more than RiThe sorting method is unstable..

Stable sorting means that two elements are not exchanged if they are equal during the sorting process. For example, 1, 2, 2, 4, 3, 2nd elements are equal to 3rd elements during sorting, but they do not exchange order. Unstable sorting means the exchange order, whether or not the same. Stable sorting includes Bubble sorting, insert sorting, Merge Sorting, and base sorting. Unstable sorting: Select sorting, fast sorting, shell sorting, and heap sorting.

Because the number of records to be sorted is different, different memory involved in the sorting process can be divided into two categories: one isInternal sortingIt refers to the sorting process in which the records to be sorted are stored in computer random memory. The other isExternal sortingIt refers to the sorting process in which the number of records to be sorted is large, so that the memory cannot accommodate all records at a time. During the sorting process, it still needs to be stored for access. We will temporarily focus on internal sorting.

There are many internal sorting methods. Each method has its own advantages and disadvantages and is suitable for use in different environments (such as the initial sorting status of records. If the internal sorting method is classified by rows based on different principles in the sorting process, it can be roughly divided into insert sorting, exchange sorting, select sorting, Merge Sorting, and base sorting. If it is determined by the workload required in the internal sorting process, it can be divided into three categories: (1) a simple sorting method, whose time complexity is O (n ^ 2); (2) the time complexity of advanced sorting methods is O (nlogn); (3) Base sorting, and the time complexity is O (d · n ).

In the sorting process, two basic operations are required: (1) compare the size of two keywords; (2) move records from one location to another. The previous operation is necessary for most sorting methods, and the latter operation can be avoided by changing the storage method of records. The sequence of records to be sorted can be stored in the following three storage methods: (1) a group of storage units with consecutive addresses. It is similar to the sequential storage structure of a linear table. In this storage method, the order relationship between records is determined by the storage location, so sorting must be performed by moving records. (2) A group of records to be sorted must be stored in a static linked list, the order relationship between records is indicated by the pointer. To sort records, you do not need to move the records. You only need to modify the pointer. (3) the records to be sorted are stored in a group of sequential address storage units, and an address vector indicating the storage location of each record is set. During the sorting process, the record itself is not moved, the "addresses" in the mobile address vector are adjusted based on the values in the address vector after sorting. Assume that the data type of the record to be sorted is set:

#include <iostream>using std::string;#define MAXSIZE 20typedef int KeyType;typedef struct  {    KeyType key;    string otherinfo;}RedType;typedef struct  {    RedType r[MAXSIZE + 1];    int length;}SqList;
Sorting usually comes with a third-party sorting function, but what if a third-party library is not allowed? Or the third-party library is too large.
You only need a simple sort, which is not that complicated. As a result, these sorting algorithms have been well developed in our general development process.
Practical opportunities.

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.