Excellent algorithm series-sorting algorithm (1)

Source: Internet
Author: User

The sorting algorithm is one of our common algorithms and an important operation in computer programming. Its function is to include any sequence of data elements (or records, re-arrange into a sequence of keyword order. Different memory involved in the sorting process can be divided into two categories: one is internal sorting, which refers to the sorting process of the records to be sorted stored in the computer random memory, inner sorting includes insert sorting, Hill sorting, exchange sorting, fast sorting, and select sorting. In addition, external sorting refers to the large number of records to be sorted, as a result, the memory cannot accommodate all records at a time, and the sorting process needs to be stored externally for access during the sorting process.

Next we will talk about swap sorting, which mainly includes Bubble sorting and quick sorting. Quick sorting is actually an improvement for Bubble sorting.

To facilitate the description, the sequence table type is defined as follows:

# Define Max size 1000
Typedef int KeyType;
Typedef struct {
KeyType key;
InfoType otherinfo;
} RecType;

Typedef struct {
RecType r [MAXSIZE + 1];
Int length;
} SqList;
  

Bubble Sorting
The basic concept is: Compare the adjacent two numbers in sequence, put the decimal points in front, and put the large numbers in the back. The entire sorting process can execute up to n-1 troughs. It is a stable algorithm.

C Code:

Void BubbleSort (SqList & q)
{
Int j, h, p = q. length-1, temp;
For (h = 1; h <= p ;)
For (j = 0; j <P-1; j ++)
If (q. r [j]. Key> q. r [j + 1]. key)
{
Temp = q. r [j];
Q. r [j] = q. r [j + 1];
Q. r [j + 1] = temp;
P = j
}
}
This problem exists in the Bubble sorting method. When there are too many sorted data, the sorting time will be significantly extended. Specific practice: select a record (usually the first record), compare its keywords with the keywords of all records, and put all the records whose keywords are smaller than it before it, stores records larger than it, so that after a sorting, all records can be divided into two parts based on the demarcation point of the record, then sort the two parts until the sorting is complete. This is the improved method: quick sorting.

Quick sorting
Set the array to be sorted to a [0]... A [n-1],

The quick sorting algorithm is as follows:

1) set two variables low and high. When sorting starts, low = 0 and low = n-1;

2) Take the first array element as the key data and assign it to the key, that is, key = a [0];

3) search forward from J, that is, search forward from the back (high = high-1), find the first value less than the key a [high], and exchange with a [low;

4) Search backward from I, that is, search backward from front to back (low = low + 1), find the first a [low] greater than the key, and exchange it with a [high;

5) Repeat steps 3rd, 4, and 5 until low = high;

:

 

C Code:

Void QuickSort (SqList & R, int s, int t)
{
Int low, high, Key;
Low = s;
High = t;
Key = R. r [s]. key;
R. r [0] = R. r [s];
While (low {
While (high> low & R. r [high]. key> Key)
High --;
If (low {
R. r [low] = R. r [high];
Low ++;
}
While (low ++ Low;
If (low {
R. r [high] = R. r [low];
High --;
}
}
R. r [low] = R. r [0];
QuickSort (R, s, low-1 );
QuickSort (R, low + 1, t );
}
Well, let's talk about it first.

Author's "flute column"
 

Related Article

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.