Sorting Algorithm space and time complexity

Source: Internet
Author: User
Sorting Algorithm space and time complexity Simple sorting-- The Bubble Method is a self-loop in the second-dimensional cycle. The minimum or maximum values are used to select sorting and exchange sorting. The values in the second-dimensional cycle are compared with those in the first-dimensional cycle. The exchange method is the clearest, the selection method has been improved. Only the position labels are exchanged, and the complexity of the algorithm remains unchanged. Insert method, the basic working principle is to draw a card, find the corresponding position in the front card to insert, and then continue to the next one (more complex) Advanced sorting-- The quick sorting method is improved from the bubble method. The basic idea is to select a record. Generally, the first record is selected, and the records to be sorted are divided into two adjacent regions by one sorting, the keywords recorded in one region are smaller than those in another region, that is, the value of one region is greater than the obtained keyword, and the value of the other region is smaller than the obtained keyword, then the records of these two regions can be sorted separately to achieve the whole sequence order. Unlike shell sorting, the latter first has a descending step size array, which sorts the content separated by step-1, then changes the step size and goes down in sequence. Merge Sorting: first, find an intermediate position (low + high)/2 in the original record, sort the two segments separately, and then sort the data in the whole (that is, divide the data into three times ). Note: There is no final overall sorting for quick sorting, but it is first ranked. Heap sorting improves the selection and sorting, and uses the heap feature to sort the record sequence. Time ComplexityThe number of comparisons for Bubble sorting, selection sorting, and insertion sorting is O(N2), worst case O(N2), best O(N) (but it is best to sort O(N2) the complexity of fast sorting on average is O(Nlogn), worst case O(N2), best O(Nlogn) the complexity of heap sorting and Merge Sorting in the worst case is O(Nlogn ). It can be seen that Merge Sorting and heap sorting are the optimal time complexity algorithms in comparison sorting algorithms. Space complexitySpace performance is the size of auxiliary space required for sorting all simple sorting and heap sorting are 0 (1) Fast sorting is 0 (logn ), the auxiliary space required for the execution of the process stack for the recursive program is the most auxiliary space required for merging sorting and base sorting. For O (N), check the small code # include <stdlib. h> main ()
{Int M, N, P;
// Scanf ("m = % DN = % dp = % d", & M, & N, & P );
// Printf ("% d/N", M, N, P );
M = 3;
N = M & (-1); P = M & (-2 );
Printf ("% d/N", N, P );
} 1. n = M & (-1); medium n constant m p = M & (-2); medium P constant 1; (0, P is 0) the binary value of-1 is 11. 2. When using the shielded two sentences, the input m = 1N = 2 p = 3 cannot be 1 2 3.

 

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.