Sorting algorithm and complexity of time space

Source: Internet
Author: User

Sorting of nonlinear time comparison classes

First, Exchange class sort: bubble sort and quick sort

Bubble sort: Starts with the first number in the array, compares the next number with the second and then exchanges the number smaller than itself until the last number. If an interchange occurs, continue with the following steps, and if the interchange does not occur, the array is ordered and the sort ends.

Average time complexity: N-side

Space complexity: 1

If you save 10 data in 5 seconds, how long does 20 data take? The complexity of time is N-square

10*10=100 20*20=400 400/100=4 4*5=20

Quick sort: Quick sort, also known as partition Exchange Sort, is the improvement of bubble sort, and the idea of quick sorting is the thought of dividing and treating.

Select a record (usually the first record) from the N records to be sorted as the partition standard;

Move all records smaller than the column to the left, move all records larger than the sorting code to the right, and place the selected records in the middle, called the first order;

Then repeat the process for each of the previous two sub-sequences until all the records are sorted.

Time complexity: Log with base n logarithm of 2

Complexity of space: log2n~n

Second, insert class sort: Direct insert sort and shell sort

Direct Insert sort: Starts with the second record in the N records to be sorted, compares the previous record and looks for the inserted position, and inserts the current number into the appropriate position at the end of each outer loop.

Complexity of Time: N-side

Space complexity: 1

Third, select class sort: Simple Selection sort (direct selection sort) Heap sort merge sort

Simple selection Sorting: Select the smallest data element from all records to exchange with the first position, and then in the remaining records, find the smallest and second-position record exchange, and loop to the last data element left.

Complexity of Time: N-side

Space complexity: 1

Linear time non-comparative class ordering

One, counting sort

Second, the base sort

Third, the bucket sort

Sorting algorithm and complexity of time space

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.