Implementation of Common sorting algorithms

Source: Internet
Author: User

 

In computer science and mathematics, a sorting algorithm is a basic and commonly used algorithm. A sorting algorithm is an algorithm that can sort a string of data according to a specific sorting method. Effective ranking algorithms are important in some calculation methods so that these algorithms can be correctly answered. Sorting algorithms are also used to process text data and generate human-readable output results. Because the number of processes in actual work is huge, the sorting algorithm has a high requirement on the speed of the algorithm itself. This section describes how to use common sorting algorithms in C/C ++.
 
Implementation of Common sorting algorithms (1) → insert sorting
Insert sorting is the simplest and most intuitive sorting algorithm, it is based on: traverse to the N element before the N-1 element is already sorted, then look for the previous N-1 element and place the nth element in the proper position until the elements of the series are traversed.
The complexity of the algorithm is also simple. sorting the first requires the complexity of 1 and sorting the second requires the complexity of 2. Therefore, the overall complexity is
1 + 2 + 3 + ...... + N = O (N ^ 2) complexity. [Details]
 

Implementation of Common sorting algorithms (2) → shell sorting
Shell sorting is a modification to insert sorting. It sorts the elements of a sequence into several subsequences according to a certain increment, and inserts and sorts these subsequences, then, the number of elements in each sub-sequence is gradually reduced and expanded. Until the increment is one, the sub-sequence is the same as the original to-be-arranged sequence, at this time, you only need to do a small amount of comparison and movement to complete the sorting of the sequence. [Details]
 

Implementation of Common sorting algorithms (3) → heap sorting
Stack definition:
N key word sequences KL, K2 ,..., Kn is called a heap, and only when the sequence meets the following properties (referred to as heap properties ):
(1) KI ≤ k2i and Ki ≤ k2i + 1 or (2) KI ≥ k2i and Ki ≥ k2i + 1 (1 ≤ I ≤)
If we store the vector R [1 ...... N] as a full binary tree storage structure, the heap is essentially a Complete Binary Tree that meets the following requirements: the keywords of any non-leaf node in the tree are not greater than (or not less) keywords of the left and right children (if any) nodes.
This feature allows you to quickly locate the smallest (large) element in a sequence.
The heap sorting algorithm process is as follows: 1) obtain the smallest (large) element of the current sequence.
(2) Exchange this element with the last element, so that the current smallest (large) element is placed at the end of the sequence, the last element is placed at the beginning of the sequence.
(3) The switching may damage the nature of the heap sequence (note that the sequence removes the elements already placed at the end). Therefore, we need to adjust the sequence, it satisfies the nature of the above heap. Repeat the above process until the sequence adjustment is complete. [Details]
 

Implementation of Common sorting algorithms (4) → Bubble Sorting
The idea of the Bubble Sorting Algorithm: It is very simple. Every time the traversal completes the sequence, the largest (small) elements are placed at the beginning, and then the rest of the sequence from the front of the parent process, after each traversal, one element will be missing in the waiting sequence. When the waiting sequence is reduced to only one element, the sorting will end. Therefore, the worst case for complexity is O (n ^ 2 ). [Details]
 

Implementation of Common sorting algorithms (5) → quick sorting
Algorithm idea of fast sorting: select a hub element and separate the sort sequence. After the split, a part of the sequence is smaller than the hub element, and a part is greater than the hub element, then we perform the above process on the two split subsequences. [Details]
 

Implementation of Common sorting algorithms (6) → Merge Sorting
The algorithm idea of Merge Sorting: divides the sequence to be sorted into two parts of the same size, sorts the two parts in sequence, and then merges them in order. [Details]
 

I have summarized some common sorting algorithms, which are essential for interviews!

 

From: http://c.chinaitlab.com/special/cpxsf/index.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.