Data Structure-Summary of various sorting algorithms [Final], data structure Algorithms

Source: Internet
Author: User

Data Structure-Summary of various sorting algorithms [Final], data structure Algorithms
Summary of various sorting algorithms 5. Allocation sorting-> base sorting:

Base sorting is a method of sorting a single key code by base into multiple key codes. Base sorting belongs to the "low priority" sorting method, which is sorted by repeated allocation and collection operations.

Single Keywords of numeric or numeric type can be considered as multiple keywords consisting of multiple digits or characters,

In this case, you can sort by the "distribution-Collection" method, which is called the base sorting method. The advantage is that you do not need to compare keywords.

For example, for the following set of keywords {278,109,063,930,589,184,505,269,008,083}, the values of the "single digit" are 0, 1,…, respectively ,..., 9 "Assign" to 10 groups, and then "collect" them in the order from 0 to 9. Then, the values are 0, 1,…, respectively ,..., 9. "Assign" to 10 groups, and then "collect" the groups from 0 to 9. Then, repeat the above operations in the order of "Hundred bits, the ordered sequence of this set of keywords can be obtained.

In order to reduce the storage space needed for base sorting on a computer, a linked list should be used as the storage structure, that is, the chain base sorting. The specific method is as follows:

(1) the records to be sorted are linked with pointers to form a linked list;

(2) For "Allocation", records are allocated to different "chain queues" based on the value of the current "keyword bit". The "keyword bit" recorded in each queue is the same;

(3) During "Collection", a chain table is formed at the beginning and end of each queue from small to large based on the value of the current keyword;

(4) Each keyword is repeated in steps (2) and (3.

 

[Performance Analysis]

(1) space efficiency: the complexity of the chain base sorting space is O (rd ).

(2) time efficiency: the time complexity of base sorting is O (d (n + rd )).

Where, allocated as O (n), collected as O (rd) (rd is "base"), and d is the number of allocated-collected shards.

(3) Stability: Base sorting is a stable sorting method.

 

6. Comparison of various internal sorting algorithms

1. Time Performance

(1) There are three sorting methods based on average time performance:

The time complexity is O (nlogn): Fast sorting, heap sorting, and merge sorting, with the best fast sorting;

The time complexity is O (n ^ 2): Direct insertion sorting, Bubble sorting, and simple selection sorting, where direct insertion is the best, this is especially true for record sequences that are approximately ordered for keywords;

The time complexity is O (n). Only the base sorting method is used.

(2) When the sequence of records to be sorted by keywords, the time for direct insertion sorting and Bubble Sorting can reach O (n)

Complexity; this is the worst case for fast sorting. At this time, the time performance is normalized to O (n ^ 2), so it should be avoided as much as possible.

(3) the time performance of simple sorting, heap sorting, and Merge Sorting does not change with the distribution of keywords in the record sequence.

 

2. Space Performance

(1) The space complexity of all simple sorting methods (including direct insertion, blister, and simple selection) and heap sorting is O (1 );

(2) The quick sorting is O (logn), which is the auxiliary space required by the stack;

(3) The most auxiliary space is required for merging and sorting, and its spatial complexity is O (n );

(4) The first and last pointer of the queue must be attached to the chain base sorting, and the space complexity is O (rd)

 

3. Stable performance of sorting methods

A stable sorting method is required when the LSD (base sorting) method is used to sort record sequences with multiple keywords.

Fast sorting and heap sorting are unstable sorting methods.

In most cases, sorting is based on the record's primary keywords. In this case, you do not need to consider the stability of the sorting method. if the sorting is based on the record's secondary keyword, the stability of the sorting method should be fully considered.

 

 

4. comprehensive comparison and selection of various sorting methods

(1) Comparison of various sorting methods, such as tables.

 

(2) Select

When selecting the sorting method, you need to consider the following factors: the number of records to be sorted n; the size of the records themselves; the keyword

Distribution, requirements for sorting stability, conditions for language tools, and sizes of auxiliary spaces.

Based on these elements, we can draw the following conclusions:

① If the number of records to be sorted is n hours, insert sorting and simple selection sorting are used;

② If the records to be sorted are sorted by keywords, direct insertion sorting and Bubble sorting are recommended. Direct insertion sorting is the best sorting method, which is usually followed by fast sorting, merge Sorting and other sorting methods.

③ When n is large and the number of keywords is small, it is better to sort by the chain base;

④ If n is large, the time complexity should be O (nlogn) sorting method: Fast sorting, heap sorting or merge sorting.


Detailed description of various sorting algorithms in the data structure ,,,,,,,,,,

Sorting algorithms include insert sorting, exchange sorting, select sorting, and merge sorting.
Insert sorting includes direct insert sorting and Shell sorting. Switch sorting includes Bubble sorting and differentiation switching sorting. Selection sorting includes direct selection sorting and heap sorting.
Among these sorting algorithms, the average time complexity of directly inserting sorting, Bubble sorting, and directly selecting sorting is O (n square ); the average time complexity of the differentiation, switching, sorting, heap sorting, and merge sorting algorithms is




Data structure C language-implement various sorting algorithms

Just finished
# Include <iostream>
Using namespace std;

Void BiInsertsort (int r [], int n) // insert sort (half)
{
For (int I = 2; I <= n; I ++)
{
If (r [I] <r [I-1])
{
R [0] = r [I]; // sets the sentry
Int low = 1, high = I-1; // half Lookup
While (low <= high)
{
Int mid = (low + high)/2;
If (r [0] <r [mid]) high = mid-1;
Else low = mid + 1;
}
Int j;
For (j = I-1; j> high; j --) r [j + 1] = r [j]; // post shift
R [j + 1] = r [0];
}
}
For (int k = 1; k <= n; k ++) cout <r [k] <"";
Cout <"\ n ";
}

Void ShellSort (int r [], int n) // sort by hill
{
For (int d = n/2; d> = 1; d = d/2) // sort the values in increments of d.
{
For (int I = d + 1; I <= n; I ++)
{
R [0] = r [I]; // Save the inserted records
Int j;
For (j = I-d; j> 0 & r [0] <r [j]; j = j-d) r [j + d] = r [j]; // record d locations after moving
R [j + d] = r [0];

}
}
For (int I = 1; I <= n; I ++) cout <r [I] <"";
Cout <"\ n ";
}

Void BubbleSort (int r [], int n) // Bubble Sorting
{
Int temp, exchange, bound;
Exchange = n; // the range of the first Bubble Sorting is r [0] To r [n-1]
While (exchange) // This sorting is performed only when there is a record exchange in the previous sorting.
{
Bound = exchange;
Exchange = 0;
For (int j = 1; j <bound; j ++) // a Bubble Sorting
If (r [j]> r [j + 1])
{
Temp = r [j];
R [j] = r [j + 1];
R [j + 1] = temp;
Exchange = j;... the remaining full text>

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.