Eight sorting algorithms Python implementation

Source: Internet
Author: User

I. Overview

Sorting has an internal sort and an external sort, the internal sort is the data record is sorted in memory, and the external sort is because the sorted data is large, one cannot hold all the sort records at a time, and the external memory needs to be accessed during the sorting process.

Here we talk about eight sorts of sort are internal sort.

When n is larger, it should be sorted by the time complexity O (nlog2n): Quick sort, heap sort, or merge sort order.

Quick sort: is currently considered the best method based on the comparison of internal sorting, when the keywords to be sorted are randomly distributed, the average time of the fast sorting is shortest;

Second, the implementation of the algorithm

1. Insert sort-Direct insert sort (straight insertion sort)

Basic idea:

Insert a record into the sorted ordered table to get a new, sequential table with a 1 increase in the number of records. That is, the 1th record of the sequence is considered an ordered subsequence, and then inserted from the 2nd record one after the other until the entire sequence is ordered.

Important: Set up Sentinel as a temporary storage and judgment array boundary.

Example of a direct insert sort:

If you encounter an element equal to the insert, the insertion element places the element you want to insert behind the equal element. So, the order of the equal elements is not changed, the order from the original unordered sequence is the order of the sequence, so the insertion sort is stable.

The implementation of the algorithm:

Transferred from: http://blog.csdn.net/u013719780/article/details/49201143

Eight sort algorithm python implementations (RPM)

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.