Analysis and induction of sorting algorithms

Source: Internet
Author: User

1. Bubble Sort

Thought example: 22 comparison between adjacent elements, small put front, large continuation with next comparison

Initial keyword sequence: all in all.

(1) [+]

(2) [More]

(3) [+]

(4) [+]

(5) [ all the above]

(6) [ all the above]

(7) [ all the above]

The final ranking result is:

Thought code:

Time Complexity Analysis:

best time: Sort complete, compare time -->n-1 Move Time - -0 O (n)

Worst time: In reverse order, compare time-to - n(n-1)/2 Move time -- > 3n(n-1) /2

O (n2)

Spatial Complexity Analysis: memory space has been in the open array space, good stability The complexity of space is O (1)

2 , directly select Sort

Thought example: Each sort will be minimized in front, and then the number of substitutions will be placed in the original position of the smallest number

Initial keyword sequence: all in all.

(1) [[]]

(2) [all]

(3) [all]

(4) [all]

(5) [all]

(6) [all]

(7) [all]

The final sort result is: the above

Thought code:

Time Complexity Analysis:

best time: Sort done, compare time-to - n(n-1)/2 Move time -- > 0 O (n2)

Worst time: In reverse order, compare time-to- n(n-1)/2 Move Time- 3(n-1) /2

O (n2)

Spatial Complexity Analysis: memory space has been in the open array space, the stability of poor The complexity of space is O (1)

3 , direct insert sort

Thought example: Starting with an initially ordered set, constantly inserting new data elements into it

Initial keyword sequence: [over]

(1) [the]

(2) [all]

(3) [all]

(4) [all]

(5) [ all the above]

(6) [ all the above]

The final sort result is: [ all the above]

Thought code:

Time Complexity Analysis:

best time: Sort done, compare time- n-1 Move Time - 0 O (n)

Worst time: In reverse order, compare time-to (n-1) (n + 2) c13>/2

Move time --(n-1) (n + 4)/2 O (n2)

Spatial Complexity Analysis: memory space has been in the open array space, good stability The complexity of space is O (1)

4 , Quick Sort

Thought example: An element is divided into two parts as a standard data element, and all elements on the left end are less than that standard element, and all elements on the right end are larger than the standard element

Initial keyword sequence: [The]

First trip sort process: [All]

[27 34 23 15 80 67 29 46]

[27 23 34 15 80 67 29 46]

[27 23 15 34 80 67 29 46]

[27 23 15 29 80 67 34 46]

[27 23 15 29 34 67 80 46]

(1) { + +}

(2) { all the above}

The final sort result is: the above

Thought code:

5 , Heap sort

Thought example: Sets the set of array elements to be sorted into a fully binary tree structure, selecting one of the largest (or smallest) data elements at a time to compare the number of full binary tree values to the height

Initial keyword sequence: all in all.

Process for creating the largest heap: see notes

Thought code:


Analysis and induction of sorting algorithms

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.